@pie-qti/i18n 1.0.0-beta.0 → 1.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +104 -55
- package/dist/core/I18n.d.ts +149 -0
- package/dist/core/I18n.js +348 -0
- package/dist/core/I18n.js.map +1 -0
- package/dist/core/I18nProvider.d.ts +91 -0
- package/dist/core/I18nProvider.js +9 -0
- package/dist/core/I18nProvider.js.map +1 -0
- package/dist/core/accessible-text.d.ts +36 -0
- package/dist/core/accessible-text.js +54 -0
- package/dist/core/accessible-text.js.map +1 -0
- package/dist/core/direction.d.ts +39 -0
- package/dist/core/direction.js +96 -0
- package/dist/core/direction.js.map +1 -0
- package/dist/core/language-tags.d.ts +47 -0
- package/dist/core/language-tags.js +91 -0
- package/dist/core/language-tags.js.map +1 -0
- package/dist/core/negotiation.d.ts +41 -0
- package/dist/core/negotiation.js +71 -0
- package/dist/core/negotiation.js.map +1 -0
- package/dist/core/numbering.d.ts +31 -0
- package/dist/core/numbering.js +88 -0
- package/dist/core/numbering.js.map +1 -0
- package/dist/core/pseudo.d.ts +33 -0
- package/dist/core/pseudo.js +44 -0
- package/dist/core/pseudo.js.map +1 -0
- package/dist/core/types.d.ts +29 -0
- package/dist/core/types.js +5 -0
- package/dist/core/types.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/locales/ar.d.ts +926 -0
- package/dist/locales/ar.js +963 -0
- package/dist/locales/ar.js.map +1 -0
- package/dist/locales/en.d.ts +870 -0
- package/dist/locales/en.js +909 -0
- package/dist/locales/en.js.map +1 -0
- package/dist/locales/es.d.ts +874 -0
- package/dist/locales/es.js +901 -0
- package/dist/locales/es.js.map +1 -0
- package/dist/locales/fr.d.ts +873 -0
- package/dist/locales/fr.js +900 -0
- package/dist/locales/fr.js.map +1 -0
- package/dist/locales/nl.d.ts +872 -0
- package/dist/locales/nl.js +898 -0
- package/dist/locales/nl.js.map +1 -0
- package/dist/locales/ro.d.ts +873 -0
- package/dist/locales/ro.js +900 -0
- package/dist/locales/ro.js.map +1 -0
- package/dist/locales/th.d.ts +872 -0
- package/dist/locales/th.js +899 -0
- package/dist/locales/th.js.map +1 -0
- package/dist/locales/zh-Hans.d.ts +870 -0
- package/dist/locales/zh-Hans.js +905 -0
- package/dist/locales/zh-Hans.js.map +1 -0
- package/dist/providers/SvelteI18nProvider.d.ts +58 -0
- package/dist/providers/SvelteI18nProvider.js +102 -0
- package/dist/providers/SvelteI18nProvider.js.map +1 -0
- package/package.json +5 -3
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writing direction and the `lang`/`dir` pair a player writes on its roots.
|
|
3
|
+
*
|
|
4
|
+
* A player writes two pairs on two channels. Chrome roots (toolbars, navigation,
|
|
5
|
+
* the shell) take the interface locale's; content roots (the item body, a
|
|
6
|
+
* passage) take the content language's, which the item declares with
|
|
7
|
+
* `xml:lang`. Content that declares no language gets no attributes and inherits
|
|
8
|
+
* the chrome's. Mirrors `stampInterfaceLocale` and `stampContentLanguage` in
|
|
9
|
+
* pie-players' `players-shared`, returning attributes for a template to spread
|
|
10
|
+
* where those write them onto an element.
|
|
11
|
+
*/
|
|
12
|
+
import { normalizeLanguageTag } from './language-tags.js';
|
|
13
|
+
/**
|
|
14
|
+
* Right-to-left primary language subtags.
|
|
15
|
+
*
|
|
16
|
+
* Only consulted when the engine has no `Intl.Locale` text info: it is the
|
|
17
|
+
* authoritative source but shipped late in Safari, and an assessment rendered
|
|
18
|
+
* left-to-right for an Arabic reader is not a graceful degradation.
|
|
19
|
+
*/
|
|
20
|
+
const RTL_LANGUAGES = new Set([
|
|
21
|
+
'ar', // Arabic
|
|
22
|
+
'ckb', // Central Kurdish
|
|
23
|
+
'dv', // Dhivehi
|
|
24
|
+
'fa', // Persian
|
|
25
|
+
'he',
|
|
26
|
+
'iw', // Hebrew (current and legacy subtags)
|
|
27
|
+
'ks', // Kashmiri
|
|
28
|
+
'ku', // Kurdish
|
|
29
|
+
'nqo', // N'Ko
|
|
30
|
+
'ps', // Pashto
|
|
31
|
+
'sd', // Sindhi
|
|
32
|
+
'syr', // Syriac
|
|
33
|
+
'ug', // Uyghur
|
|
34
|
+
'ur', // Urdu
|
|
35
|
+
'yi', // Yiddish
|
|
36
|
+
]);
|
|
37
|
+
/** Writing direction of a language tag; `ltr` for anything unrecognised. */
|
|
38
|
+
export function languageDirection(tag) {
|
|
39
|
+
const normalized = normalizeLanguageTag(tag);
|
|
40
|
+
if (!normalized)
|
|
41
|
+
return 'ltr';
|
|
42
|
+
try {
|
|
43
|
+
const locale = new Intl.Locale(normalized);
|
|
44
|
+
const direction = (locale.getTextInfo?.() ?? locale.textInfo)?.direction;
|
|
45
|
+
if (direction === 'rtl' || direction === 'ltr')
|
|
46
|
+
return direction;
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// Not a tag Intl.Locale accepts; the subtag check below still answers.
|
|
50
|
+
}
|
|
51
|
+
return RTL_LANGUAGES.has(normalized.split('-')[0] ?? '') ? 'rtl' : 'ltr';
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The canonical form of a usable language tag, or `undefined` for anything a
|
|
55
|
+
* language in use would not be: `es_MX` becomes `es-MX`, `NL-nl` becomes
|
|
56
|
+
* `nl-NL`, and `Spanish` or an empty string are not tags. Unicode extensions
|
|
57
|
+
* are dropped (`ar-EG-u-nu-latn` becomes `ar-EG`): they steer formatting and
|
|
58
|
+
* have no place in a `lang` attribute.
|
|
59
|
+
*/
|
|
60
|
+
export function canonicalLanguageTag(tag) {
|
|
61
|
+
const candidate = (tag ?? '').trim().replace(/_/g, '-').split(/[.@]/)[0] ?? '';
|
|
62
|
+
// Intl accepts any 5-8 letter primary subtag, `Spanish` included; a language
|
|
63
|
+
// in use has an ISO 639 code of two or three letters.
|
|
64
|
+
if (!/^[a-z]{2,3}(-|$)/i.test(candidate))
|
|
65
|
+
return undefined;
|
|
66
|
+
try {
|
|
67
|
+
const canonical = Intl.getCanonicalLocales(candidate)[0];
|
|
68
|
+
return canonical ? new Intl.Locale(canonical).baseName : undefined;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Chrome attributes: the provider's locale and direction, or `undefined`
|
|
76
|
+
* without a provider, so a host that passes none keeps what the page has.
|
|
77
|
+
*/
|
|
78
|
+
export function interfaceLocaleAttributes(provider) {
|
|
79
|
+
if (!provider)
|
|
80
|
+
return undefined;
|
|
81
|
+
const locale = provider.getLocale();
|
|
82
|
+
return {
|
|
83
|
+
lang: canonicalLanguageTag(locale) ?? locale,
|
|
84
|
+
dir: provider.getDirection?.() ?? languageDirection(locale),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Content attributes for a declared content language, or `undefined` when the
|
|
89
|
+
* content declares none or the declaration is not a language tag. Nothing is
|
|
90
|
+
* invented: undeclared content inherits its context's attributes.
|
|
91
|
+
*/
|
|
92
|
+
export function contentLanguageAttributes(language) {
|
|
93
|
+
const lang = canonicalLanguageTag(language);
|
|
94
|
+
return lang ? { lang, dir: languageDirection(lang) } : undefined;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=direction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"direction.js","sourceRoot":"","sources":["../../src/core/direction.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAU1D;;;;;;GAMG;AACH,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC;IAC7B,IAAI,EAAE,SAAS;IACf,KAAK,EAAE,kBAAkB;IACzB,IAAI,EAAE,UAAU;IAChB,IAAI,EAAE,UAAU;IAChB,IAAI;IACJ,IAAI,EAAE,sCAAsC;IAC5C,IAAI,EAA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C;IAC3D,IAAI,CAAC;QACJ,MAAM,SAAS,GAAG,IAAI,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;QACzD,OAAO,SAAS,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;IACpE,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,SAAS,CAAC;IAClB,CAAC;AACF,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,yBAAyB,CACxC,QAA6E;IAE7E,IAAI,CAAC,QAAQ;QAAE,OAAO,SAAS,CAAC;IAChC,MAAM,MAAM,GAAG,QAAQ,CAAC,SAAS,EAAE,CAAC;IACpC,OAAO;QACN,IAAI,EAAE,oBAAoB,CAAC,MAAM,CAAC,IAAI,MAAM;QAC5C,GAAG,EAAE,QAAQ,CAAC,YAAY,EAAE,EAAE,IAAI,iBAAiB,CAAC,MAAM,CAAC;KAC3D,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,yBAAyB,CAAC,QAAmC;IAC5E,MAAM,IAAI,GAAG,oBAAoB,CAAC,QAAQ,CAAC,CAAC;IAC5C,OAAO,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,iBAAiB,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;AAClE,CAAC","sourcesContent":["/**\n * Writing direction and the `lang`/`dir` pair a player writes on its roots.\n *\n * A player writes two pairs on two channels. Chrome roots (toolbars, navigation,\n * the shell) take the interface locale's; content roots (the item body, a\n * passage) take the content language's, which the item declares with\n * `xml:lang`. Content that declares no language gets no attributes and inherits\n * the chrome's. Mirrors `stampInterfaceLocale` and `stampContentLanguage` in\n * pie-players' `players-shared`, returning attributes for a template to spread\n * where those write them onto an element.\n */\n\nimport type { I18nProvider } from './I18nProvider.js';\nimport { normalizeLanguageTag } from './language-tags.js';\n\nexport type TextDirection = 'ltr' | 'rtl';\n\n/** `lang` and `dir` for one root. */\nexport interface LanguageAttributes {\n\tlang: string;\n\tdir: TextDirection;\n}\n\n/**\n * Right-to-left primary language subtags.\n *\n * Only consulted when the engine has no `Intl.Locale` text info: it is the\n * authoritative source but shipped late in Safari, and an assessment rendered\n * left-to-right for an Arabic reader is not a graceful degradation.\n */\nconst RTL_LANGUAGES = new Set([\n\t'ar', // Arabic\n\t'ckb', // Central Kurdish\n\t'dv', // Dhivehi\n\t'fa', // Persian\n\t'he',\n\t'iw', // Hebrew (current and legacy subtags)\n\t'ks', // Kashmiri\n\t'ku', // Kurdish\n\t'nqo', // N'Ko\n\t'ps', // Pashto\n\t'sd', // Sindhi\n\t'syr', // Syriac\n\t'ug', // Uyghur\n\t'ur', // Urdu\n\t'yi', // Yiddish\n]);\n\ntype TextInfoLocale = Intl.Locale & {\n\tgetTextInfo?: () => { direction?: string };\n\ttextInfo?: { direction?: string };\n};\n\n/** Writing direction of a language tag; `ltr` for anything unrecognised. */\nexport function languageDirection(tag: string | null | undefined): TextDirection {\n\tconst normalized = normalizeLanguageTag(tag);\n\tif (!normalized) return 'ltr';\n\ttry {\n\t\tconst locale = new Intl.Locale(normalized) as TextInfoLocale;\n\t\tconst direction = (locale.getTextInfo?.() ?? locale.textInfo)?.direction;\n\t\tif (direction === 'rtl' || direction === 'ltr') return direction;\n\t} catch {\n\t\t// Not a tag Intl.Locale accepts; the subtag check below still answers.\n\t}\n\treturn RTL_LANGUAGES.has(normalized.split('-')[0] ?? '') ? 'rtl' : 'ltr';\n}\n\n/**\n * The canonical form of a usable language tag, or `undefined` for anything a\n * language in use would not be: `es_MX` becomes `es-MX`, `NL-nl` becomes\n * `nl-NL`, and `Spanish` or an empty string are not tags. Unicode extensions\n * are dropped (`ar-EG-u-nu-latn` becomes `ar-EG`): they steer formatting and\n * have no place in a `lang` attribute.\n */\nexport function canonicalLanguageTag(tag: string | null | undefined): string | undefined {\n\tconst candidate = (tag ?? '').trim().replace(/_/g, '-').split(/[.@]/)[0] ?? '';\n\t// Intl accepts any 5-8 letter primary subtag, `Spanish` included; a language\n\t// in use has an ISO 639 code of two or three letters.\n\tif (!/^[a-z]{2,3}(-|$)/i.test(candidate)) return undefined;\n\ttry {\n\t\tconst canonical = Intl.getCanonicalLocales(candidate)[0];\n\t\treturn canonical ? new Intl.Locale(canonical).baseName : undefined;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\n/**\n * Chrome attributes: the provider's locale and direction, or `undefined`\n * without a provider, so a host that passes none keeps what the page has.\n */\nexport function interfaceLocaleAttributes(\n\tprovider: Pick<I18nProvider, 'getLocale' | 'getDirection'> | null | undefined\n): LanguageAttributes | undefined {\n\tif (!provider) return undefined;\n\tconst locale = provider.getLocale();\n\treturn {\n\t\tlang: canonicalLanguageTag(locale) ?? locale,\n\t\tdir: provider.getDirection?.() ?? languageDirection(locale),\n\t};\n}\n\n/**\n * Content attributes for a declared content language, or `undefined` when the\n * content declares none or the declaration is not a language tag. Nothing is\n * invented: undeclared content inherits its context's attributes.\n */\nexport function contentLanguageAttributes(language: string | null | undefined): LanguageAttributes | undefined {\n\tconst lang = canonicalLanguageTag(language);\n\treturn lang ? { lang, dir: languageDirection(lang) } : undefined;\n}\n"]}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BCP-47 language tag comparison.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors `packages/players-shared/src/i18n/language-tags.ts` in pie-players
|
|
5
|
+
* (`normalizeLanguageTag`, `languageTagLookupSequence`, `languageTagScript`,
|
|
6
|
+
* `languageTagScriptsMatch`), so a tag resolves to the same catalog in both
|
|
7
|
+
* repositories. `tests/language-tags.test.ts` carries that file's test cases as
|
|
8
|
+
* a parity test; change both files together and the tests keep them converged.
|
|
9
|
+
*
|
|
10
|
+
* Carries no locale data and no DOM access, so anything that compares language
|
|
11
|
+
* tags — catalog negotiation, glossary lookup — can import it without pulling a
|
|
12
|
+
* message catalog. Content producers do not agree on tag syntax: POSIX `es_ES`
|
|
13
|
+
* and BCP-47 `es-ES` both occur, so tags are compared through
|
|
14
|
+
* `normalizeLanguageTag` and never with `===`.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Canonicalize a language tag for comparison.
|
|
18
|
+
*
|
|
19
|
+
* Converts POSIX underscores to hyphens, strips a POSIX charset or modifier
|
|
20
|
+
* suffix (`es_ES.UTF-8`, `es_ES@euro`), and lowercases. The output is a
|
|
21
|
+
* comparison key, not a tag to render.
|
|
22
|
+
*
|
|
23
|
+
* Returns an empty string for nullish or blank input, which callers read as
|
|
24
|
+
* "no language declared".
|
|
25
|
+
*/
|
|
26
|
+
export declare function normalizeLanguageTag(tag: string | null | undefined): string;
|
|
27
|
+
/**
|
|
28
|
+
* The RFC 4647 lookup sequence for a tag, most specific first.
|
|
29
|
+
*
|
|
30
|
+
* `es-MX` yields `["es-mx", "es"]`, so a request for Mexican Spanish reaches a
|
|
31
|
+
* catalog tagged plain `es` without also reaching one tagged `es-ES`. Truncation
|
|
32
|
+
* never stops on a singleton subtag (`x`, `u`), per RFC 4647 §3.4, and never
|
|
33
|
+
* produces a bare region or script.
|
|
34
|
+
*/
|
|
35
|
+
export declare function languageTagLookupSequence(tag: string | null | undefined): string[];
|
|
36
|
+
/**
|
|
37
|
+
* The script a tag is written in, lowercased: explicit (`sr-Latn`), or the
|
|
38
|
+
* CLDR likely script (`zh-TW` maximizes to Hant, `sr` to Cyrl). `undefined`
|
|
39
|
+
* when the tag has no script data or the runtime has no `Intl.Locale`.
|
|
40
|
+
*/
|
|
41
|
+
export declare function languageTagScript(tag: string | null | undefined): string | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Whether a catalog reached by widening serves a request in the same script.
|
|
44
|
+
* A tag without script data passes, so a runtime without `Intl.Locale`
|
|
45
|
+
* resolves as plain RFC 4647 lookup.
|
|
46
|
+
*/
|
|
47
|
+
export declare function languageTagScriptsMatch(requested: string | null | undefined, candidate: string | null | undefined): boolean;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BCP-47 language tag comparison.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors `packages/players-shared/src/i18n/language-tags.ts` in pie-players
|
|
5
|
+
* (`normalizeLanguageTag`, `languageTagLookupSequence`, `languageTagScript`,
|
|
6
|
+
* `languageTagScriptsMatch`), so a tag resolves to the same catalog in both
|
|
7
|
+
* repositories. `tests/language-tags.test.ts` carries that file's test cases as
|
|
8
|
+
* a parity test; change both files together and the tests keep them converged.
|
|
9
|
+
*
|
|
10
|
+
* Carries no locale data and no DOM access, so anything that compares language
|
|
11
|
+
* tags — catalog negotiation, glossary lookup — can import it without pulling a
|
|
12
|
+
* message catalog. Content producers do not agree on tag syntax: POSIX `es_ES`
|
|
13
|
+
* and BCP-47 `es-ES` both occur, so tags are compared through
|
|
14
|
+
* `normalizeLanguageTag` and never with `===`.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Canonicalize a language tag for comparison.
|
|
18
|
+
*
|
|
19
|
+
* Converts POSIX underscores to hyphens, strips a POSIX charset or modifier
|
|
20
|
+
* suffix (`es_ES.UTF-8`, `es_ES@euro`), and lowercases. The output is a
|
|
21
|
+
* comparison key, not a tag to render.
|
|
22
|
+
*
|
|
23
|
+
* Returns an empty string for nullish or blank input, which callers read as
|
|
24
|
+
* "no language declared".
|
|
25
|
+
*/
|
|
26
|
+
export function normalizeLanguageTag(tag) {
|
|
27
|
+
if (!tag)
|
|
28
|
+
return '';
|
|
29
|
+
const separatorsUnified = tag.trim().replace(/_/g, '-');
|
|
30
|
+
const withoutPosixSuffix = separatorsUnified.split(/[.@]/)[0];
|
|
31
|
+
return withoutPosixSuffix.toLowerCase();
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The RFC 4647 lookup sequence for a tag, most specific first.
|
|
35
|
+
*
|
|
36
|
+
* `es-MX` yields `["es-mx", "es"]`, so a request for Mexican Spanish reaches a
|
|
37
|
+
* catalog tagged plain `es` without also reaching one tagged `es-ES`. Truncation
|
|
38
|
+
* never stops on a singleton subtag (`x`, `u`), per RFC 4647 §3.4, and never
|
|
39
|
+
* produces a bare region or script.
|
|
40
|
+
*/
|
|
41
|
+
export function languageTagLookupSequence(tag) {
|
|
42
|
+
const normalized = normalizeLanguageTag(tag);
|
|
43
|
+
if (!normalized)
|
|
44
|
+
return [];
|
|
45
|
+
const subtags = normalized.split('-');
|
|
46
|
+
const sequence = [];
|
|
47
|
+
for (let length = subtags.length; length > 0; length--) {
|
|
48
|
+
const candidate = subtags.slice(0, length);
|
|
49
|
+
// A trailing singleton introduces an extension with nothing left in it.
|
|
50
|
+
if (candidate.length > 1 && candidate[candidate.length - 1].length === 1) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
sequence.push(candidate.join('-'));
|
|
54
|
+
}
|
|
55
|
+
return sequence;
|
|
56
|
+
}
|
|
57
|
+
/** `maximize()` runs on every resolution, for each candidate catalog. */
|
|
58
|
+
const scripts = new Map();
|
|
59
|
+
/**
|
|
60
|
+
* The script a tag is written in, lowercased: explicit (`sr-Latn`), or the
|
|
61
|
+
* CLDR likely script (`zh-TW` maximizes to Hant, `sr` to Cyrl). `undefined`
|
|
62
|
+
* when the tag has no script data or the runtime has no `Intl.Locale`.
|
|
63
|
+
*/
|
|
64
|
+
export function languageTagScript(tag) {
|
|
65
|
+
const normalized = normalizeLanguageTag(tag);
|
|
66
|
+
if (!normalized || typeof Intl === 'undefined' || typeof Intl.Locale !== 'function') {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
if (scripts.has(normalized))
|
|
70
|
+
return scripts.get(normalized);
|
|
71
|
+
let script;
|
|
72
|
+
try {
|
|
73
|
+
script = new Intl.Locale(normalized).maximize().script?.toLowerCase();
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// Not a tag `Intl.Locale` parses: no script data.
|
|
77
|
+
}
|
|
78
|
+
scripts.set(normalized, script);
|
|
79
|
+
return script;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Whether a catalog reached by widening serves a request in the same script.
|
|
83
|
+
* A tag without script data passes, so a runtime without `Intl.Locale`
|
|
84
|
+
* resolves as plain RFC 4647 lookup.
|
|
85
|
+
*/
|
|
86
|
+
export function languageTagScriptsMatch(requested, candidate) {
|
|
87
|
+
const requestedScript = languageTagScript(requested);
|
|
88
|
+
const candidateScript = languageTagScript(candidate);
|
|
89
|
+
return !requestedScript || !candidateScript || requestedScript === candidateScript;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=language-tags.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"language-tags.js","sourceRoot":"","sources":["../../src/core/language-tags.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;GASG;AACH,MAAM,UAAU,oBAAoB,CAAC,GAA8B;IAClE,IAAI,CAAC,GAAG;QAAE,OAAO,EAAE,CAAC;IACpB,MAAM,iBAAiB,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACxD,MAAM,kBAAkB,GAAG,iBAAiB,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9D,OAAO,kBAAkB,CAAC,WAAW,EAAE,CAAC;AACzC,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,yBAAyB,CAAC,GAA8B;IACvE,MAAM,UAAU,GAAG,oBAAoB,CAAC,GAAG,CAAC,CAAC;IAC7C,IAAI,CAAC,UAAU;QAAE,OAAO,EAAE,CAAC;IAE3B,MAAM,OAAO,GAAG,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACtC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAE9B,KAAK,IAAI,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC,EAAE,MAAM,EAAE,E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CAAC,CAAC;IACrD,MAAM,eAAe,GAAG,iBAAiB,CAAC,SAAS,CAAC,CAAC;IACrD,OAAO,CAAC,eAAe,IAAI,CAAC,eAAe,IAAI,eAAe,KAAK,eAAe,CAAC;AACpF,CAAC","sourcesContent":["/**\n * BCP-47 language tag comparison.\n *\n * Mirrors `packages/players-shared/src/i18n/language-tags.ts` in pie-players\n * (`normalizeLanguageTag`, `languageTagLookupSequence`, `languageTagScript`,\n * `languageTagScriptsMatch`), so a tag resolves to the same catalog in both\n * repositories. `tests/language-tags.test.ts` carries that file's test cases as\n * a parity test; change both files together and the tests keep them converged.\n *\n * Carries no locale data and no DOM access, so anything that compares language\n * tags — catalog negotiation, glossary lookup — can import it without pulling a\n * message catalog. Content producers do not agree on tag syntax: POSIX `es_ES`\n * and BCP-47 `es-ES` both occur, so tags are compared through\n * `normalizeLanguageTag` and never with `===`.\n */\n\n/**\n * Canonicalize a language tag for comparison.\n *\n * Converts POSIX underscores to hyphens, strips a POSIX charset or modifier\n * suffix (`es_ES.UTF-8`, `es_ES@euro`), and lowercases. The output is a\n * comparison key, not a tag to render.\n *\n * Returns an empty string for nullish or blank input, which callers read as\n * \"no language declared\".\n */\nexport function normalizeLanguageTag(tag: string | null | undefined): string {\n\tif (!tag) return '';\n\tconst separatorsUnified = tag.trim().replace(/_/g, '-');\n\tconst withoutPosixSuffix = separatorsUnified.split(/[.@]/)[0];\n\treturn withoutPosixSuffix.toLowerCase();\n}\n\n/**\n * The RFC 4647 lookup sequence for a tag, most specific first.\n *\n * `es-MX` yields `[\"es-mx\", \"es\"]`, so a request for Mexican Spanish reaches a\n * catalog tagged plain `es` without also reaching one tagged `es-ES`. Truncation\n * never stops on a singleton subtag (`x`, `u`), per RFC 4647 §3.4, and never\n * produces a bare region or script.\n */\nexport function languageTagLookupSequence(tag: string | null | undefined): string[] {\n\tconst normalized = normalizeLanguageTag(tag);\n\tif (!normalized) return [];\n\n\tconst subtags = normalized.split('-');\n\tconst sequence: string[] = [];\n\n\tfor (let length = subtags.length; length > 0; length--) {\n\t\tconst candidate = subtags.slice(0, length);\n\t\t// A trailing singleton introduces an extension with nothing left in it.\n\t\tif (candidate.length > 1 && candidate[candidate.length - 1].length === 1) {\n\t\t\tcontinue;\n\t\t}\n\t\tsequence.push(candidate.join('-'));\n\t}\n\n\treturn sequence;\n}\n\n/** `maximize()` runs on every resolution, for each candidate catalog. */\nconst scripts = new Map<string, string | undefined>();\n\n/**\n * The script a tag is written in, lowercased: explicit (`sr-Latn`), or the\n * CLDR likely script (`zh-TW` maximizes to Hant, `sr` to Cyrl). `undefined`\n * when the tag has no script data or the runtime has no `Intl.Locale`.\n */\nexport function languageTagScript(tag: string | null | undefined): string | undefined {\n\tconst normalized = normalizeLanguageTag(tag);\n\tif (!normalized || typeof Intl === 'undefined' || typeof Intl.Locale !== 'function') {\n\t\treturn undefined;\n\t}\n\tif (scripts.has(normalized)) return scripts.get(normalized);\n\tlet script: string | undefined;\n\ttry {\n\t\tscript = new Intl.Locale(normalized).maximize().script?.toLowerCase();\n\t} catch {\n\t\t// Not a tag `Intl.Locale` parses: no script data.\n\t}\n\tscripts.set(normalized, script);\n\treturn script;\n}\n\n/**\n * Whether a catalog reached by widening serves a request in the same script.\n * A tag without script data passes, so a runtime without `Intl.Locale`\n * resolves as plain RFC 4647 lookup.\n */\nexport function languageTagScriptsMatch(\n\trequested: string | null | undefined,\n\tcandidate: string | null | undefined\n): boolean {\n\tconst requestedScript = languageTagScript(requested);\n\tconst candidateScript = languageTagScript(candidate);\n\treturn !requestedScript || !candidateScript || requestedScript === candidateScript;\n}\n"]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Locale negotiation: which catalog serves a requested language tag.
|
|
3
|
+
*
|
|
4
|
+
* Framework catalogs are keyed by language (`es`, `ar`), plus script where the
|
|
5
|
+
* script is what tells two catalogs apart (`zh-Hans`). A request walks its
|
|
6
|
+
* RFC 4647 lookup sequence with the language and likely script inserted before
|
|
7
|
+
* the bare language, so `es-MX` reaches `es`, `ar_SA` reaches `ar`, and `zh-CN`
|
|
8
|
+
* and plain `zh` reach `zh-Hans`. A catalog reached by widening must share the
|
|
9
|
+
* request's script: `zh-TW` and `zh-Hant` pass `zh-Hans` by and fall back to
|
|
10
|
+
* English.
|
|
11
|
+
*/
|
|
12
|
+
/** The catalogs this package ships, by the tag each is keyed under. */
|
|
13
|
+
export declare const FRAMEWORK_CATALOGS: readonly ["en", "ar", "es", "fr", "nl", "ro", "th", "zh-Hans"];
|
|
14
|
+
/** A shipped catalog key. */
|
|
15
|
+
export type FrameworkCatalogCode = (typeof FRAMEWORK_CATALOGS)[number];
|
|
16
|
+
/**
|
|
17
|
+
* The negotiation sequence for a tag, most specific first: its RFC 4647 lookup
|
|
18
|
+
* sequence with `language-script` inserted before the bare language.
|
|
19
|
+
* `zh-TW` yields `["zh-tw", "zh-hant", "zh"]`, `zh` yields `["zh-hans", "zh"]`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function localeLookupSequence(tag: string | null | undefined): readonly string[];
|
|
22
|
+
/**
|
|
23
|
+
* Whether `candidate`, normalized to `step`, may serve `requested`. An exact
|
|
24
|
+
* match always may; a widened or script-inserted step only in the same script.
|
|
25
|
+
*/
|
|
26
|
+
export declare function stepServes(requested: string, step: string, candidate: string): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* The best of `candidates` for a request, as authored, or `undefined` when
|
|
29
|
+
* none serves it. Ranking follows the request's negotiation sequence, not the
|
|
30
|
+
* order of `candidates`.
|
|
31
|
+
*/
|
|
32
|
+
export declare function matchLanguageTag<T extends string>(requested: string | null | undefined, candidates: readonly T[]): T | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* The shipped catalog that serves a tag, or `undefined` when none does and the
|
|
35
|
+
* tag renders in English.
|
|
36
|
+
*
|
|
37
|
+
* @example resolveFrameworkLocale('ar-EG') => 'ar'
|
|
38
|
+
* @example resolveFrameworkLocale('zh-CN') => 'zh-Hans'
|
|
39
|
+
* @example resolveFrameworkLocale('zh-TW') => undefined
|
|
40
|
+
*/
|
|
41
|
+
export declare function resolveFrameworkLocale(tag: string | null | undefined): FrameworkCatalogCode | undefined;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Locale negotiation: which catalog serves a requested language tag.
|
|
3
|
+
*
|
|
4
|
+
* Framework catalogs are keyed by language (`es`, `ar`), plus script where the
|
|
5
|
+
* script is what tells two catalogs apart (`zh-Hans`). A request walks its
|
|
6
|
+
* RFC 4647 lookup sequence with the language and likely script inserted before
|
|
7
|
+
* the bare language, so `es-MX` reaches `es`, `ar_SA` reaches `ar`, and `zh-CN`
|
|
8
|
+
* and plain `zh` reach `zh-Hans`. A catalog reached by widening must share the
|
|
9
|
+
* request's script: `zh-TW` and `zh-Hant` pass `zh-Hans` by and fall back to
|
|
10
|
+
* English.
|
|
11
|
+
*/
|
|
12
|
+
import { languageTagLookupSequence, languageTagScript, languageTagScriptsMatch, normalizeLanguageTag, } from './language-tags.js';
|
|
13
|
+
/** The catalogs this package ships, by the tag each is keyed under. */
|
|
14
|
+
export const FRAMEWORK_CATALOGS = ['en', 'ar', 'es', 'fr', 'nl', 'ro', 'th', 'zh-Hans'];
|
|
15
|
+
const sequences = new Map();
|
|
16
|
+
/**
|
|
17
|
+
* The negotiation sequence for a tag, most specific first: its RFC 4647 lookup
|
|
18
|
+
* sequence with `language-script` inserted before the bare language.
|
|
19
|
+
* `zh-TW` yields `["zh-tw", "zh-hant", "zh"]`, `zh` yields `["zh-hans", "zh"]`.
|
|
20
|
+
*/
|
|
21
|
+
export function localeLookupSequence(tag) {
|
|
22
|
+
const normalized = normalizeLanguageTag(tag);
|
|
23
|
+
const cached = sequences.get(normalized);
|
|
24
|
+
if (cached)
|
|
25
|
+
return cached;
|
|
26
|
+
let sequence = languageTagLookupSequence(normalized);
|
|
27
|
+
const script = languageTagScript(normalized);
|
|
28
|
+
if (script && sequence.length > 0) {
|
|
29
|
+
const language = sequence[sequence.length - 1];
|
|
30
|
+
const withScript = `${language}-${script}`;
|
|
31
|
+
if (!sequence.includes(withScript)) {
|
|
32
|
+
sequence = [...sequence.slice(0, -1), withScript, language];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
sequences.set(normalized, sequence);
|
|
36
|
+
return sequence;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Whether `candidate`, normalized to `step`, may serve `requested`. An exact
|
|
40
|
+
* match always may; a widened or script-inserted step only in the same script.
|
|
41
|
+
*/
|
|
42
|
+
export function stepServes(requested, step, candidate) {
|
|
43
|
+
return step === normalizeLanguageTag(requested) || languageTagScriptsMatch(requested, candidate);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The best of `candidates` for a request, as authored, or `undefined` when
|
|
47
|
+
* none serves it. Ranking follows the request's negotiation sequence, not the
|
|
48
|
+
* order of `candidates`.
|
|
49
|
+
*/
|
|
50
|
+
export function matchLanguageTag(requested, candidates) {
|
|
51
|
+
if (!requested)
|
|
52
|
+
return undefined;
|
|
53
|
+
for (const step of localeLookupSequence(requested)) {
|
|
54
|
+
const hit = candidates.find((candidate) => normalizeLanguageTag(candidate) === step && stepServes(requested, step, candidate));
|
|
55
|
+
if (hit !== undefined)
|
|
56
|
+
return hit;
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The shipped catalog that serves a tag, or `undefined` when none does and the
|
|
62
|
+
* tag renders in English.
|
|
63
|
+
*
|
|
64
|
+
* @example resolveFrameworkLocale('ar-EG') => 'ar'
|
|
65
|
+
* @example resolveFrameworkLocale('zh-CN') => 'zh-Hans'
|
|
66
|
+
* @example resolveFrameworkLocale('zh-TW') => undefined
|
|
67
|
+
*/
|
|
68
|
+
export function resolveFrameworkLocale(tag) {
|
|
69
|
+
return matchLanguageTag(tag, FRAMEWORK_CATALOGS);
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=negotiation.js.map
|
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U,EAAE,QAAQ,CAAC,CAAC;IACpC,OAAO,QAAQ,CAAC;AACjB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,SAAiB,EAAE,IAAY,EAAE,SAAiB;IAC5E,OAAO,IAAI,KAAK,oBAAoB,CAAC,SAAS,CAAC,IAAI,uBAAuB,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;AAClG,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAC/B,SAAoC,EACpC,UAAwB;IAExB,IAAI,CAAC,SAAS;QAAE,OAAO,SAAS,CAAC;IACjC,KAAK,MAAM,IAAI,IAAI,oBAAoB,CAAC,SAAS,CAAC,EAAE,CAAC;QACpD,MAAM,GAAG,GAAG,UAAU,CAAC,IAAI,CAC1B,CAAC,SAAS,EAAE,EAAE,CAAC,oBAAoB,CAAC,SAAS,CAAC,KAAK,IAAI,IAAI,UAAU,CAAC,SAAS,EAAE,IAAI,EAAE,SAAS,CAAC,CACjG,CAAC;QACF,IAAI,GAAG,KAAK,SAAS;YAAE,OAAO,GAAG,CAAC;IACnC,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CAAC,GAA8B;IACpE,OAAO,gBAAgB,CAAC,GAAG,EAAE,kBAAkB,CAAC,CAAC;AAClD,CAAC","sourcesContent":["/**\n * Locale negotiation: which catalog serves a requested language tag.\n *\n * Framework catalogs are keyed by language (`es`, `ar`), plus script where the\n * script is what tells two catalogs apart (`zh-Hans`). A request walks its\n * RFC 4647 lookup sequence with the language and likely script inserted before\n * the bare language, so `es-MX` reaches `es`, `ar_SA` reaches `ar`, and `zh-CN`\n * and plain `zh` reach `zh-Hans`. A catalog reached by widening must share the\n * request's script: `zh-TW` and `zh-Hant` pass `zh-Hans` by and fall back to\n * English.\n */\n\nimport {\n\tlanguageTagLookupSequence,\n\tlanguageTagScript,\n\tlanguageTagScriptsMatch,\n\tnormalizeLanguageTag,\n} from './language-tags.js';\n\n/** The catalogs this package ships, by the tag each is keyed under. */\nexport const FRAMEWORK_CATALOGS = ['en', 'ar', 'es', 'fr', 'nl', 'ro', 'th', 'zh-Hans'] as const;\n\n/** A shipped catalog key. */\nexport type FrameworkCatalogCode = (typeof FRAMEWORK_CATALOGS)[number];\n\nconst sequences = new Map<string, readonly string[]>();\n\n/**\n * The negotiation sequence for a tag, most specific first: its RFC 4647 lookup\n * sequence with `language-script` inserted before the bare language.\n * `zh-TW` yields `[\"zh-tw\", \"zh-hant\", \"zh\"]`, `zh` yields `[\"zh-hans\", \"zh\"]`.\n */\nexport function localeLookupSequence(tag: string | null | undefined): readonly string[] {\n\tconst normalized = normalizeLanguageTag(tag);\n\tconst cached = sequences.get(normalized);\n\tif (cached) return cached;\n\n\tlet sequence = languageTagLookupSequence(normalized);\n\tconst script = languageTagScript(normalized);\n\tif (script && sequence.length > 0) {\n\t\tconst language = sequence[sequence.length - 1];\n\t\tconst withScript = `${language}-${script}`;\n\t\tif (!sequence.includes(withScript)) {\n\t\t\tsequence = [...sequence.slice(0, -1), withScript, language];\n\t\t}\n\t}\n\tsequences.set(normalized, sequence);\n\treturn sequence;\n}\n\n/**\n * Whether `candidate`, normalized to `step`, may serve `requested`. An exact\n * match always may; a widened or script-inserted step only in the same script.\n */\nexport function stepServes(requested: string, step: string, candidate: string): boolean {\n\treturn step === normalizeLanguageTag(requested) || languageTagScriptsMatch(requested, candidate);\n}\n\n/**\n * The best of `candidates` for a request, as authored, or `undefined` when\n * none serves it. Ranking follows the request's negotiation sequence, not the\n * order of `candidates`.\n */\nexport function matchLanguageTag<T extends string>(\n\trequested: string | null | undefined,\n\tcandidates: readonly T[]\n): T | undefined {\n\tif (!requested) return undefined;\n\tfor (const step of localeLookupSequence(requested)) {\n\t\tconst hit = candidates.find(\n\t\t\t(candidate) => normalizeLanguageTag(candidate) === step && stepServes(requested, step, candidate)\n\t\t);\n\t\tif (hit !== undefined) return hit;\n\t}\n\treturn undefined;\n}\n\n/**\n * The shipped catalog that serves a tag, or `undefined` when none does and the\n * tag renders in English.\n *\n * @example resolveFrameworkLocale('ar-EG') => 'ar'\n * @example resolveFrameworkLocale('zh-CN') => 'zh-Hans'\n * @example resolveFrameworkLocale('zh-TW') => undefined\n */\nexport function resolveFrameworkLocale(tag: string | null | undefined): FrameworkCatalogCode | undefined {\n\treturn matchLanguageTag(tag, FRAMEWORK_CATALOGS);\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Digits and calendar for locale formatting
|
|
3
|
+
*
|
|
4
|
+
* The runtime's own defaults are not used: they follow whatever CLDR its ICU
|
|
5
|
+
* ships, so `ar-SA` formats with Western digits in one browser and
|
|
6
|
+
* Arabic-Indic in the next, and `ur-IN` differs between Bun and Node today.
|
|
7
|
+
* The digit table below is pinned to CLDR 48 instead, and every format runs on
|
|
8
|
+
* the Gregorian calendar, so a `fa-IR` or `th-TH` date shows the same year the
|
|
9
|
+
* rest of the assessment does.
|
|
10
|
+
*
|
|
11
|
+
* A host overrides either per tag with a Unicode extension (`ar-SA-u-nu-latn`,
|
|
12
|
+
* `fa-IR-u-ca-persian`). The extensions only steer formatting: catalog lookup
|
|
13
|
+
* and the `lang` attribute use the tag without them.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* The numbering system a tag formats with: a host-supplied `-u-nu-`, else the
|
|
17
|
+
* pinned table by language and region, then by language, else `latn`.
|
|
18
|
+
*
|
|
19
|
+
* @example numberingSystemFor('ar') => 'latn'
|
|
20
|
+
* @example numberingSystemFor('ar-EG') => 'arab'
|
|
21
|
+
* @example numberingSystemFor('ar-EG-u-nu-latn') => 'latn'
|
|
22
|
+
*/
|
|
23
|
+
export declare function numberingSystemFor(tag: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* The tag to hand `Intl` formatters: the locale with its pinned numbering
|
|
26
|
+
* system and the Gregorian calendar unless the host set either. A tag `Intl`
|
|
27
|
+
* rejects formats as English.
|
|
28
|
+
*
|
|
29
|
+
* @example formattingLocale('ar-SA') => 'ar-SA-u-ca-gregory-nu-arab'
|
|
30
|
+
*/
|
|
31
|
+
export declare function formattingLocale(tag: string): string;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Digits and calendar for locale formatting
|
|
3
|
+
*
|
|
4
|
+
* The runtime's own defaults are not used: they follow whatever CLDR its ICU
|
|
5
|
+
* ships, so `ar-SA` formats with Western digits in one browser and
|
|
6
|
+
* Arabic-Indic in the next, and `ur-IN` differs between Bun and Node today.
|
|
7
|
+
* The digit table below is pinned to CLDR 48 instead, and every format runs on
|
|
8
|
+
* the Gregorian calendar, so a `fa-IR` or `th-TH` date shows the same year the
|
|
9
|
+
* rest of the assessment does.
|
|
10
|
+
*
|
|
11
|
+
* A host overrides either per tag with a Unicode extension (`ar-SA-u-nu-latn`,
|
|
12
|
+
* `fa-IR-u-ca-persian`). The extensions only steer formatting: catalog lookup
|
|
13
|
+
* and the `lang` attribute use the tag without them.
|
|
14
|
+
*/
|
|
15
|
+
/** CLDR 48 default numbering systems that differ from `latn`, by language and region. */
|
|
16
|
+
const NUMBERING_SYSTEMS = {
|
|
17
|
+
'ar-BH': 'arab',
|
|
18
|
+
'ar-DJ': 'arab',
|
|
19
|
+
'ar-EG': 'arab',
|
|
20
|
+
'ar-ER': 'arab',
|
|
21
|
+
'ar-IL': 'arab',
|
|
22
|
+
'ar-IQ': 'arab',
|
|
23
|
+
'ar-JO': 'arab',
|
|
24
|
+
'ar-KM': 'arab',
|
|
25
|
+
'ar-KW': 'arab',
|
|
26
|
+
'ar-LB': 'arab',
|
|
27
|
+
'ar-MR': 'arab',
|
|
28
|
+
'ar-OM': 'arab',
|
|
29
|
+
'ar-PS': 'arab',
|
|
30
|
+
'ar-QA': 'arab',
|
|
31
|
+
'ar-SA': 'arab',
|
|
32
|
+
'ar-SD': 'arab',
|
|
33
|
+
'ar-SO': 'arab',
|
|
34
|
+
'ar-SS': 'arab',
|
|
35
|
+
'ar-SY': 'arab',
|
|
36
|
+
'ar-TD': 'arab',
|
|
37
|
+
'ar-YE': 'arab',
|
|
38
|
+
bn: 'beng',
|
|
39
|
+
fa: 'arabext',
|
|
40
|
+
mr: 'deva',
|
|
41
|
+
ne: 'deva',
|
|
42
|
+
ps: 'arabext',
|
|
43
|
+
'ur-IN': 'arabext',
|
|
44
|
+
};
|
|
45
|
+
const FALLBACK_LOCALE = 'en-US';
|
|
46
|
+
function parseLocale(tag) {
|
|
47
|
+
const candidate = tag.trim().replace(/_/g, '-').split(/[.@]/)[0] ?? '';
|
|
48
|
+
try {
|
|
49
|
+
return new Intl.Locale(candidate);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The numbering system a tag formats with: a host-supplied `-u-nu-`, else the
|
|
57
|
+
* pinned table by language and region, then by language, else `latn`.
|
|
58
|
+
*
|
|
59
|
+
* @example numberingSystemFor('ar') => 'latn'
|
|
60
|
+
* @example numberingSystemFor('ar-EG') => 'arab'
|
|
61
|
+
* @example numberingSystemFor('ar-EG-u-nu-latn') => 'latn'
|
|
62
|
+
*/
|
|
63
|
+
export function numberingSystemFor(tag) {
|
|
64
|
+
const locale = parseLocale(tag);
|
|
65
|
+
if (!locale)
|
|
66
|
+
return 'latn';
|
|
67
|
+
if (locale.numberingSystem)
|
|
68
|
+
return locale.numberingSystem;
|
|
69
|
+
const regional = locale.region ? NUMBERING_SYSTEMS[`${locale.language}-${locale.region}`] : undefined;
|
|
70
|
+
return regional ?? NUMBERING_SYSTEMS[locale.language] ?? 'latn';
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The tag to hand `Intl` formatters: the locale with its pinned numbering
|
|
74
|
+
* system and the Gregorian calendar unless the host set either. A tag `Intl`
|
|
75
|
+
* rejects formats as English.
|
|
76
|
+
*
|
|
77
|
+
* @example formattingLocale('ar-SA') => 'ar-SA-u-ca-gregory-nu-arab'
|
|
78
|
+
*/
|
|
79
|
+
export function formattingLocale(tag) {
|
|
80
|
+
const locale = parseLocale(tag);
|
|
81
|
+
if (!locale)
|
|
82
|
+
return FALLBACK_LOCALE;
|
|
83
|
+
return new Intl.Locale(locale.baseName, {
|
|
84
|
+
calendar: locale.calendar ?? 'gregory',
|
|
85
|
+
numberingSystem: numberingSystemFor(tag),
|
|
86
|
+
}).toString();
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=numbering.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"numbering.js","sourceRoot":"","sources":["../../src/core/numbering.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,yFAAyF;AACzF,MAAM,iBAAiB,GAAqC;IAC3D,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;IACf,EAAE,EAAE,MAAM;IACV,EAAE,EAAE,SAAS;IACb,EAAE,EAAE,MAAM;IACV,EAAE,EAAE,MAAM;IACV,EAAE,EAAE,SAAS;IACb,OAAO,EAAE,SAAS;CAClB,CAAC;AAEF,MAAM,eAAe,GAAG,OAAO,CAAC;AAEhC,SAAS,WAAW,CAAC,GAAW;IAC/B,MAAM,SAAS,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IACvE,IAAI,CAAC;QACJ,OAAO,IAAI,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACnC,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,SAAS,CAAC;IAClB,CAAC;AACF,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,kBAAkB,CAAC,GAAW;IAC7C,MAAM,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC;IAChC,IAAI,CAAC,MAAM;QAAE,OAAO,MAAM,CAAC;IAC3B,IAAI,MAAM,CAAC,eAAe;QAAE,OAAO,MAAM,CAAC,eAAe,CAAC;IAC1D,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,iBAAiB,CAAC,GAAG,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACtG,OAAO,QAAQ,IAAI,iBAAiB,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,MAAM,CAAC;AACjE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAAW;IAC3C,MAAM,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC;IAChC,IAAI,CAAC,MAAM;QAAE,OAAO,eAAe,CAAC;IACpC,OAAO,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE;QACvC,QAAQ,EAAE,MAAM,CAAC,QAAQ,IAAI,SAAS;QACtC,eAAe,EAAE,kBAAkB,CAAC,GAAG,CAAC;KACxC,CAAC,CAAC,QAAQ,EAAE,CAAC;AACf,CAAC","sourcesContent":["/**\n * Digits and calendar for locale formatting\n *\n * The runtime's own defaults are not used: they follow whatever CLDR its ICU\n * ships, so `ar-SA` formats with Western digits in one browser and\n * Arabic-Indic in the next, and `ur-IN` differs between Bun and Node today.\n * The digit table below is pinned to CLDR 48 instead, and every format runs on\n * the Gregorian calendar, so a `fa-IR` or `th-TH` date shows the same year the\n * rest of the assessment does.\n *\n * A host overrides either per tag with a Unicode extension (`ar-SA-u-nu-latn`,\n * `fa-IR-u-ca-persian`). The extensions only steer formatting: catalog lookup\n * and the `lang` attribute use the tag without them.\n */\n\n/** CLDR 48 default numbering systems that differ from `latn`, by language and region. */\nconst NUMBERING_SYSTEMS: Readonly<Record<string, string>> = {\n\t'ar-BH': 'arab',\n\t'ar-DJ': 'arab',\n\t'ar-EG': 'arab',\n\t'ar-ER': 'arab',\n\t'ar-IL': 'arab',\n\t'ar-IQ': 'arab',\n\t'ar-JO': 'arab',\n\t'ar-KM': 'arab',\n\t'ar-KW': 'arab',\n\t'ar-LB': 'arab',\n\t'ar-MR': 'arab',\n\t'ar-OM': 'arab',\n\t'ar-PS': 'arab',\n\t'ar-QA': 'arab',\n\t'ar-SA': 'arab',\n\t'ar-SD': 'arab',\n\t'ar-SO': 'arab',\n\t'ar-SS': 'arab',\n\t'ar-SY': 'arab',\n\t'ar-TD': 'arab',\n\t'ar-YE': 'arab',\n\tbn: 'beng',\n\tfa: 'arabext',\n\tmr: 'deva',\n\tne: 'deva',\n\tps: 'arabext',\n\t'ur-IN': 'arabext',\n};\n\nconst FALLBACK_LOCALE = 'en-US';\n\nfunction parseLocale(tag: string): Intl.Locale | undefined {\n\tconst candidate = tag.trim().replace(/_/g, '-').split(/[.@]/)[0] ?? '';\n\ttry {\n\t\treturn new Intl.Locale(candidate);\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\n/**\n * The numbering system a tag formats with: a host-supplied `-u-nu-`, else the\n * pinned table by language and region, then by language, else `latn`.\n *\n * @example numberingSystemFor('ar') => 'latn'\n * @example numberingSystemFor('ar-EG') => 'arab'\n * @example numberingSystemFor('ar-EG-u-nu-latn') => 'latn'\n */\nexport function numberingSystemFor(tag: string): string {\n\tconst locale = parseLocale(tag);\n\tif (!locale) return 'latn';\n\tif (locale.numberingSystem) return locale.numberingSystem;\n\tconst regional = locale.region ? NUMBERING_SYSTEMS[`${locale.language}-${locale.region}`] : undefined;\n\treturn regional ?? NUMBERING_SYSTEMS[locale.language] ?? 'latn';\n}\n\n/**\n * The tag to hand `Intl` formatters: the locale with its pinned numbering\n * system and the Gregorian calendar unless the host set either. A tag `Intl`\n * rejects formats as English.\n *\n * @example formattingLocale('ar-SA') => 'ar-SA-u-ca-gregory-nu-arab'\n */\nexport function formattingLocale(tag: string): string {\n\tconst locale = parseLocale(tag);\n\tif (!locale) return FALLBACK_LOCALE;\n\treturn new Intl.Locale(locale.baseName, {\n\t\tcalendar: locale.calendar ?? 'gregory',\n\t\tnumberingSystem: numberingSystemFor(tag),\n\t}).toString();\n}\n"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pseudo-localization: a transform over a catalog that keeps every key, plural
|
|
3
|
+
* category and `{placeholder}` intact while making each string accented,
|
|
4
|
+
* bracketed and padded roughly 40% longer.
|
|
5
|
+
*
|
|
6
|
+
* The translation gates check parity, not rendered geometry. A pseudo-locale
|
|
7
|
+
* makes truncation, clipped labels, strings glued together in code and text
|
|
8
|
+
* that never went through the catalog visible in any demo or e2e run. PIE uses
|
|
9
|
+
* the Android tags `en-XA` and `ar-XB`, registered through `customMessages` and
|
|
10
|
+
* never shipped as catalogs. Ported from pie-players `pseudo.ts`.
|
|
11
|
+
*/
|
|
12
|
+
import type { LocaleMessages } from './types.js';
|
|
13
|
+
/** Expanded Latin pseudo-locale: text grows ~40% and carries accents and brackets. */
|
|
14
|
+
export declare const PSEUDO_LOCALE_EXPANDED = "en-XA";
|
|
15
|
+
/**
|
|
16
|
+
* Right-to-left pseudo-locale: transformed English under an `ar` primary
|
|
17
|
+
* subtag, so direction resolves RTL and chrome lays out mirrored without a
|
|
18
|
+
* word of Arabic on screen.
|
|
19
|
+
*/
|
|
20
|
+
export declare const PSEUDO_LOCALE_RTL = "ar-XB";
|
|
21
|
+
export interface PseudoLocalizeOptions {
|
|
22
|
+
/** Extra length as a fraction of the original, on top of what accents add. Default 0.4. */
|
|
23
|
+
expansion?: number;
|
|
24
|
+
/** Wrap every string in `[` and `]` so concatenated fragments stand out. Default true. */
|
|
25
|
+
brackets?: boolean;
|
|
26
|
+
/** Map Latin letters to accented look-alikes. Default true. */
|
|
27
|
+
accents?: boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The pseudo-localized twin of a catalog: same keys, same plural categories,
|
|
31
|
+
* same placeholder sets, longer accented bracketed strings.
|
|
32
|
+
*/
|
|
33
|
+
export declare function pseudoLocalize(catalog: LocaleMessages, options?: PseudoLocalizeOptions): LocaleMessages;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** Expanded Latin pseudo-locale: text grows ~40% and carries accents and brackets. */
|
|
2
|
+
export const PSEUDO_LOCALE_EXPANDED = 'en-XA';
|
|
3
|
+
/**
|
|
4
|
+
* Right-to-left pseudo-locale: transformed English under an `ar` primary
|
|
5
|
+
* subtag, so direction resolves RTL and chrome lays out mirrored without a
|
|
6
|
+
* word of Arabic on screen.
|
|
7
|
+
*/
|
|
8
|
+
export const PSEUDO_LOCALE_RTL = 'ar-XB';
|
|
9
|
+
const PLAIN = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
10
|
+
const ACCENTED = 'áƀçðéƒğĥíĵķĺḿñóþǫŕšŧúṽŵẋýžÁɃÇÐÉƑĞĤÍĴĶĹḾÑÓÞǪŔŠŦÚṼŴẊÝŽ';
|
|
11
|
+
const ACCENTS = new Map([...PLAIN].map((letter, index) => [letter, [...ACCENTED][index]]));
|
|
12
|
+
const PLACEHOLDER = /(\{\w+\})/g;
|
|
13
|
+
function pseudoText(text, { expansion, accents, brackets }) {
|
|
14
|
+
const transformed = text.split(PLACEHOLDER).map((segment, index) => {
|
|
15
|
+
// A capturing split leaves each placeholder at an odd index, verbatim.
|
|
16
|
+
if (index % 2 === 1 || segment === '')
|
|
17
|
+
return segment;
|
|
18
|
+
const out = accents ? segment.replace(/[a-zA-Z]/g, (letter) => ACCENTS.get(letter) ?? letter) : segment;
|
|
19
|
+
if (expansion <= 0)
|
|
20
|
+
return out;
|
|
21
|
+
return `${out} ${'~'.repeat(Math.max(1, Math.ceil(segment.length * expansion)))}`;
|
|
22
|
+
});
|
|
23
|
+
const joined = transformed.join('');
|
|
24
|
+
return brackets ? `[${joined}]` : joined;
|
|
25
|
+
}
|
|
26
|
+
function pseudoNode(node, settings) {
|
|
27
|
+
if (typeof node === 'string')
|
|
28
|
+
return pseudoText(node, settings);
|
|
29
|
+
if (node === null || typeof node !== 'object')
|
|
30
|
+
return node;
|
|
31
|
+
return Object.fromEntries(Object.entries(node).map(([key, child]) => [key, pseudoNode(child, settings)]));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The pseudo-localized twin of a catalog: same keys, same plural categories,
|
|
35
|
+
* same placeholder sets, longer accented bracketed strings.
|
|
36
|
+
*/
|
|
37
|
+
export function pseudoLocalize(catalog, options) {
|
|
38
|
+
return pseudoNode(catalog, {
|
|
39
|
+
expansion: options?.expansion ?? 0.4,
|
|
40
|
+
brackets: options?.brackets ?? true,
|
|
41
|
+
accents: options?.accents ?? true,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=pseudo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pseudo.js","sourceRoot":"","sources":["../../src/core/pseudo.ts"],"names":[],"mappings":"AAaA,sFAAsF;AACtF,MAAM,CAAC,MAAM,sBAAsB,GAAG,OAAO,CAAC;AAE9C;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,OAAO,CAAC;AAWzC,MAAM,KAAK,GAAG,sDAAsD,CAAC;AACrE,MAAM,QAAQ,GAAG,sDAAsD,CAAC;AACxE,MA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AAQ;QAAE,OAAO,UAAU,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;IAChE,IAAI,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IAC3D,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,UAAU,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;AAC3G,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,OAAuB,EAAE,OAA+B;IACtF,OAAO,UAAU,CAAC,OAAO,EAAE;QAC1B,SAAS,EAAE,OAAO,EAAE,SAAS,IAAI,GAAG;QACpC,QAAQ,EAAE,OAAO,EAAE,QAAQ,IAAI,IAAI;QACnC,OAAO,EAAE,OAAO,EAAE,OAAO,IAAI,IAAI;KACjC,CAAmB,CAAC;AACtB,CAAC","sourcesContent":["/**\n * Pseudo-localization: a transform over a catalog that keeps every key, plural\n * category and `{placeholder}` intact while making each string accented,\n * bracketed and padded roughly 40% longer.\n *\n * The translation gates check parity, not rendered geometry. A pseudo-locale\n * makes truncation, clipped labels, strings glued together in code and text\n * that never went through the catalog visible in any demo or e2e run. PIE uses\n * the Android tags `en-XA` and `ar-XB`, registered through `customMessages` and\n * never shipped as catalogs. Ported from pie-players `pseudo.ts`.\n */\nimport type { LocaleMessages } from './types.js';\n\n/** Expanded Latin pseudo-locale: text grows ~40% and carries accents and brackets. */\nexport const PSEUDO_LOCALE_EXPANDED = 'en-XA';\n\n/**\n * Right-to-left pseudo-locale: transformed English under an `ar` primary\n * subtag, so direction resolves RTL and chrome lays out mirrored without a\n * word of Arabic on screen.\n */\nexport const PSEUDO_LOCALE_RTL = 'ar-XB';\n\nexport interface PseudoLocalizeOptions {\n\t/** Extra length as a fraction of the original, on top of what accents add. Default 0.4. */\n\texpansion?: number;\n\t/** Wrap every string in `[` and `]` so concatenated fragments stand out. Default true. */\n\tbrackets?: boolean;\n\t/** Map Latin letters to accented look-alikes. Default true. */\n\taccents?: boolean;\n}\n\nconst PLAIN = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';\nconst ACCENTED = 'áƀçðéƒğĥíĵķĺḿñóþǫŕšŧúṽŵẋýžÁɃÇÐÉƑĞĤÍĴĶĹḾÑÓÞǪŔŠŦÚṼŴẊÝŽ';\nconst ACCENTS = new Map([...PLAIN].map((letter, index) => [letter, [...ACCENTED][index]!]));\n\nconst PLACEHOLDER = /(\\{\\w+\\})/g;\n\nfunction pseudoText(text: string, { expansion, accents, brackets }: Required<PseudoLocalizeOptions>): string {\n\tconst transformed = text.split(PLACEHOLDER).map((segment, index) => {\n\t\t// A capturing split leaves each placeholder at an odd index, verbatim.\n\t\tif (index % 2 === 1 || segment === '') return segment;\n\t\tconst out = accents ? segment.replace(/[a-zA-Z]/g, (letter) => ACCENTS.get(letter) ?? letter) : segment;\n\t\tif (expansion <= 0) return out;\n\t\treturn `${out} ${'~'.repeat(Math.max(1, Math.ceil(segment.length * expansion)))}`;\n\t});\n\tconst joined = transformed.join('');\n\treturn brackets ? `[${joined}]` : joined;\n}\n\nfunction pseudoNode(node: unknown, settings: Required<PseudoLocalizeOptions>): unknown {\n\tif (typeof node === 'string') return pseudoText(node, settings);\n\tif (node === null || typeof node !== 'object') return node;\n\treturn Object.fromEntries(Object.entries(node).map(([key, child]) => [key, pseudoNode(child, settings)]));\n}\n\n/**\n * The pseudo-localized twin of a catalog: same keys, same plural categories,\n * same placeholder sets, longer accented bracketed strings.\n */\nexport function pseudoLocalize(catalog: LocaleMessages, options?: PseudoLocalizeOptions): LocaleMessages {\n\treturn pseudoNode(catalog, {\n\t\texpansion: options?.expansion ?? 0.4,\n\t\tbrackets: options?.brackets ?? true,\n\t\taccents: options?.accents ?? true,\n\t}) as LocaleMessages;\n}\n"]}
|