i18n-js

它是一个小型库,用于在 Javascript 上提供 I18n 翻译。它提供了 Rails 支持。「It's a small library to provide the I18n translations on the Javascript. It comes with Rails support.」

Github stars Tracking Chart

I18n.js

Gem Version
npm
License: MIT

Build Status
Coverage Status

Gitter

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).

  1. This translations.js file can be automatically generated by the I18n::JS::Middleware.
    Just add config.middleware.use I18n::JS::Middleware to your config/application.rb file.
  2. If you can't or prefer not to generate this file,
    you can move the middleware line to your config/environments/development.rb file
    and run rake i18n:js:export before deploying.
    This will export all translation files, including the custom scopes
    you may have defined on config/i18n-js.yml.
    If I18n.available_locales is set (e.g. in your Rails config/application.rb file)
    then only the specified locales will be exported.
    Current version of i18n.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

Overview

Name With Ownerfnando/i18n-js
Primary LanguageRuby
Program languageRuby (Language Count: 4)
PlatformLinux, Mac, Windows
License:MIT License
Release Count79
Last Release Namev4.2.3 (Posted on 2023-03-29 12:15:44)
First Release Namepre-cleanup (Posted on 2010-04-06 19:21:40)
Created At2009-09-02 03:35:45
Pushed At2024-03-01 01:01:57
Last Commit At
Stargazers Count3.7k
Watchers Count54
Fork Count519
Commits Count81
Has Issues Enabled
Issues Count385
Issue Open Count3
Pull Requests Count177
Pull Requests Open Count1
Pull Requests Close Count125
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private
To the top