Class LangTag

  • All Implemented Interfaces:
    ReadOnlyLangTag

    public class LangTag
    extends Object
    implements ReadOnlyLangTag
    Language tag according to RFC 5646.

    Supports normal language tags. Special private language tags beginning with "x" and grandfathered tags beginning with "i" are not supported.

    To construct a new language tag from scratch:

     // English as used in the United States
     LangTag tag = new LangTag("en");
     tag.setRegion("US");
    
     // Returns "en-US"
     tag.toString();
     

    To parse a language tag:

     // Chinese, Mandarin, Simplified script, as used in China
     LangTag tag = LangTag.parse("zh-cmn-Hans-CN");
    
     // Returns "zh"
     tag.getPrimaryLanguage();
    
     // Returns "cmn"
     tag.getExtendedLanguageSubtags()[0];
    
     // Returns "zh-cmn"
     tag.getLanguage();
    
     // Returns "Hans"
     tag.getScript();
    
     // Returns "CN"
     tag.getRegion();
     

    See RFC 5646.

    • Constructor Detail

      • LangTag

        public LangTag​(String primaryLanguage)
                throws LangTagException
        Creates a new simple language tag.

        Use for simple language tags such as "en" (English), "fr" (French) or "pt" (Portuguese).

        Parameters:
        primaryLanguage - The primary language, as the shortest two or three-letter ISO 639 code. Must not be null.
        Throws:
        LangTagException - If the primary language syntax is invalid.
      • LangTag

        public LangTag​(String primaryLanguage,
                       String... languageSubtags)
                throws LangTagException
        Creates a new extended language tag.

        Use for extended language tags such as "zh-cmn" (Mandarin Chinese) or "zh-yue" (Cantonese Chinese).

        Parameters:
        primaryLanguage - The primary language, as the shortest two or three-letter ISO 639 code. May be null if the subtags are sufficient to identify the language.
        languageSubtags - One or more extended language subtags, as three-letter ISO 639-3 codes. null if none.
        Throws:
        LangTagException - If the primary or extended language syntax is invalid.
    • Method Detail

      • getLanguage

        public String getLanguage()
        Description copied from interface: ReadOnlyLangTag
        Gets the language (primary language plus extended language subtags).

        See RFC 5646 section 2.2.1.

        Examples:

         en
         de
         zh-cmn
         cmn
         
        Specified by:
        getLanguage in interface ReadOnlyLangTag
        Returns:
        The language, consisting of the primary and/or extended language subtags.
      • getScript

        public String getScript()
        Description copied from interface: ReadOnlyLangTag
        Gets the script.

        See RFC 5646 section 2.2.3.

        Specified by:
        getScript in interface ReadOnlyLangTag
        Returns:
        The script, as an ISO 15924 code, in canonical title case format. null if not defined.
      • setScript

        public void setScript​(String script)
                       throws LangTagException
        Sets the script.

        See RFC 5646 section 2.2.3.

        Parameters:
        script - The script, as a four-letter ISO 15924 code. null if not defined.
        Throws:
        LangTagException - If the script syntax is invalid.
      • getRegion

        public String getRegion()
        Description copied from interface: ReadOnlyLangTag
        Gets the region.

        See RFC 5646 section 2.2.4.

        Specified by:
        getRegion in interface ReadOnlyLangTag
        Returns:
        The region, as a two-letter ISO 3166-1 code or a three-digit UN M.49 code. null if not defined.
      • setRegion

        public void setRegion​(String region)
                       throws LangTagException
        Sets the region.

        See RFC 5646 section 2.2.4.

        Parameters:
        region - The region, as a two-letter ISO 3166-1 code or a three- digit UN M.49 code. null if not defined.
        Throws:
        LangTagException - If the region syntax is invalid.
      • setVariants

        public void setVariants​(String... variants)
                         throws LangTagException
        Sets the variants.

        See RFC 5646 section 2.2.5.

        Parameters:
        variants - The variants. null if not defined.
        Throws:
        LangTagException - If the variant syntax is invalid.
      • setExtensions

        public void setExtensions​(String... extensions)
                           throws LangTagException
        Sets the extensions.

        See RFC 5646 section 2.2.6.

        Parameters:
        extensions - The extensions. null if not defined.
        Throws:
        LangTagException - If the extension syntax is invalid.
      • setPrivateUse

        public void setPrivateUse​(String privateUse)
                           throws LangTagException
        Sets the private use.

        See RFC 5646 section 2.2.7.

        Parameters:
        privateUse - The private use. null if not defined.
        Throws:
        LangTagException - If the extension syntax is invalid.
      • hashCode

        public int hashCode()
        Overrides Object.hashCode().
        Overrides:
        hashCode in class Object
        Returns:
        The object hash code.
      • equals

        public boolean equals​(Object object)
        Overrides Object.equals().
        Overrides:
        equals in class Object
        Parameters:
        object - The object to compare to.
        Returns:
        true if the objects have the same value, otherwise false.
      • parse

        public static LangTag parse​(String s)
                             throws LangTagException
        Parses the specified string representation of a language tag.
        Parameters:
        s - The string to parse. May be null.
        Returns:
        The language tag. null if the string was empty or null.
        Throws:
        LangTagException - If the string has invalid language tag syntax.