Question

I am building a single page ajax app with BackboneJS and a RoR 3.2 (upgraded from 3.1) backend. I use a client side I18n javascript translation library that holds its data in a JSON object. I am managing the translations in the backend with yml files and the default i18n gem. I have one translation asset per language, e.g English:

//FILE: <project_root>/app/assets/javascripts/translations/en.js.erb:
I18n || (I18n = {});
I18n.translations || (I18n.translations= {});
I18n.translations["en"] = <%= I18n.with_locale(:en) { I18n.t(".") }.to_json %>;

My translations are under config/locales/ and its sub-directories. I configured the load path as follows:

config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]

So far so good, everything works fine, however, new translations added to the yaml files are not automatically available on the client after a page reload in dev mode. The new translations are only available if I restart the server and make a change in the corresponding translation asset (e.g. adding a blank line). This routine is pretty tedious...

  1. How can I explicitly tell the asset pipeline/sprockets to reprocess my translation assets every request? Or even better, is there a (simple) solution to monitor the yaml files and tell sprockets to reprocess the translation assets?
  2. Rails somehow does not auto-update the i18n translation hash when new translations are added to new or existing yaml files. I don't know why but this should normally work. Any clue?
Was it helpful?

Solution

  1. i have not seen the possibility to reload it on every request. i think that this needs to be addressed in the i18n-js gem. deleting tmp/i18n-js.cache was not helpful. the thing that you can do is call rake i18n:js:export which should reload the js output. combined with guard, you could do a reloading yourself.

  2. rails misses new translation-files, but adding translations to an existing file should work. you can also force it with I18n.backend.reload!

OTHER TIPS

Rails 4.x

I was having trouble getting I18n.js to reload files after changes.

Following the docs I found that I had to run rake i18n:js:setup even though the asset pipeline is not disabled in my app.

After that change, with a I18n-js.yml file now in the config directory, I make my changes and run rake i18n:js:export and all works.

Hopefully this helps someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top