LanguageTag
An object representing a resolved BCP 47 language tag.
new
Constructs a language tag object directly.
Note: This type's constuctor should not be used in most cases. It does basic canonicalization of each subtag but no validation, so it is possible to construct invalid language tags. Use with caution.
Recommendation is to use CLDRFramework.parseLanguageTag or CLDRFramework.resolveLocale instead.
Syntax
new LanguageTag( language?, script?, region?, variant?, extensions?, privateUse? )
Properties
language?: string
- ISO-639 language code
script?: string
- ISO 15924 script code
region?: string
- ISO 3166-1 or UN M.49 code
variant?: string
- IETF registered variant
extensions?: { [x: string]: string[] }
- Mapping of extension prefixes to subtags
privateUse?: string
- Private use subtags, as a string
Example
import { LanguageTag } from '@phensley/cldr';
const tag = new LanguageTag(undefined, 'Latn', undefined);
log(tag.compact());
log(tag.expanded());
und-Latn und-Latn-ZZ
compact
Returns the most compact representation of the language tag, omitting any undefined subtags while ensuring the structure of the language tag is valid.
Syntax
compact(): string
Example
import { LanguageTag } from '@phensley/cldr';
const tag = new LanguageTag(undefined, undefined, 'US');
log(tag.compact());
und-US
expanded
Returns the expanded representation of the language tag, including the string representations for undefined subtags. The result always includes language, script, and region subtags.
Syntax
expanded(): string
Example
import { LanguageTag } from '@phensley/cldr';
const tag = new LanguageTag(undefined, undefined, 'US');
log(tag.expanded());
und-Zzzz-US
extensions
Returns a map containing the extension subtags.
Syntax
extensions(): { [x: string]: string[] }
Example
import { LanguageTag } from '@phensley/cldr';
const tag = new LanguageTag('en', '', 'US', '', { u: ['ca-gregory'] });
log(tag.expanded());
log(tag.extensions());
en-Zzzz-US-u-ca-gregory { u: [ 'ca-gregory' ] }
hasLanguage
Indicates the language subtag is defined.
Syntax
hasLanguage(): boolean
Example
import { LanguageTag } from '@phensley/cldr';
let tag = new LanguageTag('und');
log(tag.hasLanguage());
tag = new LanguageTag();
log(tag.hasLanguage());
tag = new LanguageTag('zh');
log(tag.hasLanguage());
false false true
hasRegion
Indicates the region subtag is defined.
Syntax
hasRegion(): boolean
hasScript
Indicates the script subtag is defined.
Syntax
hasScript(): boolean
language
Returns the language subtag.
Syntax
language(): string
Example
import { LanguageTag } from '@phensley/cldr';
let tag = new LanguageTag();
log(tag.language());
tag = new LanguageTag('en');
log(tag.language());
und en
privateUse
Returns the private use subtags.
Syntax
privateUse(): string
Example
import { LanguageTag } from '@phensley/cldr';
const tag = new LanguageTag('en', 'Latn', 'US', undefined, undefined, 'x-nothing');
log(tag.expanded());
log(tag.privateUse());
en-Latn-US-x-nothing x-nothing
region
Returns the region subtag.
Syntax
region(): string
Example
import { LanguageTag } from '@phensley/cldr';
let tag = new LanguageTag('en', 'Latn');
log(tag.region());
tag = new LanguageTag('en', 'Latn', 'US');
log(tag.region());
ZZ US
script
Returns the script subtag.
Syntax
script(): string
Example
import { LanguageTag } from '@phensley/cldr';
let tag = new LanguageTag('en');
log(tag.script());
tag = new LanguageTag('en', 'latn');
log(tag.script());
Zzzz Latn
toString
Equivalent of compact().