Class I18NText

java.lang.Object
org.tentackle.common.I18NText
All Implemented Interfaces:
Serializable, CharSequence, Comparable<I18NText>

public class I18NText extends Object implements CharSequence, Comparable<I18NText>, Serializable
Multilingual text.
This is an immutable and serializable object which is supported as a regular datatype via org.tentackle.sql.datatypes.I18NTextType.

Model example:

   ...
   I18NText        description   description    internationalized description [COLS=30]
   ...
 

Don't forget to add the [COLS=...] option if yoy want to use auto-binding for the field width in FX views.

Please keep in mind, that I18NText implements CharSequence and Comparable only for the current locale (see LocaleProvider.getInstance().getLocale()), but Object.equals(Object) and Object.hashCode() cover all translations. As a consequence, sorted sets of I18NText elements require appropriate attention.

See Also:
  • Constructor Details

    • I18NText

      public I18NText()
      Creates a vacant I18NText.
    • I18NText

      public I18NText(String text)
      Creates an I18NText from a string in the current locale.
      Parameters:
      text - the string
    • I18NText

      public I18NText(Locale locale, String text)
      Creates an I18NText from a string for the given locale.
      Parameters:
      locale - the locale
      text - the string
    • I18NText

      public I18NText(ParameterString parameterString)
      Creates an I18NText from a parameter string.
      Parameters:
      parameterString - the parameter string
      See Also:
    • I18NText

      protected I18NText(LinkedHashMap<Locale,String> textMap, Locale defaultLocale)
      Protected constructor for internal use only.
      Parameters:
      textMap - the translations
      defaultLocale - the default locale
  • Method Details

    • valueOf

      public static I18NText valueOf(String text)
      Creates an I18NText from a string in the current locale.
      Parameters:
      text - the string
      Returns:
      the i18n object
    • with

      public I18NText with(Locale locale, String text)
      Derives an I18NText object with a translation for the given locale.
      Parameters:
      locale - the locale
      text - the string
      Returns:
      the old translation, null if new
    • with

      public I18NText with(String text)
      Derives an I18NText object with a translation for the current locale.
      Parameters:
      text - the string
      Returns:
      the old translation, null if new
    • get

      public String get(Locale locale)
      Gets the translation for the given locale.
      Parameters:
      locale - the locale
      Returns:
      the string, null if no translations at all
    • get

      public String get()
      Gets the translation for the current locale.
      Returns:
      the string, null if no translations at all
    • getDefaultLocale

      public Locale getDefaultLocale()
      Gets the current default locale.
      Returns:
      the default locale, null if no translations at all or the translation for the fallback locale is set
    • getTranslations

      public Map<Locale,String> getTranslations()
      Gets the mapping between locales and translations.
      Returns:
      the unmodifiable map of translations
    • toParameterString

      public ParameterString toParameterString()
      Creates a parameter string of the current translations.
      Returns:
      the parameter string
    • isVacant

      public boolean isVacant()
      Returns whether there are valid translations at all.
      Returns:
      true if the map of translations is empty
    • length

      public int length()
      Specified by:
      length in interface CharSequence
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface CharSequence
    • charAt

      public char charAt(int index)
      Specified by:
      charAt in interface CharSequence
    • subSequence

      public CharSequence subSequence(int start, int end)
      Specified by:
      subSequence in interface CharSequence
    • chars

      public IntStream chars()
      Specified by:
      chars in interface CharSequence
    • codePoints

      public IntStream codePoints()
      Specified by:
      codePoints in interface CharSequence
    • compareTo

      public int compareTo(I18NText o)
      Specified by:
      compareTo in interface Comparable<I18NText>
    • toString

      public String toString()
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

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

      protected Locale apply(Locale locale, String text, LinkedHashMap<Locale,String> withTextMap, Locale withDefaultLocale)
      Applies a translation to an existing map and default locale.
      Parameters:
      locale - the locale to apply
      text - the translation to apply
      withTextMap - the current translations
      withDefaultLocale - the current default locale
      Returns:
      the new default locale
    • narrowLocale

      protected Locale narrowLocale(Locale locale)
      Narrows the effective locale returned by the LocaleProvider further to language.
      The LocaleProvider may allow wider locales for ResourceBundles, but for I18NText this would complicate things way too much.
      In most applications, the LocaleProvider narrows to language anyway.
      Parameters:
      locale - the effective locale
      Returns:
      the language-only locale