No Code Attached Yet bug
avatar marcorensch
marcorensch
6 Jan 2025

Steps to reproduce the issue

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
")

Expected result

To pass a translation including a variable from PHP to JS.

Actual result

Language Constant is printed in console.

System information (as much as possible)

Joomla 5.2.2

Additional comments

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.

avatar marcorensch marcorensch - open - 6 Jan 2025
avatar joomla-cms-bot joomla-cms-bot - change - 6 Jan 2025
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 6 Jan 2025
avatar Fedik Fedik - change - 6 Jan 2025
Labels Added: bug
avatar Fedik Fedik - labeled - 6 Jan 2025
avatar Fedik
Fedik - comment - 6 Jan 2025

This bug is affecting all following methods: Text::sprintf(), Text::plural(), Text::passSprintf()

avatar marcorensch marcorensch - change - 6 Jan 2025
The description was changed
avatar marcorensch marcorensch - edited - 6 Jan 2025

Add a Comment

Login with GitHub to post a comment