Package one.tranic.t.i18n
Class BaseLoader
java.lang.Object
one.tranic.t.i18n.BaseLoader
- Direct Known Subclasses:
BungeeLoader,KyoriLoader
A utility class for loading and managing internationalized translations through a language map.
Provides methods for setting the locale, resetting the state, formatting strings, and converting
translation keys into various component types for localized output.
You need to manually install the dependent library.
- See Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionBaseLoader(@NotNull File file, @NotNull I18n adaptar) BaseLoader(@NotNull Class<?> clazz, @NotNull String namespace, @NotNull Locale locale, @NotNull I18n adaptar) BaseLoader(@NotNull Class<?> clazz, @NotNull String namespace, @NotNull I18n adaptar) BaseLoader(@NotNull String namespace, @NotNull I18n adaptar) BaseLoader(@NotNull Path path, @NotNull I18n adaptar) BaseLoader(@Nullable File file, @Nullable Path path, @Nullable Class<?> clazz, @Nullable String namespace, @Nullable Locale locale, @NotNull I18n adaptar) -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V> Map<K, V> @Nullable LocaleRetrieves the current language setting as a Locale object.Return a copy of the language mapinthashCode()voidreset()Resets the current state of the object to its default configuration using the predefined locale.voidResets the current state of the language map and assigns a new locale for this instance.voidsetLanguage(@NotNull Locale locale) Sets the language for localized operations.@NotNull StringTranslates the provided key into a localized string using the predefined language map.@NotNull StringRetrieves a formatted translation for the specified key using the provided arguments.@NotNull StringReplaces placeholders in the localized string corresponding to the provided key with the provided arguments.@NotNull StringtoBrace(@NotNull String key, @NotNull BaseLoader.SimpleComponent... args) Replaces placeholders in a text template with corresponding values from the provided components.toString()voidupdate()Updates the internal language map with the default behavior.voidupdate(@NotNull InputStream customInputStream) Updates the current language map using the data from the provided input stream.voidUpdates the internal language map based on the provided locale.
-
Constructor Details
-
BaseLoader
-
BaseLoader
public BaseLoader(@NotNull @NotNull String namespace, @NotNull @NotNull I18n adaptar) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
BaseLoader
public BaseLoader(@NotNull @NotNull Path path, @NotNull @NotNull I18n adaptar) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
BaseLoader
public BaseLoader(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String namespace, @NotNull @NotNull I18n adaptar) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
BaseLoader
public BaseLoader(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String namespace, @NotNull @NotNull Locale locale, @NotNull @NotNull I18n adaptar) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
BaseLoader
public BaseLoader(@Nullable @Nullable File file, @Nullable @Nullable Path path, @Nullable @Nullable Class<?> clazz, @Nullable @Nullable String namespace, @Nullable @Nullable Locale locale, @NotNull @NotNull I18n adaptar) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
-
Method Details
-
createMap
-
toString
-
hashCode
public int hashCode() -
reset
Resets the current state of the language map and assigns a new locale for this instance.- Parameters:
locale- the Locale to be set; it can be null to indicate no specific locale
-
reset
public void reset()Resets the current state of the object to its default configuration using the predefined locale. -
update
Updates the internal language map with the default behavior.- Throws:
IOException- if an I/O error occurs during loading of the localized strings.
-
update
Updates the internal language map based on the provided locale.- Parameters:
locale- the locale to be used for loading the language map. Must not be null.- Throws:
IOException- if no valid configuration is available for loading the language map, or if an I/O error occurs during the loading process.
-
update
Updates the current language map using the data from the provided input stream.- Parameters:
customInputStream- the input stream containing the new language data; must not be null- Throws:
IOException- if an I/O error occurs during the loading of data from the input stream
-
getLanguageMap
Return a copy of the language map- Returns:
- a non-null copy of the language map
-
getLanguage
Retrieves the current language setting as a Locale object.- Returns:
- the Locale object representing the current language setting, or null if no language is set.
-
setLanguage
Sets the language for localized operations.- Parameters:
locale- theLocaleobject representing the desired language and region settings; must not be null
-
to
Translates the provided key into a localized string using the predefined language map.If the key is not found in the language map, the key itself is returned as the fallback value.
- Parameters:
key- the translation key used to fetch the corresponding localized string- Returns:
- the localized string corresponding to the provided key, or the key itself if no translation exists
-
to
Retrieves a formatted translation for the specified key using the provided arguments.The format follows printf-style format strings, for example:
"Hello %s" with arg "world" produces "Hello world" "%d items" with arg 5 produces "5 items" "Rate: %.2f" with arg 0.123 produces "Rate: 0.12"
- Parameters:
key- the translation key used to fetch the corresponding localized stringargs- the arguments to format the localized string- Returns:
- the formatted translated string
-
toBrace
@NotNull public @NotNull String toBrace(@NotNull @NotNull String key, @NotNull @NotNull Object... args) Replaces placeholders in the localized string corresponding to the provided key with the provided arguments.For example:
"{} items in {}" with args ["5", "cart"] produces "5 items in cart" "Hello {}" with arg "world" produces "Hello world" "Rate: {}" with arg "0.12" produces "Rate: 0.12"- Parameters:
key- the translation key used to fetch the corresponding localized stringargs- the arguments to replace the "{}" placeholders within the localized string- Returns:
- the localized and formatted string with placeholders replaced by the arguments, or the key itself if no translation exists
-
toBrace
@NotNull public @NotNull String toBrace(@NotNull @NotNull String key, @NotNull @NotNull BaseLoader.SimpleComponent... args) Replaces placeholders in a text template with corresponding values from the provided components.For example:
"{b1} items in {b2}" with args {"b1": "5", "b2", "cart"} produces "5 items in cart" "Hello {b1}" with arg {"b1", "world"} produces "Hello world" "Rate: {b1}" with arg {"b1", 0.12} produces "Rate: 0.12"- Parameters:
key- the text template containing placeholders in the form of "{keyword}"args- an array ofBaseLoader.SimpleComponentobjects that provide the values for placeholders- Returns:
- the updated text with placeholders replaced by their corresponding values; returns the original text if no placeholders match or if no components are provided
-