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 Details

    • populate

      <T> void populate(BiConsumer<Locale,T> consumer, Function<Locale,T> getter)
      Bi-directional method to populate the localized attributes. It can be used to populate Model classes by reading the content from the getter function, and also populate localized Maps 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 function getter to get the localized value that may be coming from different sources (see examples bellow). The result of the getter function will then be used on the BiConsumer setter function. Examples of its usage: Populate a Map<String, String> from a localized model final Map<String, String> map = new HashMap<>(); getLocalizedPopulator().populate( (locale, value) -> map.put(getLocalizedPopulator().getLanguage(locale), value), (locale) -> source.getSomeLocalizedField(locale) ); * Populate an ItemModel from a localized Map<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 - A BiConsumer function that will accept the Locale and the Value extracted from the getter function.
      getter - A Function that will be applied to get the localized value for a given Locale.
    • populateAsMapOfLanguages

      default <T> Map<String,T> populateAsMapOfLanguages(Function<Locale,T> getter)
      Default implementation for populating a Map<String, T applying the function getter on each locale.
      Type Parameters:
      T - the type of the object that is put as a value in the returned map.
      Parameters:
      getter - A Function that will be applied to each Locale.
      Returns:
      map with the localized values.
    • populateAsMapOfLocales

      default <T> Map<Locale,T> populateAsMapOfLocales(Function<String,T> getter)
      Default implementation for populating a Map<Locale, T applying the function getter on each language ISO code.
      Type Parameters:
      T - the type of the object that is put as a value in the returned map.
      Parameters:
      getter - A Function that will be applied to each language ISO code
      Returns:
      map with the localized values.
    • getLanguage

      default String getLanguage(Locale locale)
      Returns the language for that specific locale. The default implementation returns the result of Locale.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