Joomla JText translation in javascript

A place to discuss Joomla! translation matters.

Moderator: wendhausen

Locked
joomilove
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Wed Jun 19, 2013 4:52 pm

Joomla JText translation in javascript

Post by joomilove » Wed Jun 19, 2013 6:16 pm

I'm porting a small web app (PHP + Javascript) running in a JUMI element from Joomla 1.5 to 2.5.

Basically in 1.5 for translation we used jsJText to make the translated strings available in Javascript. From what I understood in several tutorials, 2.5 should make this easier since support for translation in Javascript is integrated.

After checking several sites and google results they all pretty much say: First add your strings to the javascript language store in PHP by using JText::script('key'); and later retrieve them in javascript using Joomla.JText._('key') or Joomla.JText.strings.key.

I'm having quite a bit of trouble getting this to work however. Translations in PHP work perfectly fine, but in Javascript I end up getting undefined instead of the correct translated text.

I've taken a small screenshot illustrating my approach and the problem.
Gfglu.png
The PHP file containing the language strings is included into the main PHP page which shows the content. This is pretty much the first include in the page.

I'm pretty sure I've made a stupid mistake somewhere, but honestly can't find it for the life of me. Anyone that can point me in the right direction of where to search would be greatly appreciated.

Thank you.

20130612-Edit:

A little more info on the stuff I've tried suggested in the comments:

Enabling joomla debuggin didn't help. No errors there.
Mootools is loaded, version 1.4.5.
I've also tried something a little unorthodox. The list of JText::script lines are in a separate PHP file. I've tried including that file into a random module and showing that module on the same page as my web app. At that point it did work. However, this isn't really a solution to the problem but just a rather dirty workaround.

That test does however confirm there's no real problem with the code itself. I'm just missing something somewhere. Still stumped as to what's missing. Any pointers on what it could be?

20130612-Edit2:

I've compared the page source when the include is in a random module, and when it's in my webapp. The only difference seems to be that the following Javascript is added to the page, which I believe should be generated by the JText::script code so... I'm really stumped.

Code: Select all

<script type="text/javascript">
      (function() {
        var strings = {"PA_ITEM_BLOWD":"Blow-dryer"...};
        if (typeof Joomla == 'undefined') {
          Joomla = {};
          Joomla.JText = strings;
        }
        else {
          Joomla.JText.load(strings);
        }
      })();
    </script>
You do not have the required permissions to view the files attached to this post.
Joomla! is a extremely customizable

Locked

Return to “Translations”