Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locale specific javascript files with assets pipeline

So what I want to know is whether the following is possible. I have a website where we internationalise using the standard I18n gem (with translations in config/locales/*.yml). At the moment we've relied on a variety of (pretty inelegant) fixes to get javascript to pick up translated words (mostly by use of the HTML5 data- attributes). Now that we've upgraded our site to Rails 3.1, and we're liking the new asset pipeline stuff, it'd be great if we could internationalize up our js files something like the following:

app/assets/jsfile.js.erb

alert(<%= I18n.t('javascript.key.name') %>)

and then get the assets pipeline to iterate over the file to generate public/assets/jsfile.en.js, public/assets/jsfile.de.js etc etc (I'm guessing that a locale aware javascripts_include_tag would also be useful)

Is anyone aware of any examples/gems/plugins that could do this kind of job, or is it time to crack out the IDE and start coding up something myself?

like image 432
Chris Bailey Avatar asked Aug 25 '26 13:08

Chris Bailey


1 Answers

I had the same problem and in the end I decided to implement something. I added to assets.rake some tasks which create the localized files specified in config.assets.localized_precompile, for example:

config.assets.localized_precompile = ['application-it.js', 'application-fr.js', 'application-de.js', 'application-en.js']

Then I'm using

=javascript_include_tag "application-#{I18n.locale}"

And application-xx.js are all like:

//= require application

I can share more details if you are interested

like image 175
pic Avatar answered Aug 27 '26 05:08

pic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!