Polyglot.js

让您的 JavaScript 能够讲多种语言。「Give your JavaScript the ability to speak many languages.」

  • 所有者: airbnb/polyglot.js
  • 平台: Linux, Mac, Windows
  • 许可证: BSD 2-Clause "Simplified" License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

Polyglot.js

[![Build Status][travis-image]][travis-url]

Join the chat at https://gitter.im/airbnb/polyglot.js

Polyglot.js is a tiny I18n helper library written in JavaScript, made to work both in the browser and in CommonJS environments (Node). It provides a simple solution for interpolation and pluralization, based off of Airbnb’s experience adding I18n functionality to its Backbone.js and Node apps.

I18n is incredibly important for us at Airbnb, as we have listings in 192 countries, and we translate our site into 30-odd different languages.
We’re also hiring talented engineers to help us scale up to meet the challenges of buliding a global marketplace.

View the documentation on Github.

View the annotated source.

Polylglot is agnostic to your translation backend. It doesn’t perform any translation; it simply gives you a way to manage translated phrases from your client- or server-side JavaScript application.

Installation

install with npm:

$ npm install node-polyglot

Running the tests

Clone the repo, run npm install, and npm test.

Usage

Instantiation

First, create an instance of the Polyglot class, which you will use for translation.

var polyglot = new Polyglot();

Polyglot is class-based so you can maintain different sets of phrases at the same time, possibly in different locales. This is very useful for example when serving requests with Express, because each request may have a different locale, and you don’t want concurrent requests to clobber each other’s phrases.

See Options Overview for information about the options object you can choose to pass to new Polyglot.

Translation

Tell Polyglot what to say by simply giving it a phrases object,
where the key is the canonical name of the phrase and the value is
the already-translated string.

polyglot.extend({
  "hello": "Hello"
});

polyglot.t("hello");
=> "Hello"

You can also pass a mapping at instantiation, using the key phrases:

var polyglot = new Polyglot({phrases: {"hello": "Hello"}});

Polyglot doesn’t do the translation for you. It’s up to you to give it
the proper phrases for the user’s locale.

A common pattern is to gather a hash of phrases in your backend, and output
them in a <script> tag at the bottom of the document. For example, in Rails:

app/controllers/home_controller.rb

def index
  @phrases = {
    "home.login" => I18n.t("home.login"),
    "home.signup" => I18n.t("home.signup"),
    ...
  }
end

app/views/home/index.html.erb

<script>
  var polyglot = new Polyglot({phrases: <%= raw @phrases.to_json %>});
</script>

And now you can utilize i.e. polyglot.t("home.login") in your JavaScript application
or Handlebars templates.

Interpolation

Polyglot.t() also provides interpolation. Pass an object with key-value pairs of
interpolation arguments as the second parameter.

polyglot.extend({
  "hello_name": "Hola, %{name}."
});

polyglot.t("hello_name", {name: "DeNiro"});
=> "Hola, DeNiro."

Polyglot also supports nested phrase objects.

polyglot.extend({
  "nav": {
    "hello": "Hello",
    "hello_name": "Hello, %{name}",
    "sidebar": {
      "welcome": "Welcome"
    }
  }
});

polyglot.t("nav.sidebar.welcome");
=> "Welcome"

The substitution variable syntax is customizable.

var polyglot = new Polyglot({
  phrases: {
    "hello_name": "Hola {{name}}"
  },
  interpolation: {prefix: '{{', suffix: '}}'}
});

polyglot.t("hello_name", {name: "DeNiro"});
=> "Hola, DeNiro."

Pluralization

For pluralization to work properly, you need to tell Polyglot what the current locale is. You can use polyglot.locale("fr") to set the locale to, for example, French. This method is also a getter:

polyglot.locale()
=> "fr"

You can also pass this in during instantiation.

var polyglot = new Polyglot({locale: "fr"});

Currently, the only thing that Polyglot uses this locale setting for is pluralization.

Polyglot provides a very basic pattern for providing pluralization based on a single string that contains all plural forms for a given phrase. Because various languages have different nominal forms for zero, one, and multiple, and because the noun can be before or after the count, we have to be overly explicit about the possible phrases.

To get a pluralized phrase, still use polyglot.t() but use a specially-formatted phrase string that separates the plural forms by the delimiter `

主要指标

概览
名称与所有者airbnb/polyglot.js
主编程语言JavaScript
编程语言Makefile (语言数: 2)
平台Linux, Mac, Windows
许可证BSD 2-Clause "Simplified" License
所有者活动
创建于2012-09-27 19:10:37
推送于2024-07-11 22:51:44
最后一次提交2024-07-11 05:16:38
发布数26
最新版本名称v2.6.0 (发布于 2024-07-11 15:24:03)
第一版名称v0.1.0 (发布于 )
用户参与
星数3.7k
关注者数185
派生数211
提交数255
已启用问题?
问题数75
打开的问题数9
拉请求数63
打开的拉请求数8
关闭的拉请求数40
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?