Question

Is it possible to print a variable inside the JText

ie:- i need to print $email to test whether it is nil or has value in it...

$this->setMessage(JText::_('COM_USERS_REGISTRATION_ACTIVATE_SUCCESS'));
Was it helpful?

Solution

The JText class has a static method '_' which converts the string argument passed into it into another string, using the language files and settings appropriate for the context. If you want to see what is being passed to setMessage you could just try:

echo 'DEBUG setMessage argument: "'.JText::_('COM_USERS_REGISTRATION_ACTIVATE_SUCCESS').'"';

If you find that this reveals that _ is returning nothing, then it is likely an indication that there is not an entry for 'COM_USERS_REGISTRATION_ACTIVATE_SUCCESS' in the language file(s) being used.

EDIT:

If you need to append $email then just do this:

$this->setMessage(JText::_('COM_USERS_REGISTRATION_ACTIVATE_SUCCESS').$email);

OTHER TIPS

And if you want to use variable in the middle of string or few variables, here's an example for that:

$this->setMessage(JText::sprintf('COM_USERS_REGISTRATION_ACTIVATE_SUCCESS', $name, $email);

And in language file:

COM_USERS_REGISTRATION_ACTIVATE_SUCCESS="Hello %s, your email is: %s"

So the final output will look ($name and $email will be replaced by variable values given):

Hello $name, your email is: $email

COM_USERS_REGISTRATION_ACTIVATE_SUCCESS is being replaced by a language definition of the message. If you break that up, Joomla won't recognise it.

You can append variables to that message if you want:

$this->setMessage(JText::_('COM_USERS_REGISTRATION_ACTIVATE_SUCCESS') . $email);

Or even omit JText if you need to

$this->setMessage('some message, email: ' . $email);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top