You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Alex Kotchnev <ak...@gmail.com> on 2009/02/11 07:01:07 UTC

[T5] Internationalizing included js

What is the best method for internationalizing strings inside of .js files
included using the @IncludeJavaScript annotation ? I've looked around and I
see that I can use the regular message catalog and string substitution (e.g.
addScript(String format, Object... arguments);) if the script is attached to
a particular element or something similar. However, if I am including the js
using the @IncludeJavaScript annotation, what is the best approach ? I've
tried (and it works great) to add localized js assets (e.g. foo.js,
foo_bg.js, etc) and the localization works nicely. However, my little hangup
with that is the actual code inside of the js is the same, just the messages
are different. Can I somehow configure T5 to process the js (just like a tml
file) and do message substitution inside (e.g. so that I can use expansions
like ${message:foo-message} inside the js file).

I guess I can also include the js inline in the .tml and the message catalog
substitution would work.

Cheers,

Alex K

Re: [T5] Internationalizing included js

Posted by Alex Kotchnev <ak...@gmail.com>.
Thiago / Jonathan / Fernando,
   thanks for everyone's input !

Indeed, the asset localization of js does work out OK. Refactoring the code
would certainly get the job done.  I've also used the ${message:foo}
expansions in js that's included in a page, that works OK too.

I guess the issue is that I was trying to package up a component that has
some error messages in a js file. Ideally, all messages would be in message
bundles, and adding a translation would only involve  dealing with bundles
(as that provides the least opportunities for mistakes). I just wanted to
make sure that I'm not missing something obvious in how to handle the issue.


Cheers,

Alex Kotchnev

On Wed, Feb 11, 2009 at 12:52 PM, Fernando Padilla <fe...@alum.mit.edu>wrote:

> Not the best option but:
>
> Don't forget that included js files are determined using Tapestry's
> Internationalication/Localization code..
>
> http://tapestry.apache.org/tapestry5/guide/localization.html
>
> so:
>
> include( my.js )
>
> would look up the right js file using the locale of the user:
>
> my_en.js
> my_fr.js
>
>
>
> So you could refactor your js file into two.. one for locale specific
> variables and messages, and another just for code.. Then tapestry will
> choose the correct messages js file using the user's locale...
>
> include( mymessages.js, mycode.js )
>
> mymessages_en.js
> mymessages_fr.js
> mycode.js
>
>
>
> This won't help if you just wanted to leverage the Message bundles that the
> app is already using..  To do that, we would have to cook up some code and
> stuff to transform the js files as they are served up (no support for that
> at the moment ).
>
>
>
>
> Thiago H. de Paula Figueiredo wrote:
>
>> On Wed, Feb 11, 2009 at 7:08 AM, Jonathan O'Connor <ni...@eircom.net>
>> wrote:
>>
>>> Alex, according to Thiago, you can have string substitution in the
>>> javascript:
>>>
>>
>> Yes, if the Javascript is generated inside the page, not in an external
>> file. ;)
>>
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: [T5] Internationalizing included js

Posted by Fernando Padilla <fe...@alum.mit.edu>.
Not the best option but:

Don't forget that included js files are determined using Tapestry's 
Internationalication/Localization code..

http://tapestry.apache.org/tapestry5/guide/localization.html

so:

include( my.js )

would look up the right js file using the locale of the user:

my_en.js
my_fr.js



So you could refactor your js file into two.. one for locale specific 
variables and messages, and another just for code.. Then tapestry will 
choose the correct messages js file using the user's locale...

include( mymessages.js, mycode.js )

mymessages_en.js
mymessages_fr.js
mycode.js



This won't help if you just wanted to leverage the Message bundles that 
the app is already using..  To do that, we would have to cook up some 
code and stuff to transform the js files as they are served up (no 
support for that at the moment ).




Thiago H. de Paula Figueiredo wrote:
> On Wed, Feb 11, 2009 at 7:08 AM, Jonathan O'Connor <ni...@eircom.net> wrote:
>> Alex, according to Thiago, you can have string substitution in the javascript:
> 
> Yes, if the Javascript is generated inside the page, not in an external file. ;)
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [T5] Internationalizing included js

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Wed, Feb 11, 2009 at 7:08 AM, Jonathan O'Connor <ni...@eircom.net> wrote:
> Alex, according to Thiago, you can have string substitution in the javascript:

Yes, if the Javascript is generated inside the page, not in an external file. ;)

-- 
Thiago

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [T5] Internationalizing included js

Posted by Jonathan O'Connor <ni...@eircom.net>.
Alex,
according to Thiago, you can have string substitution in the javascript:
 >>>THIAGO
Another solution is to put a Tapestry expression expansion inside the 
Javascript code:

Page class:

public Link getEventLink() {
     return componentResources.createXXXLink(parameters);
}

Template:

window.location.href='${eventLink}' + this.cells[1].textContent;
<<<THIAGO

So presumably, you can use "${message:some-message-key}", and it will be 
translated properly.

I haven't tried this myself, so please let me know if this works.
Ciao,
Jonathan
On 11/02/2009 06:01, Alex Kotchnev wrote:
> What is the best method for internationalizing strings inside of .js files
> included using the @IncludeJavaScript annotation ? I've looked around and I
> see that I can use the regular message catalog and string substitution (e.g.
> addScript(String format, Object... arguments);) if the script is attached to
> a particular element or something similar. However, if I am including the js
> using the @IncludeJavaScript annotation, what is the best approach ? I've
> tried (and it works great) to add localized js assets (e.g. foo.js,
> foo_bg.js, etc) and the localization works nicely. However, my little hangup
> with that is the actual code inside of the js is the same, just the messages
> are different. Can I somehow configure T5 to process the js (just like a tml
> file) and do message substitution inside (e.g. so that I can use expansions
> like ${message:foo-message} inside the js file).
>
> I guess I can also include the js inline in the .tml and the message catalog
> substitution would work.
>
> Cheers,
>
> Alex K
>
>    

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org