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 booleanequals(Object object)OverridesObject.equals().String[]getExtendedLanguageSubtags()Gets the extended language subtags.String[]getExtensions()Gets the extensions.StringgetLanguage()Gets the language (primary language plus extended language subtags).StringgetPrimaryLanguage()Gets the primary language.StringgetPrivateUse()Gets the private use.StringgetRegion()Gets the region.StringgetScript()Gets the script.String[]getVariants()Gets the variants.inthashCode()OverridesObject.hashCode().static LangTagparse(String s)Parses the specified string representation of a language tag.voidsetExtensions(String... extensions)Sets the extensions.voidsetPrivateUse(String privateUse)Sets the private use.voidsetRegion(String region)Sets the region.voidsetScript(String script)Sets the script.voidsetVariants(String... variants)Sets the variants.StringtoString()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 benullif the subtags are sufficient to identify the language.languageSubtags- One or more extended language subtags, as three-letter ISO 639-3 codes.nullif none.- Throws:
LangTagException- If the primary or extended language syntax is invalid.
-
-
Method Detail
-
getLanguage
public String getLanguage()
Description copied from interface:ReadOnlyLangTagGets the language (primary language plus extended language subtags).See RFC 5646 section 2.2.1.
Examples:
en de zh-cmn cmn
- Specified by:
getLanguagein interfaceReadOnlyLangTag- Returns:
- The language, consisting of the primary and/or extended language subtags.
-
getPrimaryLanguage
public String getPrimaryLanguage()
Description copied from interface:ReadOnlyLangTagGets the primary language.See RFC 5646 section 2.2.1.
- Specified by:
getPrimaryLanguagein 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:ReadOnlyLangTagGets the extended language subtags.See RFC 5646 section 2.2.2.
- Specified by:
getExtendedLanguageSubtagsin interfaceReadOnlyLangTag- Returns:
- The extended language subtags, as three-letter ISO 639-3
codes.
nullif none.
-
getScript
public String getScript()
Description copied from interface:ReadOnlyLangTagGets the script.See RFC 5646 section 2.2.3.
- Specified by:
getScriptin interfaceReadOnlyLangTag- Returns:
- The script, as an ISO 15924 code, in canonical title case
format.
nullif 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.nullif not defined.- Throws:
LangTagException- If the script syntax is invalid.
-
getRegion
public String getRegion()
Description copied from interface:ReadOnlyLangTagGets the region.See RFC 5646 section 2.2.4.
- Specified by:
getRegionin interfaceReadOnlyLangTag- Returns:
- The region, as a two-letter ISO 3166-1 code or a three-digit
UN M.49 code.
nullif 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.nullif not defined.- Throws:
LangTagException- If the region syntax is invalid.
-
getVariants
public String[] getVariants()
Description copied from interface:ReadOnlyLangTagGets the variants.See RFC 5646 section 2.2.5.
- Specified by:
getVariantsin interfaceReadOnlyLangTag- Returns:
- The variants.
nullif not defined.
-
setVariants
public void setVariants(String... variants) throws LangTagException
Sets the variants.See RFC 5646 section 2.2.5.
- Parameters:
variants- The variants.nullif not defined.- Throws:
LangTagException- If the variant syntax is invalid.
-
getExtensions
public String[] getExtensions()
Description copied from interface:ReadOnlyLangTagGets the extensions.See RFC 5646 section 2.2.6.
- Specified by:
getExtensionsin interfaceReadOnlyLangTag- Returns:
- The extensions.
nullif not defined.
-
setExtensions
public void setExtensions(String... extensions) throws LangTagException
Sets the extensions.See RFC 5646 section 2.2.6.
- Parameters:
extensions- The extensions.nullif not defined.- Throws:
LangTagException- If the extension syntax is invalid.
-
getPrivateUse
public String getPrivateUse()
Description copied from interface:ReadOnlyLangTagGets the private use.See RFC 5646 section 2.2.7.
- Specified by:
getPrivateUsein interfaceReadOnlyLangTag- Returns:
- The private use.
nullif 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.nullif not defined.- Throws:
LangTagException- If the extension syntax is invalid.
-
toString
public String toString()
Description copied from interface:ReadOnlyLangTagReturns the canonical string representation of this language tag.- Specified by:
toStringin interfaceReadOnlyLangTag- Overrides:
toStringin 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.
nullif the string was empty ornull. - Throws:
LangTagException- If the string has invalid language tag syntax.
-
-