Interface LocalizedString
- All Known Implementing Classes:
LocalizedStringImpl
public interface LocalizedString
A localized string is a object where the keys are
Locale
s (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 LocalizedStringBuilder
builder()
static LocalizedStringBuilder
builder
(LocalizedString template) static LocalizedString
deepCopy
(LocalizedString template) static LocalizedString
empty()
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 String
Searches the translation for some exact locales in the order they appear and using null as result if no match could be found.default String
Searches the translation for a locale specified in IETF BCP 47 by language tag string.default String
Searches the translation for an exact locale by usingnull
in the case the locale ist not present.Returns all locales included in this instance.default String
getTranslation
(Iterable<Locale> locales) Searches a translation which matches a locale inlocales
and uses language fallbackes.Delivers an immutable map of the translation.default LocalizedString
mapValue
(BiFunction<Locale, String, String> function) Creates a new instance where each translation value is transformed withfunction
.static LocalizedString
of()
static LocalizedString
of
(LocalizedString template) static LocalizedString
Creates an instance with one locale translation pair.static LocalizedString
Creates an instance for two different locales.static LocalizedString
static LocalizedString
static LocalizedString
ofStringToStringMap
(Map<String, String> translations) default LocalizedString
Creates a newLocalizedString
containing the given entries and the new one.default void
void
default LocalizedString
Creates a newLocalizedString
where all translations are slugified (remove whitespace, etc.).default LocalizedString
Creates a newLocalizedString
where 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 ofLocalizedStringEntry
s 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> T
withLocalizedString
(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 tolocale1
locale2
- the second locale which differs fromlocale1
value2
- the translation corresponding tolocale2
- Returns:
- new instance for two key value pairs
-
of
Creates an instance by supplying a map ofLocale
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
translation
at creation time
-
ofStringToStringMap
Creates an instance by supplying a map ofString
the 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
translation
at creation time
-
plus
Creates a newLocalizedString
containing 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
locale
or an empty optional if the locale is not present.
-
get
Searches the translation for an exact locale by usingnull
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
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
languageTag
ornull
if 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
locales
or 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
locales
or null if none of the locales is not present.
-
getTranslation
Searches a translation which matches a locale inlocales
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
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
LocalizedString
which 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 ofLocalizedStringEntry
s to oneLocalizedString
.- Returns:
- collector
-
slugified
Creates a newLocalizedString
where all translations are slugified (remove whitespace, etc.).- Returns:
- new instance
-
slugifiedUnique
Creates a newLocalizedString
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
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
-