Class I18n
java.lang.Object
eu.cloudnetservice.common.language.I18n
The main entry point for localization made in the CloudNet system. Language files can be registered in multiple ways
to this registry. Multiple language files for the same language will be combined to one entry and can be translated.
If multiple language files with the same language key are registered, the first registered language file will be used
to translate the requested key.
Unregistering language files can be done via the class loader which must be given for message loading.
- Since:
- 4.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final recordA registered entry in this registry mapping the loader source and all messages of it. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final AtomicReference<String>private static final Loggerprivate static final Patternprivate static final com.google.common.collect.SetMultimap<String,I18n.Entry> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddLanguageFile(@NonNull String lang, @NonNull InputStream stream, @NonNull ClassLoader source) Registers the language properties file which must be loadable from the given stream to the given language and loader source.static voidaddLanguageFile(@NonNull String lang, @NonNull Path file, @NonNull ClassLoader source) Registers the language properties file at the given path to the given language and loader source.static voidaddLanguageFile(@NonNull String lang, @NonNull Properties entries, @NonNull ClassLoader source) Registers the language properties file to the given language and loader source.static @NonNull Collection<String>Gets all the names of the known languages to this translation manager.language()Get the current language to which each message will be translated.static voidSets the current message to which all messages should get translated.static voidloadFromLangPath(@NonNull Class<?> clazzSource) Loads all language files which are located in the jar at given class source and registers them to the loader of the given class.static StringTries to translate the given message key using all currently registered language files for the language this registry currently uses.static voidUnregisters all language files which were registered by providing the given class loader.
-
Field Details
-
MESSAGE_FORMAT
-
LOGGER
-
REGISTERED_ENTRIES
-
CURRENT_LANGUAGE
-
-
Constructor Details
-
I18n
private I18n()
-
-
Method Details
-
loadFromLangPath
Loads all language files which are located in the jar at given class source and registers them to the loader of the given class. All language files in the jar must be located in thelang/directory and their extension must be properties. Subdirectories are ignored by this method.- Parameters:
clazzSource- the source which tries to register all language files.- Throws:
NullPointerException- if the given class source is null.
-
addLanguageFile
public static void addLanguageFile(@NonNull @NonNull String lang, @NonNull @NonNull Path file, @NonNull @NonNull ClassLoader source) Registers the language properties file at the given path to the given language and loader source.- Parameters:
lang- the language to register the file to.file- the location of the language file to load.source- the class loader to which the requester belongs.- Throws:
NullPointerException- if either the given language, file or loader is null.
-
addLanguageFile
public static void addLanguageFile(@NonNull @NonNull String lang, @NonNull @NonNull InputStream stream, @NonNull @NonNull ClassLoader source) Registers the language properties file which must be loadable from the given stream to the given language and loader source. This method uses utf-8 to decode the stream.- Parameters:
lang- the language to register the file to.stream- the stream from which the properties should get loaded.source- the class loader to which the requester belongs.- Throws:
NullPointerException- if either the given language, stream or loader is null.
-
addLanguageFile
public static void addLanguageFile(@NonNull @NonNull String lang, @NonNull @NonNull Properties entries, @NonNull @NonNull ClassLoader source) Registers the language properties file to the given language and loader source.- Parameters:
lang- the language to register the file to.entries- the entries of the language file to register.source- the class loader to which the requester belongs.- Throws:
NullPointerException- if either the given language, entries or loader is null.
-
unregisterLanguageFiles
Unregisters all language files which were registered by providing the given class loader.- Parameters:
loader- the loader to unregister the language files of.- Throws:
NullPointerException- if the given loader is null.
-
trans
Tries to translate the given message key using all currently registered language files for the language this registry currently uses. This method uses the first language entry which can translate the given key, ignoring all duplicate keys.This method will never return null. However, it does return a string which either indicates that no language files are registered for the current language, or that no registered entry is able to translate the given key.
- Parameters:
messageKey- the key of the message to translate.args- the arguments for the translation.- Throws:
NullPointerException- if either the given key or argument array is null.
-
language
Get the current language to which each message will be translated.- Returns:
- the current message of this manager.
-
knownLanguages
Gets all the names of the known languages to this translation manager.- Returns:
- the names of all known languages.
-
language
Sets the current message to which all messages should get translated. There is no check made if any message is registered for the given language.This method doesn't change to the given language silently if the language is not associated with a translation file.
- Parameters:
language- the language this manager should use.- Throws:
NullPointerException- if the given language is null.
-