Skip to content

🛫 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 of zh_CN or en (this parameter will be used if the target language is not provided for the translation of i18n.tr or i18n.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 of
    JavaScript
    1
    2
    3
    4
    5
    6
    7
    8
    9
    {
      localeName: {
        "src": "translation"
      },
      en_CN: {
        "a.b.c.d.114514": "1919810",
        "src": "source"
      }
    }
    
  • Throw:
  • Invalid parameter


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 the defaultLocaleName passed in when i18n.load.
  • Return value: String translation (if no translation is found after multiple fallbacks, then key is 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
    Format arguments
  • Return value: String translated 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
    Format arguments
  • Return value: String translated and formatted text
  • Note: Formatting should follow syntax
  • Throw:
  • Invalid parameter


Full sample

JavaScript
 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
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();
  pl._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);
});

  1. the file must be in JSON format and the content of the file should be similar to

    JSON
    1
    2
    3
    4
    5
    6
    7
    8
    9
    {
      "localeName": {
        "src": "translation"
      },
      "zh_CN": {
        "a.b.c.d.114514": "1919810",
        "src": "源"
      }
    }
    
     

  2. Please add the path separator (/ or \\\) at the end of the path.
    The contents of the directory should be similar to.

    Text Only
    1
    2
    3
          ┌ LOCALE_name.json
    [Dir] ┼ en.json
          └ zh_CN.json
    
    Each file inside should be similar to:
    JSON
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    {
      "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"
        }
      }
    }