0.0.4-beta.2 • Published 6 years ago

modular-intl v0.0.4-beta.2

Weekly downloads
5
License
ISC
Repository
github
Last release
6 years ago

modular-intl

Modular plan to manage international locale files. 国际化多语言文件的模块化管理方案。

Build Status Coverage Status npm npm

Install

Install By:

npm i --save modular-intl

Then import it by:

import { Application } from 'modular-intl';

Usage

You can create Application by the API provided by modular-intl.

import { Application } from 'modular-intl';

// create app with current locale language
const app = new Application('name', 'en_US');

// change locale
app.setDefaultLocale('zh_CN');

// create your modules
const m1 = app.createModule('m1');

// add locale language
m1.addLocale('zh_CN', {
  "excel": "电子表格",
});
m1.addLocale('en_US', {
  "excel": "Excel",
});

const m2 = app.createModule('m2');
// ...


// Get i18n text
const text = m1.get('excel');

Customize intl

You can customize your own intl function, e.g.

import { Application } from 'modular-intl';
import IntlMessageFormat from 'intl-messageformat';

// custom intl with intl-messageformat
const intl = (locales, currentLocale, ns, key, ...opts) =>
  new IntlMessageFormat(locales[key], currentLocale).format(opts[0]);

const app = new Application('name', 'en_US', intl);

const m1 = app.createModule('m1');
// ...

// use new intl with format opt
const text = m1.get('excel', { count: 100 });

Then you can use the formatter of intl-messageformat.

See more demo code here.

License

ISC@hustcc.