I18n.js
The above badges are generated by https://shields.io/
It's a small library to provide the Rails I18n translations on the JavaScript.
Features:
- Pluralization
- Date/Time localization
- Number localization
- Locale fallback
- Asset pipeline support
- Lots more! :)
Version Notice
The master
branch (including this README) is for latest 3.0.0
instead of 2.x
.
Usage
Installation
Rails app
Add the gem to your Gemfile.
gem "i18n-js"
Rails app with Asset Pipeline
If you're using the asset pipeline,
then you must add the following line to your app/assets/javascripts/application.js
.
//
// This is optional (in case you have `I18n is not defined` error)
// If you want to put this line, you must put it BEFORE `i18n/translations`
//= require i18n
// Some people even need to add the extension to make it work, see https://github.com/fnando/i18n-js/issues/283
//= require i18n.js
//
// This is a must
//= require i18n/translations
Rails app without Asset Pipeline
First, put this in your application.html
(layout file).
Then get the JS files following the instructions below.
<%# This is just an example, you can put `i18n.js` and `translations.js` anywhere you like %>
<%# Unlike the Asset Pipeline example, you need to require both **in order** %>
<%= javascript_include_tag "i18n" %>
<%= javascript_include_tag "translations", skip_pipeline: true %>
There are two ways to get translations.js
(For Rails app without Asset Pipeline).
- This
translations.js
file can be automatically generated by theI18n::JS::Middleware
.
Just addconfig.middleware.use I18n::JS::Middleware
to yourconfig/application.rb
file. - If you can't or prefer not to generate this file,
you can move the middleware line to yourconfig/environments/development.rb
file
and runrake i18n:js:export
before deploying.
This will export all translation files, including the custom scopes
you may have defined onconfig/i18n-js.yml
.
IfI18n.available_locales
is set (e.g. in your Railsconfig/application.rb
file)
then only the specified locales will be exported.
Current version ofi18n.js
will also be exported to avoid version mismatching by downloading.
Export Configuration (For translations)
Exported translation files generated by I18n::JS::Middleware
or rake i18n:js:export
can be customized with config file config/i18n-js.yml
(use rails generate i18n:js:config
to create it).
You can even get more files generated to different folders and with different translations to best suit your needs.
The config file also affects developers using Asset Pipeline to require translations.
Except the option file
, since all translations are required by adding //= require i18n/translations
.
Examples:
translations:
- file: 'public/javascripts/path-to-your-messages-file.js'
only: '*.date.formats'
- file: 'public/javascripts/path-to-your-second-file.js'
only: ['*.activerecord', '*.admin.*.title']
If only
is omitted all the translations will be saved. Also, make sure you add that initial *
; it specifies that all languages will be exported. If you want to export only one language, you can do something like this:
translations:
- file: 'public/javascripts/en.js'
only: 'en.*'
- file: 'public/javascripts/pt-BR.js'
only: 'pt-BR.*'
Optionally, you can auto generate a translation file per available locale if you specify the %{locale}
placeholder.
translations:
- file: "public/javascripts/i18n/%{locale}.js"
only: '*'
- file: "public/javascripts/frontend/i18n/%{locale}.js"
only: ['*.frontend', '*.users.*']
You can also include ERB in your config file.
translations:
<% Widgets.each do