Interface LocaleResolverService

All Known Implementing Classes:
L10NCompatibleLocaleResolverServiceImpl

public interface LocaleResolverService
Service for dealing with locales and languages.

The service provides access to:

  • the project's languages
  • the project's default language
  • the project's default locale
  • the contributed locale resolvers for the current user, which may read his locale from places like the profile, request parameter, etc.
This class is implemented to be immutable; once configured, the available languages cannot change. If you need a system reload feature then refactor a bit, clear caches, and test well.
  • Method Details

    • getDefaultLocale

      Locale getDefaultLocale()
      Returns:
      the project-specific default locale
    • getDefaultLanguage

      String getDefaultLanguage()
      Returns the project-specific default language.
      Returns:
      2-letter iso code in lower case eg 'fr'.
    • getAvailableLanguagesAsString

      Set<String> getAvailableLanguagesAsString()
      Returns the project-specific available languages.
      Returns:
      Unmodifiable, not empty, contains at least one 2-letter iso code in lower case.
    • getBestLocale

      Locale getBestLocale(Set<Locale> preferredLocales)
      Returns the first Locale from preferredLocales that is provided by the current project.

      Notes:

      • Compares on language level only, it does not matter if and what Country/Variant the given Locales have.
      • Falls back to the project default.
      • //Caches the result.
      Parameters:
      preferredLocales - May be empty. Iteration order matters, so use a LinkedHashSet or NavigableSet.
    • getLocaleForGui

      Locale getLocaleForGui()
      Returns the gui locale for the current user to be used in the gui right now.

      It is guaranteed to be one of the project-offered gui locales, and falls back to the default one.

      Note that this may differ from the user-configured gui locale, for example when a request comes in with an overriding locale.

    • getPreferredLocales

      Set<Locale> getPreferredLocales()
      Returns the preferred locales of the current user.

      Note: All locale resolvers are asked to return all their entries. Thus this call is more expensive than iteratePreferredLocales() and stopping early.

      See Also:
    • iteratePreferredLocales

      Iterator<Locale> iteratePreferredLocales()
      Loops the preferred locales of the current user.

      Compared to getPreferredLocales() this has the advantage that you may abort early when you've find a good match. Then not all locale resolvers must be consulted.

      See Also:
    • completeWithSystemDefaults

      Locale completeWithSystemDefaults(@Nullable @Nullable String localeString)
      Adds the systems defaults to empty fields of a locale string
      Parameters:
      localeString - a locale string (e.g. as received from DB)
      Returns:
      a locale for the localeString with added system defaults
    • completeWithSystemDefaults

      Locale completeWithSystemDefaults(@Nullable @Nullable Locale locale)
      Adds the system defaults to empty fields of a locale. This method does not overwrite any of the settings of the locale
      Parameters:
      locale - a locale
      Returns:
      a locale with fields filled with system defaults.