Interface LocalizedPopulator
-
- All Known Implementing Classes:
DefaultLocalizedPopulator
public interface LocalizedPopulatorInterfaceLocalizedPopulatoris used to retrieve the languages supported by the current base store and to populate localized attributes.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default java.lang.StringgetLanguage(java.util.Locale locale)Returns the language for that specific locale.<T> voidpopulate(java.util.function.BiConsumer<java.util.Locale,T> consumer, java.util.function.Function<java.util.Locale,T> getter)Bi-directional method to populate the localized attributes.default <T> java.util.Map<java.lang.String,T>populateAsMapOfLanguages(java.util.function.Function<java.util.Locale,T> getter)Default implementation for populating aMap<String, Tapplying the functiongetteron each locale.default <T> java.util.Map<java.util.Locale,T>populateAsMapOfLocales(java.util.function.Function<java.lang.String,T> getter)Default implementation for populating aMap<Locale, Tapplying the functiongetteron each language ISO code.
-
-
-
Method Detail
-
populate
<T> void populate(java.util.function.BiConsumer<java.util.Locale,T> consumer, java.util.function.Function<java.util.Locale,T> getter)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 <T> java.util.Map<java.lang.String,T> populateAsMapOfLanguages(java.util.function.Function<java.util.Locale,T> getter)
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 <T> java.util.Map<java.util.Locale,T> populateAsMapOfLocales(java.util.function.Function<java.lang.String,T> getter)
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
default java.lang.String getLanguage(java.util.Locale locale)
Returns the language for that specific locale. The default implementation returns the result ofLocale.toString(). 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
-
-