49

? i18n`literally` - A simple way to introduce i18n to your JS

 5 years ago
source link: https://www.tuicool.com/articles/hit/eUvq22Q
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

i18n-literally

A simple way to introduce i18n to your JS.

Install

$ npm install i18n-literally

Usage

import i18n from 'i18n-literally';
import db from './i18n.db.json';

// Set the database
i18n.db = db;

// 1. Write in the default language
i18n`Hello ${'World'}!`
// => Hello World!

// 2. Add/update your translations for a language
"$ npx i18n-literally index.js es"

// 3. Get translations based on locale
i18n.locale = 'es';
i18n`Hello ${'World'}!`
// => Hola World!

Write your entire application default language, and support multiple versions of the language by simply changing the i18n.locale . To Add/update translations simply run the.

API

i18n`template`

Returns a string based on the locale (default "en").

i18n.locale

Type: string
Default: en

i18n.db

Type: object
Default: {}

CLI

The cli helps you to easily maintain your translations in a simple web app.

All translations are stored in a i18n.db.json file.

$ npx i18n-literally --help

  Usage:
    $ literally <entry> <locale> [db]

  Arguments:
    <entry>     The entry file of your app
    <locale>    Locale to add/update translations for
    [db]        Database file defaults to "./i18n.db.json"

  Options:
    --help      Show information
    --version   Show current version

  Example:
    $ literally ./index.js es

The cli web app to add/update translations . ayiyimf.png!web

Related

Inspired by this post from Andrea Giammarchi .

License

MIT © André Ruffert


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK