Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text::sprintf option "Script" has no effect #44687

Open
marcorensch opened this issue Jan 6, 2025 · 1 comment
Open

Text::sprintf option "Script" has no effect #44687

marcorensch opened this issue Jan 6, 2025 · 1 comment

Comments

@marcorensch
Copy link
Contributor

marcorensch commented Jan 6, 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.

marcorensch pushed a commit to marcorensch/Joomla-Manual that referenced this issue Jan 6, 2025
… "script" option because its not working as described. See joomla/joomla-cms#44687
@Fedik Fedik added the bug label Jan 6, 2025
@Fedik
Copy link
Member

Fedik commented Jan 6, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants