Class LangTag
- java.lang.Object
-
- com.nimbusds.langtag.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.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object object)
OverridesObject.equals()
.String[]
getExtendedLanguageSubtags()
Gets the extended language subtags.String[]
getExtensions()
Gets the extensions.String
getLanguage()
Gets the language (primary language plus extended language subtags).String
getPrimaryLanguage()
Gets the primary language.String
getPrivateUse()
Gets the private use.String
getRegion()
Gets the region.String
getScript()
Gets the script.String[]
getVariants()
Gets the variants.int
hashCode()
OverridesObject.hashCode()
.static LangTag
parse(String s)
Parses the specified string representation of a language tag.void
setExtensions(String... extensions)
Sets the extensions.void
setPrivateUse(String privateUse)
Sets the private use.void
setRegion(String region)
Sets the region.void
setScript(String script)
Sets the script.void
setVariants(String... variants)
Sets the variants.String
toString()
Returns the canonical string representation of this language tag.
-
-
-
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 benull
.- 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 benull
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 interfaceReadOnlyLangTag
- Returns:
- The language, consisting of the primary and/or extended language subtags.
-
getPrimaryLanguage
public String getPrimaryLanguage()
Description copied from interface:ReadOnlyLangTag
Gets the primary language.See RFC 5646 section 2.2.1.
- Specified by:
getPrimaryLanguage
in interfaceReadOnlyLangTag
- Returns:
- The primary language, as a two or three-letter ISO 639 code, in canonical lower case format.
-
getExtendedLanguageSubtags
public String[] getExtendedLanguageSubtags()
Description copied from interface:ReadOnlyLangTag
Gets the extended language subtags.See RFC 5646 section 2.2.2.
- Specified by:
getExtendedLanguageSubtags
in interfaceReadOnlyLangTag
- Returns:
- The extended language subtags, as three-letter ISO 639-3
codes.
null
if none.
-
getScript
public String getScript()
Description copied from interface:ReadOnlyLangTag
Gets the script.See RFC 5646 section 2.2.3.
- Specified by:
getScript
in interfaceReadOnlyLangTag
- 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 interfaceReadOnlyLangTag
- 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.
-
getVariants
public String[] getVariants()
Description copied from interface:ReadOnlyLangTag
Gets the variants.See RFC 5646 section 2.2.5.
- Specified by:
getVariants
in interfaceReadOnlyLangTag
- Returns:
- The variants.
null
if not defined.
-
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.
-
getExtensions
public String[] getExtensions()
Description copied from interface:ReadOnlyLangTag
Gets the extensions.See RFC 5646 section 2.2.6.
- Specified by:
getExtensions
in interfaceReadOnlyLangTag
- Returns:
- The extensions.
null
if not defined.
-
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.
-
getPrivateUse
public String getPrivateUse()
Description copied from interface:ReadOnlyLangTag
Gets the private use.See RFC 5646 section 2.2.7.
- Specified by:
getPrivateUse
in interfaceReadOnlyLangTag
- Returns:
- The private use.
null
if not defined.
-
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.
-
toString
public String toString()
Description copied from interface:ReadOnlyLangTag
Returns the canonical string representation of this language tag.- Specified by:
toString
in interfaceReadOnlyLangTag
- Overrides:
toString
in classObject
- Returns:
- The canonical string representation.
-
hashCode
public int hashCode()
OverridesObject.hashCode()
.
-
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 benull
.- Returns:
- The language tag.
null
if the string was empty ornull
. - Throws:
LangTagException
- If the string has invalid language tag syntax.
-
-