API Reference

Instance

i18n4v provides global instance:

// i18n is a globals instance's translate() method
const i18n = require('i18n4v');

// i18n.translator is a global instance of Translator
const translator = i18n.translator;

Resulting object of require() is a just translate() method. It has property that points Translator instance.

You can access any features from i18n.translator. i18n is for translating.

i18n4v supports multiple Translator instances for server usage:

// es is a new instance's translate() method
var es = i18n.create({
    values: {
        cat: "gato"
    }
});

// es.translator is a Translator instance
const translator = es.translator;

Translator class

class Translator()

This is a core component of i18n4v.

translate(source:string[, count:number[, formatParams:object[, context:object]]]) : string()

Returns translated text from source string/key.

add(resource : object)

Registers translation resource.

resetData()

Resets translation resource that is set by add().

setContext(contextKey : string, contextValue : string)

Sets default context. It is used by translate() if context parameter is omitted.

clearContext(contextKey : string)

Clears default context value specified by contextKey that is set by setContext().

resetContext()

Clears all default context that is set by setContext().

reset()

It is combined method of resetData() and resetContext().

applyToHTML()

Searches data-i18n attributes and replace text on current web page.

i18n() funciton

i18n(source:string[, count:number[, formatParams:object[, context:object]]]) : string()

This is a translate() method of default instance of Translator().

translator

It points a parent instance of translate() method.

create(resource : object) : Translator()

It creates a new instance of Translator().

setLanguage(language : string)

Stores preferred language of a user. It is available only on browsers.

Changed in version 0.2.0: This function is moved from Translator.prototype to i18n. Change your code like this

// old code
i18n.translator.setLanguage('en');

// new code
i18n.setLanguage('en');
selectLanguage(languageList : string[], callback : function(err:error, lang:string)) : Promise()

Returns preferred language from langaugeList from browsers’ and node.js’s environment.

If the JavaScript environment supports Promise and callback is ommitted, it returns Promise.

Note

There is no feature selecting preferred language from user’s request for web applications on node.js now.

Changed in version 0.2.0: This function is moved from Translator.prototype to i18n. Change your code like this

// old code
i18n.translator.selectLanguage(['en', 'es', 'fr', 'ge'], callback);

// new code
i18n.selectLanguage(['en', 'es', 'fr', 'ge'], callback);