If you would like to internationalize your javascript/css files the same way you do in the rest of application... then this is for you.
= AssetPipelineI18n
Shortly: asset-pipeline + i18n
Some additional words: you would like to internationalize your javascript/css files the same way you do in the rest of application. For example you would like to write something like:
app/assets/file.js.erb
alert(<%= I18n.t(‘javascript.key.name’) %>)
That works OK in development mode but with asset pipeline you have only one precompiled and (cached) application.js file translated with the default language.
What you would need is a collection of language specific application.js files, like application-en.js, application-it.js, etc. This gem is exactly for that!
== Usage
If not already done, define all locales your application uses in an initializer:
I18n.available_locales = %w(de it en fr)
Add the gem to your Gemfile:
gem ‘asset_pipeline_i18n’
In your application.rb add:
config.localized_assets = [“application-%{locale}.js”]
Now define all the application-xx.js files for every locale like that:
//= require application
And of course, in your layout, you also have to use something like:
javascript_include_tag “application-#{I18n.locale}”
Finally:
rake assets:precompile
== Known problems
=== Remove “require_tree .”
It is no more possible to write
//= require_tree .
in application.js because this will create a circular dependency now. One easy workaround is to move all the js files
in a subdirectory (e.g. lib) and then:
//= require_tree ./lib
=== No proper i18n in development environment
This gem is intended for compiling translated assets for production environment.
Unfortunately it does not provide proper translations when running in development environment due to assets caching.
== Versioning
The idea is to use the same version number of the Rails version the gem is compatible with. So asset_pipeline_i18n version 3.2.5 is for Rails 3.2.5 and so on.
If the gem is broken and we have to release a fix, then we will use a forth level number, like 3.2.5.1
To keep things easy, the dependency of the gem if of the form ~> x.x.x, so we assume that it works also with new versions of Rails, until proved otherwise 😃
== Contacts
Home page: https://github.com/simplificator/asset_pipeline_i18n