Adding javascript and language constants

931 views
Skip to first unread message

Fran M.

unread,
Mar 1, 2012, 4:26:24 AM3/1/12
to Joomla! General Development
Hi,
I am adding a javascript code in the front end template:

<?php
$filename = 'filename.js';
// Add the path parameter if the path is different than 'media/system/
js/'
$path = 'path/to/file/';
JHTML::script($filename, $path);
?>

But my custom language constants are not translated.
How can I use language constants in javascript?
Thanks

piotr_cz

unread,
Mar 1, 2012, 6:52:48 AM3/1/12
to Joomla! General Development
Actually you have to use JDocument to translate the strings in order
to use them.

1) In the php files add them using
`JText::script('TPL_LANGUAGE_STRING')`. JDocument will translate them
and add to the language store.
2) In your js code reference them the strings using
`Joomla.JText._('TPL_LANGUAGE_STRING', 'fallback string')` (Maybe you
are doing it already).

There are few other ways, but for template you should be fine with
this, easiest one.
Check out how com_finder works in Joomla 2.5 or these references:

http://api.joomla.org/Joomla-Platform/Language/JText.html
http://docs.joomla.org/JText/ (when the strings are to be in the
rendered html as well as in js)
http://docs.joomla.org/JText::script/11.1 (exclusively js).

I haven't tested the method above recently, but this worked for me in
the past.

Fran M.

unread,
Mar 1, 2012, 10:01:29 AM3/1/12
to Joomla! General Development
I cant make it work.
I have this in my php template:




$document = JFactory::getDocument();
JText::script('MY_CONST');
$document->addScript( 'myfile.js' );




in my site language file I have the constant.




and "myfile.js" has:




....
alert("<?php  echo JText::_('MY_CONST');?>");
.....




But the alert shows:
<?php  echo JText::_('MY_CONST');?>

What I am doing wrong?
Thanks


On 1 mar, 12:52, piotr_cz <pkoniec...@hotmail.com> wrote:
> Actually you have to use JDocument to translate the strings in order
> to use them.
>
> 1) In the php files add them using
> `JText::script('TPL_LANGUAGE_STRING')`. JDocument will translate them
> and add to the language store.
> 2) In your js code reference them the strings using
> `Joomla.JText._('TPL_LANGUAGE_STRING', 'fallback string')` (Maybe you
> are doing it already).
>
> There are few other ways, but for template you should be fine with
> this, easiest one.
> Check out how com_finder works in Joomla 2.5 or these references:
>
> http://api.joomla.org/Joomla-Platform/Language/JText.htmlhttp://docs.joomla.org/JText/(when the strings are to be in the
> rendered html as well as in js)http://docs.joomla.org/JText::script/11.1(exclusively js).

Niels Braczek

unread,
Mar 1, 2012, 10:29:54 AM3/1/12
to joomla-de...@googlegroups.com
Am 01.03.2012 16:01, schrieb Fran M.:

> and "myfile.js" has:
> ....
> alert("<?php echo JText::_('MY_CONST');?>");
> .....
> But the alert shows:
> <?php echo JText::_('MY_CONST');?>
>
> What I am doing wrong?

*.js is not parsed by the PHP interpreter. Try renaming your file to
"myfile.php". Maybe you'll have to add an extra PHP line to the
beginning of the file to set the Content-Type header.

Regards,
Niels

--
| http://barcamp-wk.de · 1. Barcamp Westküste 30./31. März 2012 |
| http://www.bsds.de · BSDS Braczek Software- und DatenSysteme |
| Webdesign · Webhosting · e-Commerce · Joomla! Content Management |
------------------------------------------------------------------

Fran M.

unread,
Mar 1, 2012, 10:41:45 AM3/1/12
to Joomla! General Development
Thanks Niels.
What do you mean by "add an extra PHP line to the
beginning of the file to set the Content-Type header."?

Niels Braczek

unread,
Mar 1, 2012, 11:35:44 AM3/1/12
to joomla-de...@googlegroups.com
Am 01.03.2012 16:41, schrieb Fran M.:

> What do you mean by "add an extra PHP line to the
> beginning of the file to set the Content-Type header."?

<?php header('Content-Type: text/javascript'); ?>

Fran M.

unread,
Mar 1, 2012, 1:24:58 PM3/1/12
to Joomla! General Development
Thanks it worked.
Reply all
Reply to author
Forward
0 new messages