No Code Attached Yet bug
avatar ViscountVic
ViscountVic
29 Aug 2023

Steps to reproduce the issue

It's difficult to give a test case for this, so I just wanted to get a sanity check before creating a test case and submitting a PR.

The problem is ... in an extension with cacheable views, if you add JS language strings with ...

JText::script('FOO_BAR');

The expected result ...

Expected result

... is that when your view loads from cache, you still have your translated strings in Joomla.JText in the Javascript.

But the actual result ...

Actual result

.. is you get the untranslated FOO_BAR string.

System information (as much as possible)

Joomla 4.3.4

Additional comments

If turn off cache features, then string loading right. First time string can be loaded, but after refresh page string will be missed in joomla.jtext array.

avatar ViscountVic ViscountVic - open - 29 Aug 2023
avatar joomla-cms-bot joomla-cms-bot - change - 29 Aug 2023
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 29 Aug 2023
avatar dgrammatiko
dgrammatiko - comment - 1 Sep 2023

How are you creating the cached pages?

avatar ViscountVic
ViscountVic - comment - 1 Sep 2023

Standart System Cache -> ON - Conservative caching
Platform Specific Caching - OFF

First test:
Cache Handler - File
Second test:
Cache Handler - Redis

avatar dgrammatiko
dgrammatiko - comment - 1 Sep 2023

Both fail?

avatar ViscountVic
ViscountVic - comment - 1 Sep 2023

Both fail?

Yes, same problem in both variants. I checked hosting version, then local server backup version. Same result in 2 different servers - text without translation. If I'm turning off "Standart System Cache" then all working right - I have translated JavaScript strings.

avatar dgrammatiko
dgrammatiko - comment - 1 Sep 2023

I can replicate this. Here were my steps:

  • turn on caching selecting Conservative
  • edit the module menu layout and added \Joomla\CMS\Language\Text::script('MOD_MENU_FIELD_TAG_ID_LABEL');
  • Checked the string in the browser with Joomla.Text._('MOD_MENU_FIELD_TAG_ID_LABEL') and got back MOD_MENU_FIELD_TAG_ID_LABEL

Worth noting that the cached file contains the translated strings but those are not added to the JSON object thus not available in the browser:

Screenshot 2023-09-01 at 13 08 39
avatar Fedik Fedik - change - 1 Sep 2023
Labels Added: bug
avatar Fedik Fedik - labeled - 1 Sep 2023
avatar Fedik
Fedik - comment - 1 Sep 2023

Interesting, Script Options works as expected, and Text not, but it also part of ScriptOptions.

To reproduce use following PHP, in components/com_content/src/View/Article/HtmlView.php in display():

Text::script('JYES');
$this->document->addScriptOptions('yes', 'Yes');
$this->document->getWebAssetManager()
    ->addInlineScript('console.log(Joomla.getOptions("yes", "nope") + " " + Joomla.Text._("JYES", "Nope"))', [], ['type' => 'module']);

And check browser console:
Expected: Yes Yes

After cache enabled:
First request: Yes Yes
All following requests: Yes Nope

avatar dgrammatiko
dgrammatiko - comment - 1 Sep 2023

@Fedik I think the merge should be deep by default (now iirc is shallow replace)

avatar Fedik
Fedik - comment - 1 Sep 2023

Which merge do you mean?

avatar dgrammatiko
dgrammatiko - comment - 1 Sep 2023

public function addScriptOptions($key, $options, $merge = true)

Called here:

$this->addScriptOptions($key, $scriptOptions, true);

avatar Fedik
Fedik - comment - 1 Sep 2023

It already use array_replace_recursive

I do not find problem in mergeHeadData() it do not called for me, at all.
Hovewer setHeadData() set all corectly, and then all gone.

avatar Fedik
Fedik - comment - 1 Sep 2023

Ah, I know, it do not restore Text::$strings, it stay empty,
and any further call of Text::script(), override everything in scriptOptions.

hmhm

avatar dgrammatiko
dgrammatiko - comment - 1 Sep 2023

I think the mergeHeadData needs also the code for passing the text to script options:

*/
$data['scriptText'] = Text::getScriptStrings();
$data['scriptOptions'] = $this->scriptOptions;

avatar Fedik
Fedik - comment - 1 Sep 2023

Nope, that should be deleted in future, it not in use.
I have some ideas, need to test.

avatar dgrammatiko
dgrammatiko - comment - 1 Sep 2023

it not in use.

Well, well, well:

avatar Fedik
Fedik - comment - 1 Sep 2023

It just a trash in the cache, it not used later ?
You can ignore that. I will do some test and will make a PR.
If I right, the probblem in the Text class.

avatar dgrammatiko
dgrammatiko - comment - 1 Sep 2023

I'll bet my money here:

public static function getWorkarounds($data, $options = [])

avatar Fedik
Fedik - comment - 1 Sep 2023

How much? ?

avatar dgrammatiko
dgrammatiko - comment - 1 Sep 2023

100 EUR ?

avatar Fedik
Fedik - comment - 1 Sep 2023

I will send you my Bank account info ?

avatar Fedik Fedik - change - 1 Sep 2023
Status New Closed
Closed_Date 0000-00-00 00:00:00 2023-09-01 15:57:06
Closed_By Fedik
avatar Fedik Fedik - close - 1 Sep 2023
avatar Fedik
Fedik - comment - 1 Sep 2023

Please test #41543

Add a Comment

Login with GitHub to post a comment