Class LocaleUtil


  • public final class LocaleUtil
    extends Object
    A collection of utility functions (static methods) operating on Locales.
    • Method Detail

      • getLocale

        public static Locale getLocale​(String lang)
        Returns the Locale corresponding to specified language.
        Parameters:
        lang - specifies the language: a two-letter ISO language code as defined by ISO-639, possibly followed by a two-letter ISO Country Code as defined by ISO-3166. Examples: "en", "en-US", "fr", "fr-CA".
        Returns:
        the Locale corresponding to specified language. Note that this Locale is not necessarily supported by Java methods such as NumberFormat.getInstance().
      • normalizeLang

        public static String normalizeLang​(String lang,
                                           String[] parts)
        Returns a normalized, possibly simplified, version of specified language. For example, returns "en-US" (ISO 639-1) for "en_us-win" and "eng" for "ENG" (ISO 639-2 or ISO 639-3).
        Parameters:
        lang - a language code preferably matching pattern [a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*
        parts - returns lower_case_language in the first element of this array and upper_case_country or null in its second element. May be null.
        Returns:
        lang normalized as lower_case_language, optionally followed by '-' upper_case_country.
      • getIndividualLanguageCode

        public static String getIndividualLanguageCode​(Locale locale)
        Returns the lowercase ISO 639 code corresponding to the individual language of the specified locale.

        For example, returns nn for no-NO-NY because no is just a macrolanguage and not an actual individual language.