Class Translator
Language.
On construction, the translator attempts to load the appropriate .lang
file from the plugin's data folder. If the file is not present it will be downloaded
from the ValorlessUtils GitHub repository. When no server version is available (e.g.
during unit-tests or early startup) a bundled fallback file is used instead.
All translation keys follow the standard Minecraft namespaced-key format
(e.g. "block.minecraft.stone").
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionTranslator(String key) Deprecated, for removal: This API element is subject to removal in a future version.Translator(Language language) Creates a newTranslatorand immediately loads translations for the givenLanguage. -
Method Summary
Modifier and TypeMethodDescriptiondownloadLanguage(Language language) Downloads the.langfile for the specifiedLanguagefrom the ValorlessUtils GitHub repository and saves it to the plugin's data folder.booleanfileExists(Language language) Checks whether the.langfile for the givenLanguagealready exists in the plugin's data folder for the current server version.booleanFileExists(String key) Deprecated, for removal: This API element is subject to removal in a future version.UsefileExists(Language)instead.Returns theLanguagethat is currently active for this translator.getLanguageFileContent(Language language) Reads and returns the raw JSON content of the.langfile for the givenLanguagefrom the plugin's data folder.Deprecated, for removal: This API element is subject to removal in a future version.UsegetLanguage()and callLanguage.getCode()on the result instead.Reads the bundledfallback.langresource from the plugin's data folder, copying it from the plugin JAR if it has not yet been extracted.Deprecated, for removal: This API element is subject to removal in a future version.UseloadFallbackLanguage()instead.voidsetLanguage(Language language) Switches the active language tolanguageand reloads the translation map.voidSetLanguage(String key) Deprecated, for removal: This API element is subject to removal in a future version.UsesetLanguage(Language)instead.Returns the localised string for the given Minecraft translation key.Deprecated, for removal: This API element is subject to removal in a future version.Usetranslate(String)instead.
-
Constructor Details
-
Translator
Creates a newTranslatorand immediately loads translations for the givenLanguage.- Parameters:
language- the language whose translation file should be loaded; must not benull.
-
Translator
@Deprecated(since="Replaced by Translator(Language), will still work until removed.", forRemoval=true) public Translator(String key) Deprecated, for removal: This API element is subject to removal in a future version.UseTranslator(Language)instead. This constructor will be removed in a future release.Creates a newTranslatorand immediately loads translations for the language identified bykey.- Parameters:
key- the language code (e.g."en_us") used to look up the correspondingLanguageconstant.
-
-
Method Details
-
translate
Returns the localised string for the given Minecraft translation key.If the language map has not been loaded (e.g. the language file could not be found or parsed), the original
translationKeyis returned unchanged so that callers always receive a non-nullresult.- Parameters:
translationKey- the Minecraft translation key to look up (e.g."item.minecraft.diamond_sword").- Returns:
- the localised display name, or
translationKeyitself if no mapping is available.
-
Translate
@Deprecated(since="Replaced by translate()", forRemoval=true) public String Translate(String translationKey) Deprecated, for removal: This API element is subject to removal in a future version.Usetranslate(String)instead. This method will be removed in a future release.Returns the localised string for the given Minecraft translation key.- Parameters:
translationKey- the Minecraft translation key to look up.- Returns:
- the localised display name, or
translationKeyif no mapping is available.
-
getLanguage
Returns theLanguagethat is currently active for this translator.- Returns:
- the active
Language; nevernull.
-
GetLanguageKey
Deprecated, for removal: This API element is subject to removal in a future version.UsegetLanguage()and callLanguage.getCode()on the result instead. This method will be removed in a future release.Returns the language code for the currently active language (e.g."en_us").- Returns:
- the language code string.
-
loadFallbackLanguage
Reads the bundledfallback.langresource from the plugin's data folder, copying it from the plugin JAR if it has not yet been extracted.This method is used when the server version cannot be determined (e.g. during early startup or in test environments).
- Returns:
- the raw JSON content of the fallback language file, or
nullif an I/O error occurs.
-
LoadFallbackLanguage
@Deprecated(since="Replaced by loadFallbackLanguage(String)", forRemoval=true) public String LoadFallbackLanguage()Deprecated, for removal: This API element is subject to removal in a future version.UseloadFallbackLanguage()instead. This method will be removed in a future release.Reads the bundledfallback.langresource from the plugin's data folder.- Returns:
- the raw JSON content of the fallback language file, or
nullif an I/O error occurs.
-
fileExists
Checks whether the.langfile for the givenLanguagealready exists in the plugin's data folder for the current server version.The expected path is:
<dataFolder>/languages/<serverVersion>/<languageCode>.lang.- Parameters:
language- the language whose file presence should be checked; must not benull.- Returns:
trueif the file exists and can be found;falseotherwise.
-
FileExists
@Deprecated(since="Replaced by fileExists()", forRemoval=true) public boolean FileExists(String key) Deprecated, for removal: This API element is subject to removal in a future version.UsefileExists(Language)instead. This method will be removed in a future release.Checks whether the.langfile for the language identified bykeyexists in the plugin's data folder for the current server version.- Parameters:
key- the language code (e.g."en_us").- Returns:
trueif the file exists;falseotherwise.
-
setLanguage
Switches the active language tolanguageand reloads the translation map.After this call, subsequent
translate(String)invocations will return strings in the new language.- Parameters:
language- the new language to use; must not benull.
-
SetLanguage
Deprecated, for removal: This API element is subject to removal in a future version.UsesetLanguage(Language)instead. This method will be removed in a future release.Switches the active language to the one identified bykeyand reloads the translation map.- Parameters:
key- the language code (e.g."en_us").
-
getLanguageFileContent
Reads and returns the raw JSON content of the.langfile for the givenLanguagefrom the plugin's data folder.The file is expected at:
<dataFolder>/languages/<serverVersion>/<languageCode>.lang.- Parameters:
language- the language whose file should be read; must not benull.- Returns:
- the file content as a
String, ornullif an I/O error occurs or the file does not exist.
-
downloadLanguage
Downloads the.langfile for the specifiedLanguagefrom the ValorlessUtils GitHub repository and saves it to the plugin's data folder.The method tries the following branches in order until a successful download is achieved:
maindev
The remote URL pattern is:
https://raw.githubusercontent.com/Valorless/ValorlessUtils/refs/heads/<branch>/languages/<serverVersion>/<languageCode>.lang.If all download attempts fail,
loadFallbackLanguage()is returned as a last resort.- Parameters:
language- the language to download; must not benull.- Returns:
- the raw JSON content of the downloaded language file, the content of the
fallback language file if all downloads fail, or
nullif even the fallback cannot be read.
-
Translator(Language)instead.