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?

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top