public class LangTag extends Object implements ReadOnlyLangTag
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 and Description |
---|
LangTag(String primaryLanguage)
Creates a new simple language tag.
|
LangTag(String primaryLanguage,
String... languageSubtags)
Creates a new extended language tag.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object object)
Overrides
Object.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()
Overrides
Object.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.
|
public LangTag(String primaryLanguage) throws LangTagException
Use for simple language tags such as "en" (English), "fr" (French) or "pt" (Portuguese).
primaryLanguage
- The primary language, as the shortest two or
three-letter ISO 639 code. Must not be
null
.LangTagException
- If the primary language syntax is invalid.public LangTag(String primaryLanguage, String... languageSubtags) throws LangTagException
Use for extended language tags such as "zh-cmn" (Mandarin Chinese) or "zh-yue" (Cantonese Chinese).
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.LangTagException
- If the primary or extended language syntax
is invalid.public String getLanguage()
ReadOnlyLangTag
See RFC 5646 section 2.2.1.
Examples:
en de zh-cmn cmn
getLanguage
in interface ReadOnlyLangTag
public String getPrimaryLanguage()
ReadOnlyLangTag
See RFC 5646 section 2.2.1.
getPrimaryLanguage
in interface ReadOnlyLangTag
public String[] getExtendedLanguageSubtags()
ReadOnlyLangTag
See RFC 5646 section 2.2.2.
getExtendedLanguageSubtags
in interface ReadOnlyLangTag
null
if none.public String getScript()
ReadOnlyLangTag
See RFC 5646 section 2.2.3.
getScript
in interface ReadOnlyLangTag
null
if not defined.public void setScript(String script) throws LangTagException
See RFC 5646 section 2.2.3.
script
- The script, as a four-letter ISO 15924 code.
null
if not defined.LangTagException
- If the script syntax is invalid.public String getRegion()
ReadOnlyLangTag
See RFC 5646 section 2.2.4.
getRegion
in interface ReadOnlyLangTag
null
if not defined.public void setRegion(String region) throws LangTagException
See RFC 5646 section 2.2.4.
region
- The region, as a two-letter ISO 3166-1 code or a three-
digit UN M.49 code. null
if not defined.LangTagException
- If the region syntax is invalid.public String[] getVariants()
ReadOnlyLangTag
See RFC 5646 section 2.2.5.
getVariants
in interface ReadOnlyLangTag
null
if not defined.public void setVariants(String... variants) throws LangTagException
See RFC 5646 section 2.2.5.
variants
- The variants. null
if not defined.LangTagException
- If the variant syntax is invalid.public String[] getExtensions()
ReadOnlyLangTag
See RFC 5646 section 2.2.6.
getExtensions
in interface ReadOnlyLangTag
null
if not defined.public void setExtensions(String... extensions) throws LangTagException
See RFC 5646 section 2.2.6.
extensions
- The extensions. null
if not defined.LangTagException
- If the extension syntax is invalid.public String getPrivateUse()
ReadOnlyLangTag
See RFC 5646 section 2.2.7.
getPrivateUse
in interface ReadOnlyLangTag
null
if not defined.public void setPrivateUse(String privateUse) throws LangTagException
See RFC 5646 section 2.2.7.
privateUse
- The private use. null
if not defined.LangTagException
- If the extension syntax is invalid.public String toString()
ReadOnlyLangTag
toString
in interface ReadOnlyLangTag
toString
in class Object
public int hashCode()
Object.hashCode()
.public static LangTag parse(String s) throws LangTagException
s
- The string to parse. May be null
.null
if the string was empty or
null
.LangTagException
- If the string has invalid language tag
syntax.Copyright © 2016 Connect2id Ltd.. All Rights Reserved.