Class I18nLoader

java.lang.Object
one.tranic.t.i18n.I18nLoader

public class I18nLoader extends Object
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
    Modifier and Type
    Class
    Description
    static final record 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    I18nLoader(@NotNull File file, @NotNull I18n adaptar)
     
    I18nLoader(@NotNull Class<?> clazz, @NotNull String namespace, @NotNull Locale locale, @NotNull I18n adaptar)
     
    I18nLoader(@NotNull Class<?> clazz, @NotNull String namespace, @NotNull I18n adaptar)
     
    I18nLoader(@NotNull String namespace, @NotNull I18n adaptar)
     
    I18nLoader(@NotNull Path path, @NotNull I18n adaptar)
     
    I18nLoader(@Nullable File file, @Nullable Path path, @Nullable Class<?> clazz, @Nullable String namespace, @Nullable Locale locale, @NotNull I18n adaptar)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <K, V> Map<K,V>
     
    @Nullable Locale
    Retrieves the current language setting as a Locale object.
    @NotNull Map<String,String>
    Return a copy of the language map
    int
     
    void
    Resets the current state of the object to its default configuration using the predefined locale.
    void
    reset(@Nullable Locale locale)
    Resets the current state of the language map and assigns a new locale for this instance.
    void
    setLanguage(@NotNull Locale locale)
    Sets the language for localized operations.
    @NotNull String
    to(@NotNull String key)
    Translates the provided key into a localized string using the predefined language map.
    @NotNull String
    to(@NotNull String key, @NotNull Object... args)
    Retrieves a formatted translation for the specified key using the provided arguments.
    @NotNull net.md_5.bungee.api.chat.BaseComponent[]
    toBaseComponent(@NotNull String key)
    Converts a localized string corresponding to the given key into an array of BaseComponent objects.
    @NotNull net.md_5.bungee.api.chat.BaseComponent[]
    toBaseComponent(@NotNull String key, @NotNull Object... args)
    Converts a translation key and its formatted arguments into an array of BaseComponent.
    @NotNull net.md_5.bungee.api.chat.BaseComponent[]
    toBaseComponentBrace(@NotNull String key, @NotNull Object... args)
    Converts a localized and formatted string with placeholders replaced by the provided arguments into an array of BaseComponent.
    @NotNull net.md_5.bungee.api.chat.BaseComponent[]
    toBaseComponentBrace(@NotNull String key, @NotNull I18nLoader.SimpleComponent... args)
    Converts the provided key and arguments into an array of BaseComponent objects using a legacy text format with braces.
    @NotNull String
    toBrace(@NotNull String key, @NotNull Object... args)
    Replaces placeholders in the localized string corresponding to the provided key with the provided arguments.
    @NotNull String
    toBrace(@NotNull String key, @NotNull I18nLoader.SimpleComponent... args)
    Replaces placeholders in a text template with corresponding values from the provided components.
    net.kyori.adventure.text.Component
    toComponent(@NotNull String key)
    Converts the provided translation key into an Adventure Component.
    net.kyori.adventure.text.Component
    toComponent(@NotNull String key, @NotNull Object... args)
    Converts the translation key and its formatted arguments into an Adventure Component.
    net.kyori.adventure.text.Component
    toComponent(@NotNull String key, @NotNull net.kyori.adventure.text.minimessage.tag.resolver.TagResolver... tagResolvers)
    Converts the translation key into a Component using MiniMessage format.
    net.kyori.adventure.text.Component
    toComponentBrace(@NotNull String key, @NotNull Object... args)
    Converts the provided translation key and its formatted arguments into an Adventure Component.
    net.kyori.adventure.text.Component
    toComponentBrace(@NotNull String key, @NotNull I18nLoader.SimpleComponent... args)
    Converts the given key and arguments into a formatted text component using braces for placeholders.
     
    void
    Updates the internal language map with the default behavior.
    void
    update(@NotNull InputStream customInputStream)
    Updates the current language map using the data from the provided input stream.
    void
    update(@NotNull Locale locale)
    Updates the internal language map based on the provided locale.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

  • Method Details

    • createMap

      public static <K, V> Map<K,V> createMap()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • reset

      public void reset(@Nullable @Nullable Locale locale)
      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

      public void update() throws IOException
      Updates the internal language map with the default behavior.
      Throws:
      IOException - if an I/O error occurs during loading of the localized strings.
    • update

      public void update(@NotNull @NotNull Locale locale) throws IOException
      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

      public void update(@NotNull @NotNull InputStream customInputStream) throws IOException
      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

      @NotNull public @NotNull Map<String,String> getLanguageMap()
      Return a copy of the language map
      Returns:
      a non-null copy of the language map
    • getLanguage

      @Nullable public @Nullable Locale 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

      public void setLanguage(@NotNull @NotNull Locale locale)
      Sets the language for localized operations.
      Parameters:
      locale - the Locale object representing the desired language and region settings; must not be null
    • to

      @NotNull public @NotNull String to(@NotNull @NotNull String key)
      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

      @NotNull public @NotNull String to(@NotNull @NotNull String key, @NotNull @NotNull Object... args)
      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 string
      args - 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 string
      args - 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 I18nLoader.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 of I18nLoader.SimpleComponent objects 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
    • toBaseComponent

      @NotNull public @NotNull net.md_5.bungee.api.chat.BaseComponent[] toBaseComponent(@NotNull @NotNull String key)
      Converts a localized string corresponding to the given key into an array of BaseComponent objects.
      Parameters:
      key - the translation key used to fetch the corresponding localized string
      Returns:
      an array of BaseComponent objects representing the localized string, or the key itself as BaseComponents if no translation exists
    • toBaseComponent

      @NotNull public @NotNull net.md_5.bungee.api.chat.BaseComponent[] toBaseComponent(@NotNull @NotNull String key, @NotNull @NotNull Object... args)
      Converts a translation key and its formatted arguments into an array of BaseComponent.

      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 string
      args - the arguments to format the localized string
      Returns:
      an array of BaseComponent representing the localized and formatted text
    • toBaseComponentBrace

      @NotNull public @NotNull net.md_5.bungee.api.chat.BaseComponent[] toBaseComponentBrace(@NotNull @NotNull String key, @NotNull @NotNull Object... args)
      Converts a localized and formatted string with placeholders replaced by the provided arguments into an array of BaseComponent.

      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 string
      args - the arguments to replace the "{}" placeholders within the localized string
      Returns:
      an array of BaseComponent representing the processed localized string
    • toBaseComponentBrace

      @NotNull public @NotNull net.md_5.bungee.api.chat.BaseComponent[] toBaseComponentBrace(@NotNull @NotNull String key, @NotNull @NotNull I18nLoader.SimpleComponent... args)
      Converts the provided key and arguments into an array of BaseComponent objects using a legacy text format with braces.

      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 key to be formatted, must not be null
      args - the array of SimpleComponent arguments to format, must not be null
      Returns:
      an array of BaseComponent objects representing the formatted text
    • toComponent

      @NotNull public net.kyori.adventure.text.Component toComponent(@NotNull @NotNull String key)
      Converts the provided translation key into an Adventure Component.
      Parameters:
      key - the translation key used to fetch the corresponding localized string
      Returns:
      an Adventure Component containing the localized string for the given translation key, or the key itself if no translation exists
    • toComponent

      @NotNull public net.kyori.adventure.text.Component toComponent(@NotNull @NotNull String key, @NotNull @NotNull Object... args)
      Converts the translation key and its formatted arguments into an Adventure Component.

      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 string
      args - the arguments to format the localized string
      Returns:
      an Adventure Component containing the localized and formatted string for the given translation key, or a plain text Component containing the key if no translation exists
    • toComponentBrace

      @NotNull public net.kyori.adventure.text.Component toComponentBrace(@NotNull @NotNull String key, @NotNull @NotNull Object... args)
      Converts the provided translation key and its formatted arguments into an Adventure Component.

      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 string
      args - the arguments to replace the "{}" placeholders within the localized string
      Returns:
      an Adventure Component containing the localized and formatted string for the given key, or the key itself as a plain text Component if no translation exists
    • toComponentBrace

      @NotNull public net.kyori.adventure.text.Component toComponentBrace(@NotNull @NotNull String key, @NotNull @NotNull I18nLoader.SimpleComponent... args)
      Converts the given key and arguments into a formatted text component using braces for placeholders.

      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 key used as the base text for the component must not be null
      args - the arguments to be placed into the placeholders within the text must not be null
      Returns:
      a Component instance representing the formatted text
    • toComponent

      @NotNull public net.kyori.adventure.text.Component toComponent(@NotNull @NotNull String key, @NotNull @NotNull net.kyori.adventure.text.minimessage.tag.resolver.TagResolver... tagResolvers)
      Converts the translation key into a Component using MiniMessage format.

      For example:

      
       "Click <click:run_command:/help>here</click> for help" produces a clickable "here" text
       "This is <red>red text</red>" produces text with red color
       "Player: <player>" with player tag resolver shows player name
       
      Parameters:
      key - the translation key used to fetch the corresponding localized string
      tagResolvers - optional tag resolvers used to process placeholders or tags within the localized string
      Returns:
      the Component representation of the translated key, or a plain text component containing the key if no translation exists
      See Also: