Class LocaleUtils

java.lang.Object
com.priint.pubserver.util.LocaleUtils

public class LocaleUtils extends Object
Utility class containing helper methods for language and country settings.
Since:
4.0.0
  • Field Details

    • CONFIG_FILE_PATH

      public static final String CONFIG_FILE_PATH
      Configuration file location for this class
      See Also:
    • LANGUAGE_NONE_EMPTY

      public static final 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:
    • LANGUAGE_IGNORE

      public static final 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:
    • LANGUAGE_RESERVE

      public static final 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:
    • COUNTRY_NONE_EMPTY

      public static final 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:
    • COUNTRY_IGNORE

      public static final 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:
    • COUNTRY_RESERVE

      public static final 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:
    • SCRIPT_NONE_EMPTY

      public static final 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:
    • SCRIPT_IGNORE

      public static final 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:
    • SCRIPT_RESERVE

      public static final 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:
    • ISO_3166_1_Alpha2

      public static final String ISO_3166_1_Alpha2
      A two uppercase letter code for countries.

      E.g. "DE" for Germany, "GB" for United Kingdom

      See: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

      Since:
      4.0.0
      See Also:
    • ISO_639_1_Alpha2

      public static final String ISO_639_1_Alpha2
      A two lowercase letter code for languages.

      E.g. "de" for German and "en" for English

      See: https://en.wikipedia.org/wiki/ISO_639

      Since:
      4.0.0
      See Also:
    • ISO_639_3_Alpha3

      public static final String ISO_639_3_Alpha3
      A three lowercase letter code for languages.

      E.g. "deu" for German, "eng" for English, "sgg" for Swiss-German Sign Language

      See: https://en.wikipedia.org/wiki/ISO_639

      Since:
      4.0.0
      See Also:
    • ISO_15924

      public static final String ISO_15924
      A four letter code for script (writing system) (in title case).

      E.g. "Latn" for Latin and "Cyrl" for Cyrillic

      See: https://en.wikipedia.org/wiki/ISO_15924

      Since:
      4.0.0
      See Also:
    • languageCodes

      protected static final Map<String,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 Map<String,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 Map<String,String> scriptCodes
      Cache for script codes.
  • Method Details

    • getPubServerLanguage

      public static String getPubServerLanguage(String language, 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
      IllegalArgumentException - if no matching language key could be found
      Since:
      4.0.0
    • getCustomLanguage

      public static String getCustomLanguage(String language, 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
      IllegalArgumentException - if no matching language key could be found
      Since:
      4.0.0
    • getPubServerCountry

      public static String getPubServerCountry(String country, 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
      IllegalArgumentException - if no matching country key could be found
      Since:
      4.0.0
    • getCustomCountry

      public static String getCustomCountry(String country, 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
      IllegalArgumentException - if no matching country key could be found
      Since:
      4.0.0
    • getPubServerScript

      public static String getPubServerScript(String script, 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
      IllegalArgumentException - if no matching script key could be found
      Since:
      4.0.0
    • getCustomScript

      public static String getCustomScript(String script, 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
      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 String[] getLanguageCodes()
      Returns:
      array of language codes (ISO_639_3_Alpha3 plus optional extensions)
    • getCountryCodes

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

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

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

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

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

      public static String[] parseLanguageAndCountry(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 String[] parseLanguageAndCountry(String in, 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