locale-translate

1.0.2 • Public • Published

Locale Translate

Locale Translate is an NPM module made to make managing locales as easy as possible. With LT, you can define new locales, and easily use them afterwards.

Installation

npm i translate-it

Usage

First, you need to create a JSON file. Feel free to name it whatever you like. We recommend locales.json. Then use this format, changing what you need:

{
  "en-US": {
    "started": "Your project has been initialized.",
    "colorChange": "Your project's color has changed."
  },
  "en-UK": {
    "started": "Your project has been initialised.",
    "colorChange": "Your project's colour has changed."
  }
}

Then, in your JS file, use this,

let translator = require('locale-translate')
 
translator.importLocales(require('locales.json'), 'memory') // stores the locales in memory
 
let translated = translator.translate('started', 'en-US')
 
console.log(translated) // Expected result 'Your project has been initialized.'

Parameters

Using {} in a translation string allows use of parameters. This is so you can easily pass data into your translated strings. For example,

{
  "en-US": {
    "started": "Your project, {}, has been initialized.",
    "colorChange": "Your project's color has changed to {}."
  },
  "en-UK": {
    "started": "Your project, {}, has been initialised.",
    "colorChange": "Your project's colour has changed to {}."
  }
}

Use the same usage, but use .translateParams()

Package Sidebar

Install

npm i locale-translate

Weekly Downloads

1

Version

1.0.2

License

GPL-3.0

Unpacked Size

4.2 kB

Total Files

3

Last publish

Collaborators

  • danielnewell