-
Notifications
You must be signed in to change notification settings - Fork 3
/
locale.ts
45 lines (40 loc) · 1.17 KB
/
locale.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { parseLanguageTag, LanguageTag } from '@phensley/language-tag';
import { LanguageResolver } from './resolver';
const UNDEFINED = new LanguageTag();
/**
* Wrapper pairing an application's opaque locale identifier with a
* parsed and resolved language tag object.
*
* @alpha
*/
export interface Locale {
/**
* Application's own identifier for the locale, e.g. 'en_US', 'fr-CA', etc.
* We preserve this since applications may use it as a unique key to
* resolve translated messages, and may be forced to use a legacy
* identifier.
*/
readonly id: string;
/**
* Language tag that has been parsed and resolved. Parsing canonicalizes
* the subtags, while resolution includes substituting language and
* territory aliases and adding likely subtags.
*/
readonly tag: LanguageTag;
}
/**
* Merged declaration to add a parse() method to Locale interface.
*
* @alpha
*/
export class Locale {
static resolve(id: string): Locale {
let tag = parseLanguageTag(id);
if (tag.hasLanguage() || tag.hasScript() || tag.hasRegion()) {
tag = LanguageResolver.resolve(tag);
} else {
tag = UNDEFINED;
}
return { id, tag };
}
}