Class LocaleId

    • Field Detail

      • EMPTY

        public static final LocaleId EMPTY
        An empty locale.
      • AUTODETECT

        public static final LocaleId AUTODETECT
        Same as EMPTY, but reminds the user that some components use EMPTY as a setting to enable locale auto detection. e.g., XlifffFilter, TableFilter and TmxFilter
      • ARABIC

        public static final LocaleId ARABIC
        LocaleId constant for "ar".
      • CHINA_CHINESE

        public static final LocaleId CHINA_CHINESE
        LocaleId constant for "zh-cn".
      • TAIWAN_CHINESE

        public static final LocaleId TAIWAN_CHINESE
        LocaleId constant for "zh-tw".
      • ENGLISH

        public static final LocaleId ENGLISH
        LocaleId constant for "en".
      • US_ENGLISH

        public static final LocaleId US_ENGLISH
        LocaleId constant for "en-US".
      • FRENCH

        public static final LocaleId FRENCH
        LocaleId constant for "fr".
      • GERMAN

        public static final LocaleId GERMAN
        LocaleId constant for "de".
      • HEBREW

        public static final LocaleId HEBREW
        LocaleId constant for "he".
      • ITALIAN

        public static final LocaleId ITALIAN
        LocaleId constant for "it".
      • JAPANESE

        public static final LocaleId JAPANESE
        LocaleId constant for "ja".
      • KOREAN

        public static final LocaleId KOREAN
        LocaleId constant for "ko".
      • PORTUGUESE

        public static final LocaleId PORTUGUESE
        LocaleId constant for "pt".
      • RUSSIAN

        public static final LocaleId RUSSIAN
        LocaleId constant for "ru".
      • SPANISH

        public static final LocaleId SPANISH
        LocaleId constant for "es".
      • POSIX_PATTERN

        public static final Pattern POSIX_PATTERN
      • ISO_CODE_PATTERN

        public static final Pattern ISO_CODE_PATTERN
    • Constructor Detail

      • LocaleId

        public LocaleId​(String language)
        Creates a new LocaleId for a given language code. This constructor does not take a locale identifier as argument, just a language identifier. Use LocaleId(String) to create a new LocaleId from a locale identifier.
        Parameters:
        language - the language code (e.g. "de" for German).
        Throws:
        IllegalArgumentException - if the argument in invalid.
      • LocaleId

        public LocaleId​(String language,
                        String region)
        Creates a new LocaleId for a given language code and region code.
        Parameters:
        language - the language code (e.g. "es" for Spanish).
        region - the region code (e.g. "es" for Spain or "005" for South America. This parameter is ignored if null or empty.
        Throws:
        IllegalArgumentException - if the argument in invalid.
      • LocaleId

        public LocaleId​(String language,
                        String region,
                        String userPart)
        Creates a new LocaleId for a given language code, region code, and a user part.
        Parameters:
        language - the language code (e.g. "es" for Spanish).
        region - the region code (e.g. "es" for Spain or "005" for South America.
        userPart - the user part of locale. The latter two parameters are ignored if null or empty.
        Throws:
        IllegalArgumentException - if the argument in invalid.
      • LocaleId

        public LocaleId​(com.ibm.icu.util.ULocale uLoc)
        Creates a new LocaleId for the given ICU Locale.
        Parameters:
        uLoc - the ICU Locale object to use.
        Throws:
        IllegalArgumentException - if the argument in invalid.
      • LocaleId

        public LocaleId​(Locale loc)
        Creates a new LocaleId for the given Java Locale.
        Parameters:
        loc - the Java Locale object to use.
        Throws:
        IllegalArgumentException - if the argument in invalid.
    • Method Detail

      • replaceVariables

        public static String replaceVariables​(String input,
                                              LocaleId srcLoc,
                                              LocaleId trgLoc)
        Replaces the locale/language variables in a given input string by their runtime values. If one of the locale passed is null, its corresponding variables are replaced by an empty string.
        Parameters:
        input - the string with the variables.
        srcLoc - the source locale code (can be null).
        trgLoc - the target locale code (can be null).
        Returns:
        the modified string.
      • replaceVariables

        public static String replaceVariables​(String input,
                                              String srcLoc,
                                              String trgLoc)
        Replaces the locale/language variables in a given input string by their runtime values. If one of the locale passed is null, its corresponding variables are replaced by an empty string.
        Parameters:
        input - the string with the variables.
        srcLoc - the source locale code (can be null).
        trgLoc - the target locale code (can be null).
        Returns:
        the modified string.
      • toString

        public String toString()
        Gets the string representation of the LocaleId.
        Overrides:
        toString in class Object
        Returns:
        the string representation of the LocaleId.
      • clone

        public LocaleId clone()
        Returns a clone of this LocaleId. Because LocaleId are immutable objects this method returns the same LocaleId.
        Overrides:
        clone in class Object
        Returns:
        the same LocaleId (because it is immutable).
      • hashCode

        public int hashCode()
        Returns a hash code value for this LocaleId.
        Overrides:
        hashCode in class Object
        Returns:
        the hash code value for this LocaleId.
      • equalToString

        public boolean equalToString​(String str)
        Convenience method to compare a LocaleId to a String

        Conflating this functionality in equals was error prone.

        Parameters:
        str - string to compare to
        Returns:
        true if equal, false if not equal
      • equals

        public boolean equals​(Object arg)
        Indicates if a given object is equal to this localeId object.
        Overrides:
        equals in class Object
        Parameters:
        arg - the object to compare. This can be a LocaleId object or a string. Any other object will always return false. If the parameter is a string it is normalized before being compared.
        Returns:
        true if the parameter is the same object, or if it is a LocaleId with the same identifier, or if it is a string equals to the identifier. False otherwise.
      • compareTo

        public int compareTo​(Object arg)
        Compares this LocaleId with a given object.
        Specified by:
        compareTo in interface Comparable<Object>
        Parameters:
        arg - the object to compare. If the parameter is a string it is normalized before being compared.
        Returns:
        1 if the parameter is null. If the parameter is a LocaleId or a string, the return is the same as the return of a comparison between the identifier of this LocaleId and the string representation of the argument. Otherwise the return is 1;
      • fromString

        public static LocaleId fromString​(String locId)
        Creates a new LocaleId from a locale identifier (and validate it). Calling this method is the same as calling new LocaleId(locId, true);
        Parameters:
        locId - the locale identifier to use (it will be normalized).
        Returns:
        a new localeId object from the given identifier.
        Throws:
        IllegalArgumentException - if the argument is invalid.
      • fromPOSIXLocale

        public static LocaleId fromPOSIXLocale​(String locId)
        Creates a new LocaleId from a POSIX locale identifier.
        Parameters:
        locId - the POSIX locale identifier (e.g. "de-at.UTF-8@EURO")
        Returns:
        a new LocaleId or null if an error occurred.
        Throws:
        IllegalArgumentException - if the argument is invalid.
      • toPOSIXLocaleId

        public String toPOSIXLocaleId()
        Gets a POSIX locale identifier for this LocaleId. For example: "af-za" returns "af_ZA".
        Returns:
        the corresponding POSIX locale identifier for this LocaleId.
      • fromBCP47

        public static LocaleId fromBCP47​(String langtag,
                                         boolean strict)
        Creates a new LocaleId from a BCP-47 language tag.

        If strict is true the language tag must be well-formed (see ICU4J ULocale) or an exception is thrown. It rejects language tags that use '_' instead of '-', or have ill-formed "parts".

        If strict is false then it accepts both '_' and '-', converts invalid language id to "und", and discards ill-formed and following portions of the tag.

        Parameters:
        langtag - the language tag to use (e.g. "fr-CA")
        strict - the language tag parsing is stricter
        Returns:
        a new LocaleId.
        Throws:
        IllegalArgumentException - if the argument in invalid.
        See Also:
        BCP 47 Conformance
      • toBCP47

        public String toBCP47()
        Gets the BCP-47 language tag for this LocaleId.
        Returns:
        the BCP-47 language tag for the given LocaleId.
      • toJavaLocale

        public Locale toJavaLocale()
        Creates a new Java Locale object from this LocaleId.
        Returns:
        a new Java Locale object based on the best match for the given LocaleId, or null if an error occurred.
      • toIcuLocale

        public com.ibm.icu.util.ULocale toIcuLocale()
        Gets the ICU ULocale object wrapped by this LocaleId.
        Returns:
        ICU ULocale object wrapped by the given LocaleId.
      • getLanguage

        public String getLanguage()
        Gets the language code for this LocaleId.
        Returns:
        the language code.
      • getRegion

        public String getRegion()
        Gets the region code for this LocaleId.
        Returns:
        the region code or null if there is none or if an error occurs.
      • getScript

        public String getScript()
        Gets the script code for this LocaleId.
        Returns:
        the script code or null if there is none or if an error occurs.
      • getVariant

        public String getVariant()
        Gets the variant for this LocaleId.
        Returns:
        the variant or null if there is none or if an error occurs.
      • getUserPart

        public String getUserPart()
        Gets the user part of this LocaleId.
        Returns:
        the user part or null if there is none or if an error occurs.
      • sameLanguageAs

        public boolean sameLanguageAs​(LocaleId other)
        Indicates if the language of a given LocaleId is the same as the one of this LocaleId. For example: "en" and "en-us" returns true, "es-es" and "ca-es" return false.
        Parameters:
        other - the LocaleId object to compare.
        Returns:
        true if the languages of two given LocaleIds are the same.
      • sameLanguageAs

        public boolean sameLanguageAs​(String langCode)
        Indicates if a given string has the same language as the one of this LocaleId. For example: "en" and "en-us" returns true, "es-es" and "ca-es" return false.
        Parameters:
        langCode - the string to compare.
        Returns:
        true if the languages of both objects are the same.
      • sameRegionAs

        public boolean sameRegionAs​(LocaleId other)
        Indicates if the region of a given LocaleId is the same as the one of this LocaleId. For example: "es-us" and "en-us" returns true, "es-es" and "es-us" return false.
        Parameters:
        other - the LocaleId object to compare.
        Returns:
        true if the region parts of two given LocaleIds are the same.
      • sameRegionAs

        public boolean sameRegionAs​(String locId)
        Indicates if a given string has the same region as the one of this LocaleId. For example: "es-us" and "en-us" returns true, "es-es" and "es-us" return false.
        Parameters:
        locId - the string to compare.
        Returns:
        true if the region parts of both objects are the same.
      • sameUserPartAs

        public boolean sameUserPartAs​(LocaleId other)
        Indicates if the user part of a given LocaleId is the same as the one of this LocaleId. For example: "es-us-x-win" and "en_us@win" returns true, "es_us@mac" and "es_us@ats" return false.
        Parameters:
        other - the LocaleId object to compare.
        Returns:
        true if the region parts of two given LocaleIds are the same.
      • sameUserPartAs

        public boolean sameUserPartAs​(String langCode)
        Indicates if a given string has the same user part as the one of this LocaleId. For example: "es-us-x-win" and "en-us@win" returns true, "es-us@mac" and "es-us@ats" return false.
        Parameters:
        langCode - the string to compare.
        Returns:
        true if the region parts of both objects are the same.
      • getAvailableLocales

        public static LocaleId[] getAvailableLocales()
        Gets an array of the LocaleId objects for all the Java locales installed on the system.
        Returns:
        an array of the LocaleId objects for all the available Java locales.
      • isBidirectional

        public static boolean isBidirectional​(LocaleId locId)
        Indicates if a given locale usually uses a bi-directional script.

        Note that this is not perfect as some languages use several scripts.

        Parameters:
        locId - the locale to check.
        Returns:
        true if the locale uses a bi-directional script.
      • hasCharactersAsNumeralSeparators

        public static boolean hasCharactersAsNumeralSeparators​(LocaleId localeId)
        Checks whether the specified locale has characters as numeral separators.
        Parameters:
        localeId - The locale to check against
        Returns:
        true - if the specified locale has characters as numeral separators false - otherwise
      • convertToLocaleIds

        public static List<LocaleId> convertToLocaleIds​(List<String> languageCodes)
        Converts a list of language codes into LocaleIds
        Parameters:
        languageCodes - the list of codes.
        Returns:
        the list of LocaleId objects created.