Create a language constant in an example project:
MOD_EXAMPLE_ARGS_EXAMPLE="Trying to use %s inside JavaScript"
Text::sprintf("MOD_EXAMPLE_ARGS_EXAMPLE", 12, array("script" => true));
// The last argument can take an array of options
// [...] array('jsSafe'=>boolean, 'interpretBackSlashes'=>boolean, 'script'=>boolean)
// [...] script is a boolean to indicate that the string will be push in the javascript language store.
// See description of the Text::sprintf method
$registeredStrings = Text::getScriptStrings();
// Output the array for debugging or custom processing >> all fine here the "MOD_EXAMPLE_ARGS_EXAMPLE" is listed.
echo '<pre>' . var_export($registeredStrings, true) . '</pre>';
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
$wa->addInlineScript("
console.log(Joomla.Text) // not listed here
const message = Joomla.Text._('MOD_EXAMPLE_ARGS_EXAMPLE')
console.log(message) // results in printing out the constant
")
To pass a translation including a variable from PHP to JS.
Language Constant is printed in console.
Joomla 5.2.2
I am currently working on the documentation and stumbled across this when I wanted to explain the use of the options based on the method description. To be honest, I read about this for the first time when researching the topic and I'm not quite sure whether this functionality still exists at all? It would make sense and based on the method description it would still exist - at least on the php side everything still works - the constant incl. variable is transferred to the array when queried with Text::getScriptStrings();
. I would also like to document this in detail, as it is a good way to at least output php variables in translations that are used in JS.
Labels |
Added:
No Code Attached Yet
|
Labels |
Added:
bug
|
This bug is affecting all following methods:
Text::sprintf()
,Text::plural()
,Text::passSprintf()