Package dev.orne.i18n

Class I18nResourcesString

java.lang.Object
dev.orne.i18n.I18nResourcesString
All Implemented Interfaces:
I18nString, Serializable

@API(status=STABLE, since="0.1") public class I18nResourcesString extends Object implements I18nString
Implementation of I18nString based on I18nResources.

The class supports providing arguments to be used when computing the resulting texts for the different languages. As the I18N resources formatting depends of the underlying implementation the default text will be formatted with MessageFormat passing the configured arguments, if any.

Since:
0.1
Version:
1.0, 2021-01
Author:
(w) Iker Hernaez
See Also:
  • Constructor Details

    • I18nResourcesString

      public I18nResourcesString(String resources, @NotNull @NotNull String defaultMessage, @NotNull @NotNull String[] codes, Serializable... arguments)
      Creates a new instance.
      Parameters:
      resources - The I18N resources to use. If null will use default ones
      defaultMessage - The default text
      codes - The I18N text codes, in order of priority
      arguments - The text arguments, applied to resolved text or default text
  • Method Details

    • forDefault

      public static I18nResourcesString.Builder forDefault(@NotNull @NotNull String defaultMessage)
      Creates a builder of a new instance with the specified default text.

      The text will be formatted with MessageFormat passing the configured arguments.

      Parameters:
      defaultMessage - The default text
      Returns:
      The new instance builder
      See Also:
    • getI18nResourcesKey

      public String getI18nResourcesKey()
      Returns the key of the I18N resources to use. If null default ones will be used.
      Returns:
      The key of the I18N resources to use
    • getDefaultText

      @NotNull public @NotNull String getDefaultText()
      Returns the unformatted default text.
      Returns:
      The unformatted default text
    • getFormattedDefaultText

      @NotNull public @NotNull String getFormattedDefaultText()
      Returns the formatted default text.
      Returns:
      The formatted default text
    • getCodes

      @NotNull public @NotNull String[] getCodes()
      Returns the I18N text codes, in order of priority.
      Returns:
      The I18N text codes
    • getArguments

      @NotNull public @NotNull Serializable[] getArguments()
      Returns the text arguments, applied to resolved text or default text.
      Returns:
      The text arguments
    • get

      public String get()
      Returns the text for the current language.
      Specified by:
      get in interface I18nString
      Returns:
      The text for the current language
    • get

      public String get(@NotNull @NotNull String language)
      Returns the text for the specified language, or the default text if no translation is available.
      Specified by:
      get in interface I18nString
      Parameters:
      language - The language of the requested text
      Returns:
      The text for the specified language
    • get

      public String get(@NotNull @NotNull Locale locale)
      Returns the text for language of the specified locale, or the default text if no translation is available.
      Specified by:
      get in interface I18nString
      Parameters:
      locale - The language of the requested text
      Returns:
      The text for the specified language
    • hashCode

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • isEquivalent

      public boolean isEquivalent(I18nString obj)
      Returns true if the specified I18nString is equivalent to this instance.
      1. If other is null returns false.
      2. If other is of the same type calls equals().
      3. Otherwise checks that the translations for current language of both instances are equal.
      Specified by:
      isEquivalent in interface I18nString
      Parameters:
      obj - The instance to compare against
      Returns:
      If both instances are equivalent
    • asMap

      @NotNull public @NotNull I18nStringMap asMap()
      Returns this instance represented as an I18nStringMap instance.
      Specified by:
      asMap in interface I18nString
      Returns:
      This instance represented as an I18nStringMap instance
    • toString

      @NotNull public @NotNull String toString()
      Overrides:
      toString in class Object