Skip to content

Internationalization

UltiTools provides an easy-to-use API for internationalization, allowing you to easily add multilingual support to your plugin.

Create a language file

Create a lang folder in the resources folder. Put your plugin language files in it according to your needs.

json
{
  "test": "测试",
  "test2": "测试2"
}

Name the file zh.json, where zh is the language code.

Language codes can be found here.

Register language

UltiTools needs to know which languages your plugin supports, so you need to register them in the class that inherits UltiToolsPlugin.

A simple way is to add @I18n and add language codes in the class that inherits UltiToolsPlugin:

java
@I18n({"zh", "en"})

Sure, you can also override the supported() method and return a List<String> containing the language code:

java
@Override
public List<String> supported() {
    return Arrays.asList("zh", "en");
}

How to use

In the class that inherits UltiToolsPlugin, there is an i18n method for getting multilingual strings.

java
String test = i18n("test");

// Output:测试

If the string does not exist in the language file, the string itself will be returned.

java
String test3 = i18n("test3");

// Output:test3

TIP

In the case of only two languages, you can create only one language file, where the key of the key-value pair is the original text and the value is the translation.

Released under the MIT License.