Class LocaleUtils


  • public class LocaleUtils
    extends java.lang.Object
    Utility class containing helper methods for language and country settings.
    Since:
    4.0.0
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  LocaleUtils.LocaleItem
      Data class representing a locale item in configuration files.
      static class  LocaleUtils.LocaleItems
      Data element describing a configuration file for Locales.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String CONFIG_FILE_PATH
      Configuration file location for this class
      static java.lang.String COUNTRY_IGNORE
      Match-all entries regardless of country.
      static java.lang.String COUNTRY_NONE_EMPTY
      Match-all non-empty countries.
      static java.lang.String COUNTRY_RESERVE
      Reserved country codes to be used if ISO mappings are not enough.
      protected static java.util.Map<java.lang.String,​java.lang.String> countryCodes
      Cache for country codes.
      static java.lang.String ISO_15924
      A four letter code for script (writing system) (in title case).
      static java.lang.String ISO_3166_1_Alpha2
      A two uppercase letter code for countries.
      static java.lang.String ISO_639_1_Alpha2
      A two lowercase letter code for languages.
      static java.lang.String ISO_639_3_Alpha3
      A three lowercase letter code for languages.
      static java.lang.String LANGUAGE_IGNORE
      Match-all entries regardless of language.
      static java.lang.String LANGUAGE_NONE_EMPTY
      Match-all non-empty languages.
      static java.lang.String LANGUAGE_RESERVE
      Reserved language codes to be used if ISO mappings are not enough.
      protected static java.util.Map<java.lang.String,​java.lang.String> languageCodes
      Cache for lang codes.
      static java.lang.String SCRIPT_IGNORE
      Match-all entries regardless of script.
      static java.lang.String SCRIPT_NONE_EMPTY
      Match-all non-empty scripts.
      static java.lang.String SCRIPT_RESERVE
      Reserved script code to be used if ISO mappings are not enough.
      protected static java.util.Map<java.lang.String,​java.lang.String> scriptCodes
      Cache for script codes.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String[] getCountryCodes()  
      static java.util.List<LocaleUtils.LocaleItem> getCountryItems()  
      static java.util.List<LocaleUtils.LocaleItem> getCountryItems​(java.lang.String code)  
      static java.lang.String getCustomCountry​(java.lang.String country, java.lang.String code)
      Get external country code (if code scheme is supported) from internal country code.
      static java.lang.String getCustomLanguage​(java.lang.String language, java.lang.String code)
      Get external language code from internal language code if the code scheme is supported.
      static java.lang.String getCustomScript​(java.lang.String script, java.lang.String code)
      Get external script code (if code scheme is supported) from internal script code.
      static java.lang.String[] getLanguageCodes()  
      static java.util.List<LocaleUtils.LocaleItem> getLanguageItems()  
      static java.util.List<LocaleUtils.LocaleItem> getLanguageItems​(java.lang.String code)  
      static java.lang.String getPubServerCountry​(java.lang.String country, java.lang.String code)
      Translates an external country code into pubserver internal representation (ISO_3166_1_Alpha2).
      static java.lang.String getPubServerLanguage​(java.lang.String language, java.lang.String code)
      Get internal language code from any supported language code scheme.
      static java.lang.String getPubServerScript​(java.lang.String script, java.lang.String code)
      Translates an external script code into pubserver internal representation (ISO_15924).
      static java.lang.String[] parseLanguageAndCountry​(java.lang.String in)
      Parse language and country part from a string in the format <lang>delimiter<country>
      static java.lang.String[] parseLanguageAndCountry​(java.lang.String in, java.lang.String delimiter)
      Parse language and country part from a string in the format <lang>delimiter<country>
      static int refresh​(PluginConfig config)
      Refreshes the cache of supported language and country codes from the server.
      static int refresh​(LocaleUtils.LocaleItems items)
      Refreshes the cache of supported language and country codes from the server.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • CONFIG_FILE_PATH

        public static final java.lang.String CONFIG_FILE_PATH
        Configuration file location for this class
        See Also:
        Constant Field Values
      • LANGUAGE_NONE_EMPTY

        public static final java.lang.String LANGUAGE_NONE_EMPTY
        Match-all non-empty languages.

        See https://tools.ietf.org/html/bcp47
        2.2.1 The subtags in the range 'qaa' through 'qtz' are reserved for private use in language tags.

        Value is existing as extension of ISO_639_3_Alpha3 codes.

        See Also:
        Constant Field Values
      • LANGUAGE_IGNORE

        public static final java.lang.String LANGUAGE_IGNORE
        Match-all entries regardless of language.

        See https://tools.ietf.org/html/bcp47
        2.2.1 The subtags in the range 'qaa' through 'qtz' are reserved for private use in language tags.

        Value is existing as extension of ISO_639_3_Alpha3 codes.

        See Also:
        Constant Field Values
      • LANGUAGE_RESERVE

        public static final java.lang.String LANGUAGE_RESERVE
        Reserved language codes to be used if ISO mappings are not enough. Other language codes can be added by config file.

        Values are existing as extension of ISO_639_3_Alpha3 codes.

        See Also:
        Constant Field Values
      • COUNTRY_NONE_EMPTY

        public static final java.lang.String COUNTRY_NONE_EMPTY
        Match-all non-empty countries.

        See https://tools.ietf.org/html/bcp47
        2.2.4 The region subtags 'AA', 'QM'-'QZ', 'XA'-'XZ', and 'ZZ' are reserved for private use in language tags.

        Value is existing as extension of ISO_3166_1_Alpha2 codes.

        See Also:
        Constant Field Values
      • COUNTRY_IGNORE

        public static final java.lang.String COUNTRY_IGNORE
        Match-all entries regardless of country.

        See https://tools.ietf.org/html/bcp47
        2.2.4 The region subtags 'AA', 'QM'-'QZ', 'XA'-'XZ', and 'ZZ' are reserved for private use in language tags.

        Value is existing as extension of ISO_3166_1_Alpha2 codes.

        See Also:
        Constant Field Values
      • COUNTRY_RESERVE

        public static final java.lang.String COUNTRY_RESERVE
        Reserved country codes to be used if ISO mappings are not enough. Other country codes can be added by config file.

        Values are existing as extension of ISO_3166_1_Alpha2 codes.

        See Also:
        Constant Field Values
      • SCRIPT_NONE_EMPTY

        public static final java.lang.String SCRIPT_NONE_EMPTY
        Match-all non-empty scripts.

        See https://tools.ietf.org/html/bcp47
        2.2.3 The script subtags 'Qaaa' through 'Qabx' are reserved for private use in language tags. Value is existing as extension of ISO_15924 codes.

        See Also:
        Constant Field Values
      • SCRIPT_IGNORE

        public static final java.lang.String SCRIPT_IGNORE
        Match-all entries regardless of script.

        See https://tools.ietf.org/html/bcp47
        2.2.3 The script subtags 'Qaaa' through 'Qabx' are reserved for private use in language tags. Value is existing as extension of ISO_15924 codes.

        See Also:
        Constant Field Values
      • SCRIPT_RESERVE

        public static final java.lang.String SCRIPT_RESERVE
        Reserved script code to be used if ISO mappings are not enough. Other script codes can be added by config file.

        Value is existing as extension of ISO_15924 codes.

        See Also:
        Constant Field Values
      • languageCodes

        protected static final java.util.Map<java.lang.String,​java.lang.String> languageCodes
        Cache for lang codes.

        Key is [source-name]+[delimiter]+[code-constant], value is [target-name].

        E.g. key="de>ISO_3166_1_Alpha2", value="deu"

      • countryCodes

        protected static final java.util.Map<java.lang.String,​java.lang.String> countryCodes
        Cache for country codes.

        Key is [source-name]+[delimiter]+[code-constant], value is [target-name].

        E.g. key="de>IANA_TDL", value="DE" (if IANA_TDL is supported by configuration)

      • scriptCodes

        protected static final java.util.Map<java.lang.String,​java.lang.String> scriptCodes
        Cache for script codes.
    • Method Detail

      • getPubServerLanguage

        public static java.lang.String getPubServerLanguage​(java.lang.String language,
                                                            java.lang.String code)
        Get internal language code from any supported language code scheme.

        E.g. getPubServerLanguage("de", ISO_639_1_Alpha2) will return "deu".

        E.g. getPubServerLanguage("ger", "ISO_639_2_B") will return "deu" (if ISO_639_2_B is suported by configuration)

        Parameters:
        language - Name of language according to code.
        code - Name of external code.
        If code is an empty string the lang argument has to be a valid ISO 639‑Alpha-2, or ISO 639‑Alpha-3 lang name
        Returns:
        A PubServer language name (i.e. ISO 639‑3 three letter code) or empty string
        Throws:
        NullArgumentException - if any of the input arguments is null
        java.lang.IllegalArgumentException - if no matching language key could be found
        Since:
        4.0.0
      • getCustomLanguage

        public static java.lang.String getCustomLanguage​(java.lang.String language,
                                                         java.lang.String code)
        Get external language code from internal language code if the code scheme is supported.

        E.g. getCustomLanguage("deu", ISO_639_1_Alpha2) will return "de".

        Parameters:
        language - A PubServer language name (i.e. ISO 639‑3 three letter code)
        code - Name of external code.
        Returns:
        Name of language according to external code.
        Throws:
        NullArgumentException - if any of the input arguments is null
        java.lang.IllegalArgumentException - if no matching language key could be found
        Since:
        4.0.0
      • getPubServerCountry

        public static java.lang.String getPubServerCountry​(java.lang.String country,
                                                           java.lang.String code)
        Translates an external country code into pubserver internal representation (ISO_3166_1_Alpha2).
        Parameters:
        country -
        code -
        Returns:
        pubserver country code from ISO_3166_1_Alpha2 scheme or empty string
        Throws:
        NullArgumentException - if any of the input arguments is null
        java.lang.IllegalArgumentException - if no matching country key could be found
        Since:
        4.0.0
      • getCustomCountry

        public static java.lang.String getCustomCountry​(java.lang.String country,
                                                        java.lang.String code)
        Get external country code (if code scheme is supported) from internal country code.

        E.g. getCustomCountry("GB", "IANA_TDL") will return "uk" (if IANA_TDL is supported by configuration).

        Parameters:
        country - A PubServer country name (i.e. ISO_3166_1_Alpha2 two letter code)
        code - Name of external code.
        Returns:
        Name of country according to external code or empty string.
        Throws:
        NullArgumentException - if any of the input arguments is null
        java.lang.IllegalArgumentException - if no matching country key could be found
        Since:
        4.0.0
      • getPubServerScript

        public static java.lang.String getPubServerScript​(java.lang.String script,
                                                          java.lang.String code)
        Translates an external script code into pubserver internal representation (ISO_15924).
        Parameters:
        script -
        code -
        Returns:
        pubserver script code from ISO_15924 scheme or empty string
        Throws:
        NullArgumentException - if any of the input arguments is null
        java.lang.IllegalArgumentException - if no matching script key could be found
        Since:
        4.0.0
      • getCustomScript

        public static java.lang.String getCustomScript​(java.lang.String script,
                                                       java.lang.String code)
        Get external script code (if code scheme is supported) from internal script code.

        E.g. getCustomScript("Latn", "MYSCHEME") might return "west" (if MYSCHEME is supported by configuration).

        Parameters:
        script - A PubServer script name (i.e. ISO_15924 four letter code)
        code - Name of external code.
        Returns:
        Name of script according to external code or empty string.
        Throws:
        NullArgumentException - if any of the input arguments is null
        java.lang.IllegalArgumentException - if no matching script key could be found
        Since:
        4.0.0
      • refresh

        public static int refresh​(PluginConfig config)
        Refreshes the cache of supported language and country codes from the server.
        Parameters:
        config - PluginConfig
        Returns:
        the joined size of the language codes, country codes and script codes
      • refresh

        public static int refresh​(LocaleUtils.LocaleItems items)
        Refreshes the cache of supported language and country codes from the server.
        Parameters:
        items - locale items
        Returns:
        the joined size of the language codes, country codes and script codes
      • getLanguageCodes

        public static java.lang.String[] getLanguageCodes()
        Returns:
        array of language codes (ISO_639_3_Alpha3 plus optional extensions)
      • getCountryCodes

        public static java.lang.String[] getCountryCodes()
        Returns:
        array of country codes (ISO_3166_1_Alpha2 plus optional extensions)
      • getCountryItems

        public static java.util.List<LocaleUtils.LocaleItem> getCountryItems()
        Returns:
        list of country items (ISO_3166_1_Alpha2 plus optional extensions)
      • getLanguageItems

        public static java.util.List<LocaleUtils.LocaleItem> getLanguageItems()
        Returns:
        list of language items (ISO_639_3_Alpha3 plus optional extensions)
      • getCountryItems

        public static java.util.List<LocaleUtils.LocaleItem> getCountryItems​(java.lang.String code)
        Parameters:
        code - constant like ISO_3166_1_Alpha2
        Returns:
        array of custom country items
      • getLanguageItems

        public static java.util.List<LocaleUtils.LocaleItem> getLanguageItems​(java.lang.String code)
        Parameters:
        code - constant like ISO_639_1_Alpha2
        Returns:
        array of custom language items
      • parseLanguageAndCountry

        public static java.lang.String[] parseLanguageAndCountry​(java.lang.String in)

        Parse language and country part from a string in the format <lang>delimiter<country>

        Parse language and country part from a string in the format <lang>[_- ]<country>.
        If the input matches the expected format, a String array is returned with first element set to language and second to country; otherwise null is returned.

        Parameters:
        in - a locale, e.g. "deu_DE"
        Returns:
        String array, first element is language, second is country or null
      • parseLanguageAndCountry

        public static java.lang.String[] parseLanguageAndCountry​(java.lang.String in,
                                                                 java.lang.String delimiter)

        Parse language and country part from a string in the format <lang>delimiter<country>

        Parse language and country part from a string in the format <lang>delimiter<country>.
        If the input matches the expected format, a String array is returned with first element set to language and second to country; otherwise null is returned.

        Parameters:
        in - a locale code, e.g. "deu_DE"
        delimiter - a delimiter, e.g. "_"
        Returns:
        String array, first element is language, second is country or null