Dynamically generate i18n/translations #141

Closed
nicolai86 opened this Issue Mar 24, 2013 · 5 comments

Comments

Projects
None yet
2 participants

Hey,

I just stumbled across this gem and it looks like a really great fit for javascript I18n w/ copy copter.
For this to work I need the i18n/translations.js to be dynamically served in production mode. I've tried to make this work with a sample app of mine, however, upon deployment I receive the following error:

can't modify immutable index
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/sprockets-2.2.2/lib/sprockets/index.rb:80:in `expire_index!'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/sprockets-2.2.2/lib/sprockets/processing.rb:91:in `register_preprocessor'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/bundler/gems/i18n-js-8801f8d17ef9/lib/i18n/js/engine.rb:11:in `block (2 levels) in <class:Engine>'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:34:in `call'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:42:in `each'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/railties-3.2.12/lib/rails/application/finisher.rb:59:in `block in <module:Finisher>'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/railties-3.2.12/lib/rails/initializable.rb:30:in `instance_exec'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/railties-3.2.12/lib/rails/initializable.rb:30:in `run'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/railties-3.2.12/lib/rails/initializable.rb:55:in `block in run_initializers'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/railties-3.2.12/lib/rails/initializable.rb:54:in `each'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/railties-3.2.12/lib/rails/initializable.rb:54:in `run_initializers'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/railties-3.2.12/lib/rails/application.rb:136:in `initialize!'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/railties-3.2.12/lib/rails/railtie/configurable.rb:30:in `method_missing'
/home/sample/tmp/build-136416562224259/config/environment.rb:5:in `<top (required)>'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `require'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `block in require'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `require'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/railties-3.2.12/lib/rails/application.rb:103:in `require_environment!'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/railties-3.2.12/lib/rails/application.rb:297:in `block (2 levels) in initialize_tasks'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/actionpack-3.2.12/lib/sprockets/assets.rake:93:in `block (2 levels) in <top (required)>'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/actionpack-3.2.12/lib/sprockets/assets.rake:60:in `block (3 levels) in <top (required)>'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/actionpack-3.2.12/lib/sprockets/assets.rake:23:in `invoke_or_reboot_rake_task'
/home/sample/tmp/build-136416562224259/vendor/bundle/ruby/2.0.0/gems/actionpack-3.2.12/lib/sprockets/assets.rake:29:in `block (2 levels) in <top (required)>'
Tasks: TOP => environment

I think the best way to fix this would to add support for a route which dynamically serves i18n/translations at run time.

@fnando what do you think? Any chance that you'll this kind of feature to this awesome gem?

ck3g commented Mar 28, 2013

👍 I've just met same issue

I've tried to reassign translations

$.getJSON Routes.api_translations_path(), (data) ->
  I18n.translations = {}
  I18n.translations[locale] = data

It's works for me

@ck3g nice to know it works well that way.

I guess it's time to create a PR for the rewrite branch, since this functionally makes client side localization a low hanging fruit :)

ck3g commented Mar 28, 2013

@nicolai86 But it's will work only for dynamically loaded content. After that call. So it's worst solution

@ck3g depends on the app & integration I guess. If you want to localize dynamically even with existing content jquery-localize might be a better fit for you. It wraps localization with a span and a key attribute so you can change it on the fly anytime you like.

My use case is a traditional web app and localization is only used in certain parts after the page has been loaded. So my dynamic content evaluation happens AFTER the locales have been loaded dynamically.

@ck3g if you want the best of both worlds on modern browsers you might want to check out my try using i18next:
https://github.com/nicolai86/rails-asset-localization

serves bundled & dynamic resources. :)

nicolai86 closed this Mar 29, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment