Posts simplel10n
Post
Cancel

simplel10n

A really simple l10n class

npm install @benline/simplel10n

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//You would have some keys and values
var language = {
  en: {
    HELLO: 'Hello',
  },
  fr: {
    HELLO: 'Bonjour',
  }
}

const currentLang = new l10n();

currentLang.getTranslationStr("HELLO")
currentLang.setLanguage('de');
currentLang.__translation // Contains strings 

Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { language, DEFAULT_LANGUAGE } from '../mockData';

export class l10n {
    _CURRENTLANGUAGE: any
    DEFAULT_LANGUAGE: any
    _translation: any
    translation: any
    constructor() {
        this.DEFAULT_LANGUAGE = 'en';
        this._translation = {}
    }

    /**
     * getTranslationStr
     */
    public getTranslationStr(key) {
        var fallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
        return this._translation[key] || fallback || language[DEFAULT_LANGUAGE][key] || key;
    }
    /**
     * setLanguage
     */
    public setLanguage(lang) {
        this._translation = lang;
        this._CURRENTLANGUAGE = '';

        if (!language[lang]) {
            this._translation = language[DEFAULT_LANGUAGE] || {};
            this._CURRENTLANGUAGE = lang;
        } else {
            this._translation = language[lang];
            this._CURRENTLANGUAGE = lang;
        }
    }
}

View Repo

Trending Tags

Trending Tags