Interface LocalizedString
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 Summary
Modifier and TypeMethodDescriptionstatic LocalizedStringBuilderbuilder()static LocalizedStringBuilderbuilder(LocalizedString template) static LocalizedStringdeepCopy(LocalizedString template) static LocalizedStringempty()Creates an instance without any value.Searches the translation for some exact locales in the order they appear and returning the result in anOptional.Searches the translation for an exact locale and returning the result in anOptional.default StringSearches the translation for some exact locales in the order they appear and using null as result if no match could be found.default StringSearches the translation for a locale specified in IETF BCP 47 by language tag string.default StringSearches the translation for an exact locale by usingnullin the case the locale ist not present.Returns all locales included in this instance.default StringgetTranslation(Iterable<Locale> locales) Searches a translation which matches a locale inlocalesand uses language fallbackes.Delivers an immutable map of the translation.default LocalizedStringmapValue(BiFunction<Locale, String, String> function) Creates a new instance where each translation value is transformed withfunction.static LocalizedStringof()static LocalizedStringof(LocalizedString template) static LocalizedStringCreates an instance with one locale translation pair.static LocalizedStringCreates an instance for two different locales.static LocalizedStringstatic LocalizedStringstatic LocalizedStringofStringToStringMap(Map<String, String> translations) default LocalizedStringCreates a newLocalizedStringcontaining the given entries and the new one.default voidvoiddefault LocalizedStringCreates a newLocalizedStringwhere all translations are slugified (remove whitespace, etc.).default LocalizedStringCreates a newLocalizedStringwhere all translations are slugified (remove whitespace, etc.) and a random supplement is added.default Stream<LocalizedStringEntry>stream()Creates a new Stream of entries.static Collector<LocalizedStringEntry,?, LocalizedString> Collector to collect a stream ofLocalizedStringEntrys to oneLocalizedString.static com.fasterxml.jackson.core.type.TypeReference<LocalizedString>Creates a container which contains the full Java type information to deserialize this class from JSON.values()default <T> TwithLocalizedString(Function<LocalizedString, T> helper)
-
Method Details
-
values
-
setValue
-
of
-
of
-
deepCopy
-
builder
-
builder
-
withLocalizedString
-
localeValues
-
empty
Creates an instance without any value.- Returns:
- instance without any value
-
of
Creates an instance with one locale translation pair.- Parameters:
locale- the locale for the one translationvalue- the translation for the specified locale- Returns:
- translation for one language
-
of
Creates an instance for two different locales.- Parameters:
locale1- the first localevalue1- the translation corresponding tolocale1locale2- the second locale which differs fromlocale1value2- the translation corresponding tolocale2- Returns:
- new instance for two key value pairs
-
of
Creates an instance by supplying a map ofLocaleandString. 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
translationat creation time
-
ofStringToStringMap
Creates an instance by supplying a map ofStringthe language tag andString. 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
translationat creation time
-
plus
Creates a newLocalizedStringcontaining the given entries and the new one. It is not allowed to override existing entries.- Parameters:
locale- the additional locale of the new entryvalue- the value for thelocale- Returns:
- a LocalizedString containing this data and the from the parameters.
- Throws:
IllegalArgumentException- if duplicate locales are provided
-
find
Searches the translation for an exact locale and returning the result in anOptional.- Parameters:
locale- the locale which should be searched- Returns:
- A filled optional with the translation belonging to
localeor an empty optional if the locale is not present.
-
get
Searches the translation for an exact locale by usingnullin the case the locale ist not present.- Parameters:
locale- the locale which should be searched- Returns:
- the translation belonging to
localeor null if the locale is not present.
-
get
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 anLocale- Returns:
- the translation belonging to
languageTagornullif the locale is not present.
-
find
Searches the translation for some exact locales in the order they appear and returning the result in anOptional.- 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
localesor an empty optional if none of the locales is not present.
-
get
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
localesor null if none of the locales is not present.
-
getTranslation
Searches a translation which matches a locale inlocalesand 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
Creates a new instance where each translation value is transformed withfunction.- Parameters:
function- transforms a value for a locale into a new value- Returns:
- a new
LocalizedStringwhich consist all elements for this transformed withfunction
-
stream
Creates a new Stream of entries.- Returns:
- stream of all entries
-
streamCollector
Collector to collect a stream ofLocalizedStringEntrys to oneLocalizedString.- Returns:
- collector
-
slugified
Creates a newLocalizedStringwhere all translations are slugified (remove whitespace, etc.).- Returns:
- new instance
-
slugifiedUnique
Creates a newLocalizedStringwhere 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
Returns all locales included in this instance.- Returns:
- locales
-
getTranslations
Delivers an immutable map of the translation.- Returns:
- the key-value pairs for the translation
-
set
-
typeReference
Creates a container which contains the full Java type information to deserialize this class from JSON.- Returns:
- type reference
-
ofEnglish
-