-
Notifications
You must be signed in to change notification settings - Fork 12
LanguageRepository.java
LanguageRepository
is the core localization component for all NyaaCat plugins.
It's extensively used so you can check any of our plugin for usages.
public class I18n extends LanguageRepository { // You need to extend the LanguageRepository
public static I18n instance = null;
private String lang = null;
private final NyaaUtils plugin;
@Override
protected JavaPlugin getPlugin() { // will be called by super.load()
return plugin;
}
@Override
protected String getLanguage() { // will be called by super.load()
return lang; // used to determine the preferred language
}
public I18n(NyaaUtils plugin, String lang) {
instance = this;
this.plugin = plugin;
this.lang = lang;
load(); // call load() to load the language items
}
public static String format(@LangKey String key, Object... args) { // Make getFormatted() static so can be used with ease
return instance.getFormatted(key, args);
}
}
A language item is the minimal single element can be translated. I can be a word, a sentence or a paragraph. Language items are stored in YAML files. For example:
root:
item1: This is a sentence
item2: This is another sentence
This YAML file consists of two language items:
-
root.item1
:This is a sentence
-
root.item2
:This is another sentence
Language items are also referred to as language key-value pairs. In the example above,
root.item1
and root.item2
are the language keys.
This is a sentence
and This is another sentence
are their values.
Language items have their keys start with internal.
are internal language items.
These language items are usually used for internal error message or common prompt shown by
other components of NyaaCore (e.g. CommandReceiver). The internal items are mainly provided bundled together with NyaaCore and get shared across all plugins using NyaaCore.
For language items other than internal items, they are normal language items. And is usually provided together with plugins depends on NyaaCore. They provide plugin-specific messages. Specially, plugins can also provide internal section items, which will overwrite the message only for that plugin.
A language item is selected based on this priority:
- Prefer language items of selected language rather than fallback language (i.e.
en_US
) - Prefer language items provided by specific plugin rather than those provided by NyaaCore.
- Prefer language items in local files rather than in bundled files in jar.
For example, when preferred language is set to zh_CN
, an English message may be shown if Chinese language item is found in none of these places:
- plugin's config folder
- plugin jar file
- NyaaCore config folder
- NyaaCore jar file
If the language item cannot be found even for the fallback language, some internal structures may be printed and a warning will be printed in the console.
- Bundled:
<plugin_name>.jar/lang/<lang_code>.yml
- Local:
plugins/<plugin_name>/lang/<lang_code>.yml
Note that a local language file will be automatically created and used since then. If you want to ignore all local language files, you can pass argument
-Dnyaautils.i18n.refreshLangFiles=true
The sole purpose of this interface is to decouple CommandReceiver with LanguageRepository So plugin developers can use CommandReceiver with their own L10n system.