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

The active language comes from the UltiTools config

The language actually used is the language key of the UltiTools main plugin config, which getLanguageCode() reads, while @I18n and an overridden supported() are declarative metadata with no runtime reader. Name each language file lang/<code>.json so that one of them matches that key: when no file matches, the module falls back to an empty dictionary and i18n("some.key") silently returns the key you passed in. Giving supported() a runtime role, or removing it, is tracked in issue #315.

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.

Contributors

The avatar of contributor named as Ling Bao Ling Bao

Changelog

Released under the MIT License.