Interface LocalizedString

All Known Implementing Classes:
LocalizedStringImpl

public interface LocalizedString
A localized string is a object where the keys are Locales (HTTP API: ISO language tags), and the values are the corresponding strings used for that language.
final LocalizedString ls = LocalizedString.of(Locale.GERMAN, "Hundefutter", Locale.ENGLISH, "dog food");

assertThat(ls.getTranslation(singletonList(Locale.US))).isEqualTo("dog food");//fuzzy search
assertThat(ls.getTranslation(singletonList(Locale.ENGLISH))).isEqualTo("dog food");//strict search
assertThat(ls.getLocales()).isEqualTo(new HashSet<>(asList(Locale.GERMAN, Locale.ENGLISH)));//inspecting locales
assertThat(ls.slugified())//slugified values for urls
        .isEqualTo(LocalizedString.of(Locale.GERMAN, "hundefutter", Locale.ENGLISH, "dog-food"));
final LocalizedString slugifiedUnique = ls.slugifiedUnique();
assertThat(slugifiedUnique.get(Locale.GERMAN)).matches("hundefutter-\\d+");//example: hundefutter-62899407
assertThat(slugifiedUnique.get(Locale.ENGLISH)).matches("dog-food-\\d+");

See the test code.

  • Method Details

    • values

      @NotNull @NotNull Map<String,String> values()
    • setValue

      void setValue(String key, String value)
    • of

      static LocalizedString of()
    • of

      static LocalizedString of(LocalizedString template)
    • deepCopy

      @Nullable static LocalizedString deepCopy(@Nullable LocalizedString template)
    • builder

      static LocalizedStringBuilder builder()
    • builder

      static LocalizedStringBuilder builder(LocalizedString template)
    • withLocalizedString

      default <T> T withLocalizedString(Function<LocalizedString,T> helper)
    • localeValues

      @NotNull default @NotNull Map<Locale,String> localeValues()
    • empty

      static LocalizedString empty()
      Creates an instance without any value.
      Returns:
      instance without any value
    • of

      static LocalizedString of(Locale locale, String value)
      Creates an instance with one locale translation pair.
      Parameters:
      locale - the locale for the one translation
      value - the translation for the specified locale
      Returns:
      translation for one language
    • of

      static LocalizedString of(Locale locale1, String value1, Locale locale2, String value2)
      Creates an instance for two different locales.
      Parameters:
      locale1 - the first locale
      value1 - the translation corresponding to locale1
      locale2 - the second locale which differs from locale1
      value2 - the translation corresponding to locale2
      Returns:
      new instance for two key value pairs
    • of

      static LocalizedString of(Map<Locale,String> translations)
      Creates an instance by supplying a map of Locale and String. Changes to the map won't affect the instance.
      Parameters:
      translations - the key value pairs for the translation
      Returns:
      a new instance which has the same key value pairs as translation at creation time
    • ofStringToStringMap

      static LocalizedString ofStringToStringMap(Map<String,String> translations)
      Creates an instance by supplying a map of String the language tag and String. Changes to the map won't affect the instance.
      Parameters:
      translations - the key value pairs for the translation
      Returns:
      a new instance which has the same key value pairs as translation at creation time
    • plus

      default LocalizedString plus(Locale locale, String value)
      Creates a new LocalizedString containing the given entries and the new one. It is not allowed to override existing entries.
      Parameters:
      locale - the additional locale of the new entry
      value - the value for the locale
      Returns:
      a LocalizedString containing this data and the from the parameters.
      Throws:
      IllegalArgumentException - if duplicate locales are provided
    • find

      @Nonnull default Optional<String> find(Locale locale)
      Searches the translation for an exact locale and returning the result in an Optional.
      Parameters:
      locale - the locale which should be searched
      Returns:
      A filled optional with the translation belonging to locale or an empty optional if the locale is not present.
    • get

      @Nullable default String get(Locale locale)
      Searches the translation for an exact locale by using null in the case the locale ist not present.
      Parameters:
      locale - the locale which should be searched
      Returns:
      the translation belonging to locale or null if the locale is not present.
    • get

      @Nullable default String get(String languageTag)
      Searches the translation for a locale specified in IETF BCP 47 by language tag string. If the specified language tag contains any ill-formed subtags, the first such subtag and all following subtags are ignored.
      Parameters:
      languageTag - the IETF language tag corresponding to an Locale
      Returns:
      the translation belonging to languageTag or null if the locale is not present.
    • find

      @Nonnull default Optional<String> find(Iterable<Locale> locales)
      Searches the translation for some exact locales in the order they appear and returning the result in an Optional.
      Parameters:
      locales - the locale which should be searched, the first exact match wins
      Returns:
      A filled optional with the translation belonging to one of the locales or an empty optional if none of the locales is not present.
    • get

      @Nullable default String get(Iterable<Locale> locales)
      Searches the translation for some exact locales in the order they appear and using null as result if no match could be found.
      Parameters:
      locales - the locale which should be searched, the first exact match wins
      Returns:
      the translation belonging to one of the locales or null if none of the locales is not present.
    • getTranslation

      @Nullable default String getTranslation(Iterable<Locale> locales)
      Searches a translation which matches a locale in locales and uses language fallbackes. If locales which countries are used then the algorithm searches also for the pure language locale. So if "en_US" could not be found then "en" will be tried.
      Parameters:
      locales - the locales to try out
      Returns:
      a translation matching one of the locales or null
    • mapValue

      default LocalizedString mapValue(BiFunction<Locale,String,String> function)
      Creates a new instance where each translation value is transformed with function.
      Parameters:
      function - transforms a value for a locale into a new value
      Returns:
      a new LocalizedString which consist all elements for this transformed with function
    • stream

      default Stream<LocalizedStringEntry> stream()
      Creates a new Stream of entries.
      Returns:
      stream of all entries
    • streamCollector

      static Collector<LocalizedStringEntry,?,LocalizedString> streamCollector()
      Collector to collect a stream of LocalizedStringEntrys to one LocalizedString.
      Returns:
      collector
    • slugified

      default LocalizedString slugified()
      Creates a new LocalizedString where all translations are slugified (remove whitespace, etc.).
      Returns:
      new instance
    • slugifiedUnique

      default LocalizedString slugifiedUnique()
      Creates a new LocalizedString where all translations are slugified (remove whitespace, etc.) and a random supplement is added. This slugify methods appends a random string for a little uniqueness.
      Returns:
      new instance
    • getLocales

      @Nonnull default Set<Locale> getLocales()
      Returns all locales included in this instance.
      Returns:
      locales
    • getTranslations

      default Map<Locale,String> getTranslations()
      Delivers an immutable map of the translation.
      Returns:
      the key-value pairs for the translation
    • set

      default void set(Locale languageTag, String value)
    • typeReference

      static com.fasterxml.jackson.core.type.TypeReference<LocalizedString> typeReference()
      Creates a container which contains the full Java type information to deserialize this class from JSON.
      Returns:
      type reference
    • ofEnglish

      static LocalizedString ofEnglish(String translationForEnglish)