项目作者: syu93

项目描述 :
A simple really small i18n library with intuitive API.
高级语言: JavaScript
项目地址: git://github.com/syu93/translit.git
创建时间: 2020-04-25T16:36:06Z
项目社区:https://github.com/syu93/translit

开源协议:

下载


Translit

A simple really small i18n library with intuitive API.


size
version

Features

  • Dependency free
  • Really small
  • Compatible with Web Components and LitElement

Install

  1. $ npm i @syu93/translit

Usage

  1. import Translit from '@syu93/translit';
  2. const i18n = new Translit({
  3. translation: {
  4. en: {
  5. hello: {
  6. world: 'Hello world'
  7. },
  8. itemInList: (count) => `This list contains ${count} item${count > 1 ? 's' : ''}.`
  9. }
  10. }
  11. });
  12. console.log(i18n.t('hello.world'))
  13. // => Hello world

Locales

By default Translit define the locale as English en .

You can change the locale in the constructor

  1. import Translit from '@syu93/translit';
  2. const i18n = new Translit({
  3. translation: {
  4. // ...
  5. },
  6. locale: 'en'
  7. });

Or you can dynamically change the locale using the setLocale method

  1. i18n.setLocale('fr');

Or you can even set the locale as you use it

  1. i18n.t('hello.wold', null, 'en');

You can as well load another locale with the addLocale method

  1. i18n.addLocale({
  2. fr: {
  3. hello : {
  4. world: 'Bonjour à tous'
  5. }
  6. }
  7. });

Simple translation

Define a JSON of translation and use the path as a string to access the translation.

  1. const i18n = new Translit({
  2. translation: {
  3. en: {
  4. hello: {
  5. world: 'Hello world'
  6. }
  7. }
  8. }
  9. });
  10. console.log(i18n.t('hello.world'));
  11. // => Hello world

Pluralisation

If you need a more complex translation, with pluralisation for example, you can simple define a method that takes a param and return a string.

  1. const i18n = new Translit({
  2. translation: {
  3. en: {
  4. itemInList: (count) => `This list contains ${count} item${count > 1 ? 's' : ''}.`
  5. }
  6. }
  7. });
  8. console.log(i18n.t('itemInList', 1));
  9. // => This list contains 1 item
  10. console.log(i18n.t('itemInList', 2));
  11. // => This list contains 2 items

LitElement

To use Translit inside a LitElement, just call the this.t method inside your template

  1. render() {
  2. return html`
  3. <section>
  4. <p>${this.t('text.hi')}</p>
  5. </section>
  6. `;
  7. }

API

Translit( config : Object )

  • Translation : An object containing the translation.
  • Locale : The current locale used for translation.

setLocale( locale : String )

Locale : The language string.

Dynamically change the locale translation.

addLocale( translation : Object )

Translation : A translation object.

Dynamically add a new translation.

t( translation : String, data : Any, locale : String )

Translation : A string representing the path for the translation.
Data : The data to be passed the translate function.
Locale : The locale of the translation (override the default locale).

Translate a given string from the translation object.