Class LocaleUtil

java.lang.Object
org.keycloak.services.util.LocaleUtil

public class LocaleUtil extends Object
Author:
Marek Posolda, Daniel Fesenmeyer
  • Method Details

    • processLocaleParam

      public static void processLocaleParam(KeycloakSession session, RealmModel realm, AuthenticationSessionModel authSession)
    • resolveSupportedLocale

      public static Locale resolveSupportedLocale(RealmModel realm, Theme theme, String localeString)
      Resolves a language tag received from a client to a locale the server is prepared to serve messages for.

      The requested tag is matched against the locales declared by the theme, the locales supported by the realm and the realm default locale. Only a locale from that bounded, server-controlled set is ever returned, so a client cannot introduce arbitrary locales into the caches which are keyed by locale. A tag without a match resolves to the realm default locale, or to Locale.ENGLISH, rather than being rejected, so that clients still requesting a locale which has meanwhile been removed from the realm keep receiving usable content.

      Parameters:
      realm - the realm
      theme - the theme whose messages are looked up, may be null
      localeString - the requested language tag, may be null
      Returns:
      a supported locale, never null
    • getParentLocale

      @Deprecated(since="26.5", forRemoval=true) public static Locale getParentLocale(Locale locale)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Returns the parent locale of the given locale. If the locale just contains a language (e.g. "de"), returns the fallback locale "en". For "en" no parent exists, null is returned.
      Parameters:
      locale - the locale
      Returns:
      the parent locale, may be null
    • getParentLocale

      public static Locale getParentLocale(Locale locale, RealmModel realm)
      Returns the parent locale of the given locale. If the locale just contains a language (e.g. "de"), returns the fallback default locale of the realm or if that does not exist "en". For "en" no parent exists, null is returned.
      Returns:
      the parent locale, may be null
    • mergeGroupedMessages

      public static Properties mergeGroupedMessages(RealmModel realm, Locale locale, Map<Locale,Properties> messages)
      Merge the given (locale-)grouped messages into one instance of Properties, applicable for the given locale.
      Parameters:
      locale - the locale
      messages - the (locale-)grouped messages
      Returns:
      the merged properties
      See Also:
    • mergeGroupedMessages

      public static Properties mergeGroupedMessages(RealmModel realm, Locale locale, Map<Locale,Properties> firstMessages, Map<Locale,Properties> secondMessages)
      Merge the given (locale-)grouped messages into one instance of Properties, applicable for the given locale.

      The priority of the messages is as follows (abbreviations: F = firstMessages, S = secondMessages):

      1. F <language-region-variant>
      2. S <language-region-variant>
      3. F <language-region>
      4. S <language-region>
      5. F <language>
      6. S <language>
      7. F en
      8. S en

      Example for the message priority for locale "de-CH-1996" (language "de", region "CH", variant "1996):

      1. F de-CH-1996
      2. S de-CH-1996
      3. F de-CH
      4. S de-CH
      5. F de
      6. S de
      7. F en
      8. S en
      Parameters:
      locale - the locale
      firstMessages - the first (locale-)grouped messages, having higher priority (per locale) than secondMessages
      secondMessages - may be null, the second (locale-)grouped messages, having lower priority (per locale) than firstMessages
      Returns:
      the merged properties
      See Also:
    • enhancePropertiesWithRealmLocalizationTexts

      public static Properties enhancePropertiesWithRealmLocalizationTexts(RealmModel realm, Locale locale, Map<Locale,Properties> themeMessages)
      Enhance the properties from a theme with realm localization texts. Realm localization texts take precedence over the theme properties, but only when defined for the same locale. In general, texts for a more specific locale take precedence over texts for a less specific locale.

      For implementation details, see mergeGroupedMessages(RealmModel, Locale, Map, Map).

      Parameters:
      realm - the realm from which the localization texts should be used
      locale - the locale for which the relevant texts should be retrieved
      themeMessages - the theme messages, which should be enhanced and maybe overwritten
      Returns:
      the enhanced properties
    • getRealmLocalizationTexts

      public static Map<Locale,Properties> getRealmLocalizationTexts(RealmModel realm, Locale locale)