easy-localization

javascript react localization

Usage no npm install needed!

<script type="module">
  import easyLocalization from 'https://cdn.skypack.dev/easy-localization';
</script>

README

easy-localization

javascript localization

Usage

Setup

First you need to set a default language.

// ES6 module syntax
import EasyLocalization from 'easy-localization';

const loc = new EasyLocalization();
/// Set default language
loc.setDefaultLanguage("fr");

After that you can set a current language.

/// Set current language
loc.setCurrentLanguage("sr");

const testObj = {
  sr: {
    add: "Dodaj",
    remove: "Izbriši",
    add_new_item: "Dodaj novu stavku"
  },
  fr: {
    add: "Ajouter",
    remove: "Retirer",
    add_new_item: "Ajoute un Nouvel Objet"
  }
};
/// localise
let strings = loc.localize(testObj)
/*
expected return  {
    add: "dodaj",
    remove: "izbriši",
    add_new_item: "Dodaj novu stavku"
  }
*/

Using properites of default language as one more language

You can use a properties of default language object as one more language. You can name it whatever you like with setObjectPropertiesLanguage() method and set it as current language. Property will be splitted by underscore and first word will be first letter uppercased.


loc.setObjectPropertiesLanguage("en");
/// Set current language
loc.setCurrentLanguage("en");
/// localise and set usage of object prop language as true
let strings = loc.localize(testObj, { propsLanguage: true })
/*
expected return  {
    add: "Add",
    remove: "Remove",
    add_new_item: "Add new item"
  }
*/

You can use functions to set strings in language object but in that case Object Properties Language will not work properly.