i18n-js

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

Github星跟蹤圖

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

主要指標

概覽
名稱與所有者fnando/i18n-js
主編程語言Ruby
編程語言Ruby (語言數: 4)
平台Linux, Mac, Windows
許可證MIT License
所有者活动
創建於2009-09-02 03:35:45
推送於2024-11-14 17:30:02
最后一次提交
發布數79
最新版本名稱v4.2.3 (發布於 2023-03-29 12:15:44)
第一版名稱pre-cleanup (發布於 2010-04-06 19:21:40)
用户参与
星數3.8k
關注者數52
派生數520
提交數82
已啟用問題?
問題數387
打開的問題數4
拉請求數178
打開的拉請求數4
關閉的拉請求數126
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?