JText function in javascript code

1,824 views
Skip to first unread message

ced1870

unread,
Apr 17, 2011, 12:02:02 PM4/17/11
to Joomla! General Development
Hi
In the component tutorial for Joomla! 1.6 on the wiki I've just seen
that there is a JS fuction to emulate JText to have translatable
strings.
It looks like this :
Joomla.JText._('KEY','default text')

But for me it returns only 'default key' and not the translated string
that match with the KEY.
Can someone help ?
CEd

Ian MacLennan

unread,
Apr 17, 2011, 1:06:15 PM4/17/11
to joomla-de...@googlegroups.com
Do you have language debugging on?  Is the language file where the key is being loaded?

Ian


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.


Ken Ballou

unread,
Apr 17, 2011, 1:27:21 PM4/17/11
to joomla-de...@googlegroups.com
Please provide a URL for where you found this on the wiki.

Have you called Joomla.JText.load() to load the translation strings?
Take a look at the implementation of Joomla.JText in
media/system/js/core-uncompressed.js (lines 56-67 in this file for the
Joomla! 1.6.2 release). If you haven't loaded any translation strings,
the "strings" object will be empty, and you will always get back the key
you passed in.

I apologize in advance if this is trivially obvious, but remember that
JavaScript executes in the browser client, not in the web server. The
translation strings installed on the server aren't going to help you in
the client.

- Ken

Ken Ballou

unread,
Apr 17, 2011, 1:52:59 PM4/17/11
to joomla-de...@googlegroups.com
I just dug a little more into this (grep is your good friend).

The call to Joomla.JText.load() is handled for you in
JDocumentRendererHead::fetchHead() (file
libraries/joomla/document/html/renderer/head.php), but you need to have
specified somewhere that you want the translation to be available in
JavaScript.

The easiest way to do that is to call JText::script with the key to be
translated. (The translation needs to be available [that is, the
language file with the translation must have been loaded] when you call
JText::script().)

There are three examples of this in the 1.6.2 distribution:

1. administrator/components/com_users/views/groups/tmpl/default.php
2. administrator/templates/hathor/html/com_users/groups/default.php
3 installation/views/filesystem/view.html.php

There are other ways of making the translation available to JavaScript,
such as passing true as the fourth parameter to JText::_(). The
implementation of JText is in libraries/joomla/methods.php (along with
JRoute).

- Ken

elin

unread,
Apr 17, 2011, 7:39:06 PM4/17/11
to joomla-de...@googlegroups.com
Ken,

Would you mind posting that in the wiki?

Elin

ced1870

unread,
Apr 18, 2011, 7:11:30 AM4/18/11
to Joomla! General Development
Hi
thanks for your replies.
I finally found the answer here :
http://wiki.joomla-nafu.de/joomla-dokumentation/Benutzer:Elkuku/Proyektz/EasyCreator/Language_handling/Javascript_translations

You just have to declare the string in your PHP file with
JText::script('KEY');

And then use it in the javascript file as
alert(Joomla.JText._('KEY');

This works !
have a nice day
CEd

Christopher Noyes

unread,
Apr 18, 2011, 7:33:04 AM4/18/11
to joomla-de...@googlegroups.com

ced1870 <ced...@gmail.com> wrote:

--

El KuKu

unread,
Apr 18, 2011, 2:45:43 PM4/18/11
to Joomla! General Development
Thanks for quoting my artice ;) I still believe that:
<quote>
While the Joomla! method works great if you have only a few
translatable strings in your JavaScript files. For extensions with
more then a few alert boxes it might seem quite redundant to define
your strings three times..
</quote>

Happy coding, anyway

On 18 Apr., 06:33, Christopher Noyes
<christopher.no...@crispmedia.com> wrote:
> ced1870 <ced1...@gmail.com> wrote:
>
> Hi
> thanks for your replies.
> I finally found the answer here :http://wiki.joomla-nafu.de/joomla-dokumentation/Benutzer:Elkuku/Proye...
Reply all
Reply to author
Forward
0 new messages