@goldenthumb/simple-i18n
TypeScript icon, indicating that this package has built-in type declarations

0.2.3 • Public • Published

simple-i18n

npm


Install

npm install @goldenthumb/simple-i18n
//es6
import SimpleI18n from '@goldenthumb/simple-i18n';

//commonjs
const SimpleI18n = require('@goldenthumb/simple-i18n');


Example

const i18n = new SimpleI18n({
    defaultLocale: ['en'],
    locale: 'en',
    messages: {
        ['ko']: {
            hello: '안녕하세요.'
        },
        ['en']: {
            hello: 'hello.',
            bye: 'goodbye.'
        },
        ['ja']: {
            hello: 'こんにちは。',
            bye: 'さようなら。'
        }
    }
});

i18n.message('hello')
> hello.

i18n.switchLang('ja');
i18n.message('bye');
> さようなら。

i18n.switchLang('ko');
i18n.message('hello');
i18n.message('bye')
> 안녕하세요.
> goodbye.
const i18n = new SimpleI18n({
    fallbackLocales: ['ko'],
    locale: 'ko',
    messages: {
        ['ko']: {
            product: '이것은 {{money}}원 이고, {{size}}개 들어있습니다.'
        }
    }
});

i18n.message('product', { money: 500, size: 2 })
> '이것은 500원 이고, 2개 들어있습니다.'


Using by with @goldenthumb/simple-i18n-csv-to-json

@goldenthumb/simple-i18n-csv-to-json

sample csv

example excel
,ko,en,ja,zh_CN,zh_TW
yes,예,Yes,はい,是的,是的
no,아니오,No,いいえ,没有,沒有

npm install @goldenthumb/simple-i18n @goldenthumb/simple-i18n-csv-to-json
import path from 'path';

const SimpleI18n = require('@goldenthumb/simple-i18n');
const toJson = require('@goldenthumb/simple-i18n-csv-to-json');

(async () => {
    const i18n = new SimpleI18n({
        fallbackLocales: ['en'],
        locale: 'en',
        messages: await toJson(path.resolve(__dirname, './sample.csv'))
    });

    i18n.message('yes')
    > Yes.

    i18n.switchLang('ja');
    i18n.message('no');
    > いいえ
})();

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i @goldenthumb/simple-i18n

Weekly Downloads

11

Version

0.2.3

License

MIT

Unpacked Size

8.88 kB

Total Files

6

Last publish

Collaborators

  • goldenthumb