Interface LocalizedPopulator
- All Known Implementing Classes:
DefaultLocalizedPopulator
public interface LocalizedPopulator
Interface
LocalizedPopulator is used to retrieve the languages supported by the current base store and to
populate localized attributes.-
Method Summary
Modifier and TypeMethodDescriptiondefault StringgetLanguage(Locale locale) Returns the language for that specific locale.<T> voidpopulate(BiConsumer<Locale, T> consumer, Function<Locale, T> getter) Bi-directional method to populate the localized attributes.populateAsMapOfLanguages(Function<Locale, T> getter) Default implementation for populating aMap<String, Tapplying the functiongetteron each locale.populateAsMapOfLocales(Function<String, T> getter) Default implementation for populating aMap<Locale, Tapplying the functiongetteron each language ISO code.
-
Method Details
-
populate
Bi-directional method to populate the localized attributes. It can be used to populate Model classes by reading the content from thegetterfunction, and also populate localizedMaps by reading content from a localized data model. This method will iterate over all locales (Locale) available at the execution time. For each locale, it will execute the functiongetterto get the localized value that may be coming from different sources (see examples bellow). The result of thegetterfunction will then be used on theBiConsumersetterfunction. Examples of its usage: Populate aMap<String, String>from a localized modelfinal Map<String, String> map = new HashMap<>(); getLocalizedPopulator().populate( (locale, value) -> map.put(getLocalizedPopulator().getLanguage(locale), value), (locale) -> source.getSomeLocalizedField(locale) );* Populate anItemModelfrom a localizedMap<String, String>// source.getTitle() returns Map<String, String> Optional.ofNullable(source.getTitle()) .ifPresent(title -> getLocalizedPopulator() .populate((locale, value) -> target.setTitle(value, locale), (locale) -> title.get(getLocalizedPopulator().getLanguage(locale))));- Parameters:
consumer- ABiConsumerfunction that will accept the Locale and the Value extracted from thegetterfunction.getter- AFunctionthat will be applied to get the localized value for a givenLocale.
-
populateAsMapOfLanguages
Default implementation for populating aMap<String, Tapplying the functiongetteron each locale.- Type Parameters:
T- the type of the object that is put as a value in the returned map.- Parameters:
getter- AFunctionthat will be applied to eachLocale.- Returns:
- map with the localized values.
-
populateAsMapOfLocales
Default implementation for populating aMap<Locale, Tapplying the functiongetteron each language ISO code.- Type Parameters:
T- the type of the object that is put as a value in the returned map.- Parameters:
getter- AFunctionthat will be applied to each language ISO code- Returns:
- map with the localized values.
-
getLanguage
Returns the language for that specific locale. The default implementation returns the result ofLocale.toLanguageTag(). More specialized implementations should opt to return the format in which the data can be correctly translated in other domains.- Parameters:
locale- the locale we want to get the language from.- Returns:
- the language for that locale
-