Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Client Side Localisation for Rails
Ruby CoffeeScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
app
config
lib
.gitignore
Gemfile
LICENSE
README.md
i18n-coffee.gemspec

README.md

i18n-coffee

Client Side Localisation for Rails

Gem Version

Usage

In your Gemfile add

gem 'i18n-coffee', '~> 0.1.3'

Setup locale translations

By default, it looks for javascripts node in your {locale}.yml.

# config/locales/en.yml
en:
  javascripts:
    hello: "Hello"

On the client-side

In your app/asssets/javascripts/application.js file include i18n

//= require i18n

You can now try use window.translate or window.t for translation lookup.

window.t('javascripts.hello'); // "Hello"

Based on I18n.locale in Rails, it will load the corresponding translations from your locale files.

Passing variables to translation

You can use variables to generate dynamic content in the client-side translations the same way that Rails does.

window.t('javascripts.greeting', { name: "Mann" });
# config/locales/en.yml
en:
  javascripts:
    greeting: "Hello %{name}!"

Change javascript translations root

You can choose a different root node as the translations.

# config/locales/en.yml
en:
  views:
    home:
      title: "Hello world!"

To select views.home as translation root:

# config/application.rb
config.i18n_translations_root = 'views.home'

The translation root setting = views.home in config/application.rb will become views_home in client-side lookup key.

window.t('views_home.title'); // "Hello world!"
Something went wrong with that request. Please try again.