๐ซ Internationalization API
As our community gets bigger and bigger, language becomes a barrier for users and players from different countries to
communicate. To change this, we made I18nAPI and encourage plugin creators use it.
Load translation data
i18n.load(path, defaultLocaleName, defaultLangData)
- Params:
- path:
String
The file1/directory2 where the translation data is located - defaultLocaleName:
String
default locale name, in the form ofzh_CNoren(this parameter will be used if the target language is not provided for the translation ofi18n.trori18n.get)
If an empty string is passed in, the default follows the system language - defaultLangData:
Object
This parameter will be used to complete or create translation files in the form ofJavaScript{ localeName: { "src": "translation" }, en_CN: { "a.b.c.d.114514": "1919810", "src": "source" } }
- path:
- Throw:
- Invalid parameter
JSON
{
"localeName": {
"src": "translation"
},
"zh_CN": {
"a.b.c.d.114514": "1919810",
"src": "ๆบ"
}
}Text Only
โ LOCALE_name.json
[Dir] โผ en.json
โ zh_CN.jsonEach file inside should be similar to:
JSON
{
"a.c.b.d": "translation",
"use": {
"nested": {
"src": "translation"
},
"114514": {
"1919810": "heng heng aaaaaaaaaaaaaaaaaaaaaaaaa",
"a": "ๆญคๅค็IDไธบuse.nested.114514.a",
"b": "The ID of this will be use.nested.114514.b"
}
}
}Get the translation of the text in the specified language
i18n.get(key[,localeName])
- Params:
- key:
String
Text or ID - localeName:
String(Optional)
Target language, default is thedefaultLocaleNamepassed in wheni18n.load.
- key:
- Return value:
Stringtranslation (if no translation is found after multiple fallbacks, thenkeyis returned) - Throw:
- Invalid parameter
Translate and format the text using the specified language
i18n.trl(localeName, key, ...)
- Parameters:
- localeName:
String
Target language - key:
String
Text or ID - ... :
Any type
Format arguments
- localeName:
- Return value:
Stringtranslated and formatted text - Note: Formatting should follow syntax
- Throw:
- Invalid parameter
Translate and format the text using the default language
i18n.tr(key, ...)
- Parameters:
- key:
String
Text or ID - ... :
Any type
Format arguments
- key:
- Return value:
Stringtranslated and formatted text - Note: Formatting should follow syntax
- Throw:
- Invalid parameter
Full sample
JavaScript
i18n.load("plugins/feature-llse-i18n-test/language.json", "" /* "zh_CN" */, {
"zh_CN": {
"{1}, {0}, named_arg(float.2): {named_arg:.2f}": "ๆต่ฏ๏ผ {1}, {0}, named_arg(float.2): {named_arg:.2f}",
"{} {} {a} {b}": "ๆต่ฏ2๏ผ {} {} {a} {b}",
"welcome": "Hi, {}! Welcome to the server!"
},
"en": {
"welcome": "Hi, {}! Welcome to the SERVER!"
}
});
LLSE_Player.prototype._sendText = LLSE_Player.prototype.sendText;
LLSE_Player.prototype.sendText = function () {
let args = Array.from(arguments);
let text = args.shift();
this._sendText(i18n.trl(this.langCode, text, ...args));
}
logger.info(tr("{1}, {0}, named_arg(float.2): {named_arg:.2f}", "string0", 1, {
named_arg: 114.51419 // Named arguments should be object
}));
// Out: test: 1, string0, named_arg(float.2): 114.51
logger.info(tr("{} {} {a} {b}", "arg0", "arg1", {
a: "114514",
b: "1919810"
}));
mc.listen("onJoin", (pl) => {
pl.sendText("welcome", pl.realName);
});