@prezly/theme-kit-intl 7.1.0 → 8.2.0
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/build/dsl.cjs +9 -0
- package/build/dsl.d.ts +12 -0
- package/build/dsl.mjs +3 -0
- package/build/formatting/datetime/constants.cjs +7 -0
- package/build/formatting/datetime/constants.d.ts +1 -0
- package/build/formatting/datetime/constants.mjs +1 -0
- package/build/formatting/datetime/index.cjs +25 -0
- package/build/formatting/datetime/index.d.ts +3 -0
- package/build/formatting/datetime/index.mjs +2 -0
- package/build/formatting/datetime/lib.cjs +58 -0
- package/build/formatting/datetime/lib.d.ts +10 -0
- package/build/formatting/datetime/lib.mjs +51 -0
- package/build/formatting/datetime/shared.cjs +15 -0
- package/build/formatting/datetime/shared.d.ts +2 -0
- package/build/formatting/datetime/shared.mjs +9 -0
- package/build/formatting/datetime/types.cjs +1 -0
- package/build/formatting/datetime/types.d.ts +3 -0
- package/build/formatting/datetime/types.mjs +1 -0
- package/build/formatting/index.cjs +27 -0
- package/build/formatting/index.d.ts +2 -0
- package/build/formatting/index.mjs +2 -0
- package/build/formatting/messages/index.cjs +18 -0
- package/build/formatting/messages/index.d.ts +2 -0
- package/build/formatting/messages/index.mjs +1 -0
- package/build/formatting/messages/lib.cjs +108 -0
- package/build/formatting/messages/lib.d.ts +3 -0
- package/build/formatting/messages/lib.mjs +101 -0
- package/build/formatting/messages/lib.test.cjs +87 -0
- package/build/formatting/messages/lib.test.d.ts +1 -0
- package/build/formatting/messages/lib.test.mjs +84 -0
- package/build/formatting/messages/types.cjs +1 -0
- package/build/formatting/messages/types.d.ts +10 -0
- package/build/formatting/messages/types.mjs +1 -0
- package/build/index.cjs +37 -0
- package/build/index.d.ts +3 -0
- package/build/index.mjs +4 -0
- package/build/locales/Locale.cjs +105 -0
- package/build/locales/Locale.d.ts +61 -0
- package/build/locales/Locale.mjs +99 -0
- package/build/locales/Locale.test.cjs +120 -0
- package/build/locales/Locale.test.d.ts +1 -0
- package/build/locales/Locale.test.mjs +118 -0
- package/build/locales/constants.cjs +7 -0
- package/build/locales/constants.d.ts +1 -0
- package/build/locales/constants.mjs +1 -0
- package/build/locales/index.cjs +40 -0
- package/build/locales/index.d.ts +5 -0
- package/build/locales/index.mjs +5 -0
- package/build/locales/isLocaleSupported.cjs +15 -0
- package/build/locales/isLocaleSupported.d.ts +2 -0
- package/build/locales/isLocaleSupported.mjs +9 -0
- package/build/locales/pickSuportedLocale.cjs +37 -0
- package/build/locales/pickSuportedLocale.d.ts +6 -0
- package/build/locales/pickSuportedLocale.mjs +31 -0
- package/build/locales/pickSuportedLocale.test.cjs +26 -0
- package/build/locales/pickSuportedLocale.test.d.ts +1 -0
- package/build/locales/pickSuportedLocale.test.mjs +24 -0
- package/build/locales/supportedLocales.cjs +12 -0
- package/build/locales/supportedLocales.d.ts +6 -0
- package/build/locales/supportedLocales.mjs +6 -0
- package/build/{cjs/messages/actions.js → messages/actions.cjs} +5 -2
- package/build/{esm/messages → messages}/actions.mjs +1 -1
- package/build/{cjs/messages/content.js → messages/content.cjs} +11 -8
- package/build/{esm/messages → messages}/content.mjs +1 -1
- package/build/{cjs/messages/errors.js → messages/errors.cjs} +7 -4
- package/build/{esm/messages → messages}/errors.mjs +1 -1
- package/build/{cjs/messages/index.js → messages/index.cjs} +9 -6
- package/build/{cjs/messages/misc.js → messages/misc.cjs} +5 -2
- package/build/{esm/messages → messages}/misc.mjs +1 -1
- package/build/{cjs/messages/search.js → messages/search.cjs} +6 -3
- package/build/{esm/messages → messages}/search.mjs +1 -1
- package/build/{cjs/messages/subscription.js → messages/subscription.cjs} +5 -2
- package/build/{esm/messages → messages}/subscription.mjs +1 -1
- package/package.json +27 -28
- package/build/cjs/index.d.ts +0 -4
- package/build/cjs/index.js +0 -19
- package/build/cjs/isLocaleSupported.d.ts +0 -1
- package/build/cjs/isLocaleSupported.js +0 -7
- package/build/cjs/locales.d.ts +0 -1
- package/build/cjs/locales.js +0 -5
- package/build/esm/index.d.ts +0 -4
- package/build/esm/index.mjs +0 -4
- package/build/esm/isLocaleSupported.d.ts +0 -1
- package/build/esm/isLocaleSupported.mjs +0 -4
- package/build/esm/locales.d.ts +0 -1
- package/build/esm/locales.mjs +0 -2
- package/build/esm/messages/actions.d.ts +0 -58
- package/build/esm/messages/content.d.ts +0 -66
- package/build/esm/messages/errors.d.ts +0 -34
- package/build/esm/messages/index.d.ts +0 -6
- package/build/esm/messages/misc.d.ts +0 -20
- package/build/esm/messages/search.d.ts +0 -66
- package/build/esm/messages/subscription.d.ts +0 -19
- /package/build/{messages → i18n}/af.json +0 -0
- /package/build/{messages → i18n}/ar.json +0 -0
- /package/build/{messages → i18n}/az.json +0 -0
- /package/build/{messages → i18n}/be.json +0 -0
- /package/build/{messages → i18n}/bg.json +0 -0
- /package/build/{messages → i18n}/ca.json +0 -0
- /package/build/{messages → i18n}/cs.json +0 -0
- /package/build/{messages → i18n}/da.json +0 -0
- /package/build/{messages → i18n}/de.json +0 -0
- /package/build/{messages → i18n}/el.json +0 -0
- /package/build/{messages → i18n}/en.json +0 -0
- /package/build/{messages → i18n}/es.json +0 -0
- /package/build/{messages → i18n}/et.json +0 -0
- /package/build/{messages → i18n}/fi.json +0 -0
- /package/build/{messages → i18n}/fil.json +0 -0
- /package/build/{messages → i18n}/fr.json +0 -0
- /package/build/{messages → i18n}/ga.json +0 -0
- /package/build/{messages → i18n}/he.json +0 -0
- /package/build/{messages → i18n}/hi.json +0 -0
- /package/build/{messages → i18n}/hr.json +0 -0
- /package/build/{messages → i18n}/hu.json +0 -0
- /package/build/{messages → i18n}/id.json +0 -0
- /package/build/{messages → i18n}/it.json +0 -0
- /package/build/{messages → i18n}/ja.json +0 -0
- /package/build/{messages → i18n}/kk.json +0 -0
- /package/build/{messages → i18n}/ko.json +0 -0
- /package/build/{messages → i18n}/lt.json +0 -0
- /package/build/{messages → i18n}/lv.json +0 -0
- /package/build/{messages → i18n}/mt.json +0 -0
- /package/build/{messages → i18n}/nl.json +0 -0
- /package/build/{messages → i18n}/no.json +0 -0
- /package/build/{messages → i18n}/pl.json +0 -0
- /package/build/{messages → i18n}/pt-BR.json +0 -0
- /package/build/{messages → i18n}/pt.json +0 -0
- /package/build/{messages → i18n}/ro.json +0 -0
- /package/build/{messages → i18n}/ru.json +0 -0
- /package/build/{messages → i18n}/sk.json +0 -0
- /package/build/{messages → i18n}/sl.json +0 -0
- /package/build/{messages → i18n}/sr.json +0 -0
- /package/build/{messages → i18n}/sv.json +0 -0
- /package/build/{messages → i18n}/sw.json +0 -0
- /package/build/{messages → i18n}/th.json +0 -0
- /package/build/{messages → i18n}/tr.json +0 -0
- /package/build/{messages → i18n}/uk.json +0 -0
- /package/build/{messages → i18n}/ur.json +0 -0
- /package/build/{messages → i18n}/uz.json +0 -0
- /package/build/{messages → i18n}/vi.json +0 -0
- /package/build/{messages → i18n}/zh-CN.json +0 -0
- /package/build/{messages → i18n}/zh-TW.json +0 -0
- /package/build/{cjs/messages → messages}/actions.d.ts +0 -0
- /package/build/{cjs/messages → messages}/content.d.ts +0 -0
- /package/build/{cjs/messages → messages}/errors.d.ts +0 -0
- /package/build/{cjs/messages → messages}/index.d.ts +0 -0
- /package/build/{esm/messages → messages}/index.mjs +0 -0
- /package/build/{cjs/messages → messages}/misc.d.ts +0 -0
- /package/build/{cjs/messages → messages}/search.d.ts +0 -0
- /package/build/{cjs/messages → messages}/subscription.d.ts +0 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
2
|
+
import { formatMessageFragment, formatMessageString } from "./lib.mjs";
|
|
3
|
+
var I18N = {
|
|
4
|
+
["hello.world"]: [{
|
|
5
|
+
type: 0,
|
|
6
|
+
value: '¡Hola, '
|
|
7
|
+
}, {
|
|
8
|
+
type: 1,
|
|
9
|
+
value: 'name'
|
|
10
|
+
}, {
|
|
11
|
+
type: 0,
|
|
12
|
+
value: '!'
|
|
13
|
+
}]
|
|
14
|
+
};
|
|
15
|
+
describe('formatMessageString', () => {
|
|
16
|
+
it('should format intl-generated message intl object using the given values', () => {
|
|
17
|
+
var formatted = formatMessageString({
|
|
18
|
+
id: 'hello.world',
|
|
19
|
+
defaultMessage: 'Hello, {name}!'
|
|
20
|
+
}, I18N, {
|
|
21
|
+
name: 'Julio'
|
|
22
|
+
});
|
|
23
|
+
expect(formatted).toBe('¡Hola, Julio!');
|
|
24
|
+
});
|
|
25
|
+
it('should leave placeholders if the given values object does not have enough data', () => {
|
|
26
|
+
var formatted = formatMessageString({
|
|
27
|
+
id: 'hello.world',
|
|
28
|
+
defaultMessage: 'Hello, {name}!'
|
|
29
|
+
}, I18N, {});
|
|
30
|
+
expect(formatted).toBe('¡Hola, {name}!');
|
|
31
|
+
});
|
|
32
|
+
it('should fallback to the default message if the dictionary does not have it', () => {
|
|
33
|
+
var formatted = formatMessageString({
|
|
34
|
+
id: 'hello.world',
|
|
35
|
+
defaultMessage: 'Hello, {name}!'
|
|
36
|
+
}, {}, {
|
|
37
|
+
name: 'Julio'
|
|
38
|
+
});
|
|
39
|
+
expect(formatted).toBe('Hello, Julio!');
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
describe('formatMessageFragment', () => {
|
|
43
|
+
it('should format intl-generated message intl object using the given values', () => {
|
|
44
|
+
var formatted = formatMessageFragment({
|
|
45
|
+
id: 'hello.world',
|
|
46
|
+
defaultMessage: 'Hello, {name}!'
|
|
47
|
+
}, I18N, {
|
|
48
|
+
name: createElement('strong', ['Julio'])
|
|
49
|
+
});
|
|
50
|
+
expect(formatted).toEqual(['¡Hola, ', {
|
|
51
|
+
type: 'strong',
|
|
52
|
+
children: ['Julio']
|
|
53
|
+
}, '!']);
|
|
54
|
+
});
|
|
55
|
+
it('should leave placeholders if the given values object does not have enough data', () => {
|
|
56
|
+
var formatted = formatMessageFragment({
|
|
57
|
+
id: 'hello.world',
|
|
58
|
+
defaultMessage: 'Hello, {name}!'
|
|
59
|
+
}, I18N, {});
|
|
60
|
+
expect(formatted).toEqual(['¡Hola, ', '{name}', '!']);
|
|
61
|
+
});
|
|
62
|
+
it('should fallback to the default message if the dictionary does not have it', () => {
|
|
63
|
+
var formatted = formatMessageFragment({
|
|
64
|
+
id: 'hello.world',
|
|
65
|
+
defaultMessage: 'Hello, {name}!'
|
|
66
|
+
}, {}, {
|
|
67
|
+
name: createElement('strong', ['Julio'])
|
|
68
|
+
});
|
|
69
|
+
expect(formatted).toEqual(['Hello, ', {
|
|
70
|
+
type: 'strong',
|
|
71
|
+
children: ['Julio']
|
|
72
|
+
}, '!']);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Emulating React.createElement for test purposes.
|
|
78
|
+
*/
|
|
79
|
+
function createElement(type, children) {
|
|
80
|
+
return {
|
|
81
|
+
type,
|
|
82
|
+
children
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type IntlDictionary = Partial<Record<string, IntlMessageFormat>>;
|
|
2
|
+
export type IntlMessageFormat = {
|
|
3
|
+
type: 0 | 1;
|
|
4
|
+
value: string;
|
|
5
|
+
}[];
|
|
6
|
+
export interface IntlMessageDescriptor {
|
|
7
|
+
id: string;
|
|
8
|
+
defaultMessage?: string;
|
|
9
|
+
}
|
|
10
|
+
export type IntlMessageValues<T> = Record<string, T>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/build/index.cjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
translations: true
|
|
8
|
+
};
|
|
9
|
+
exports.translations = void 0;
|
|
10
|
+
var _index = require("./formatting/index.cjs");
|
|
11
|
+
Object.keys(_index).forEach(function (key) {
|
|
12
|
+
if (key === "default" || key === "__esModule") return;
|
|
13
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
14
|
+
if (key in exports && exports[key] === _index[key]) return;
|
|
15
|
+
Object.defineProperty(exports, key, {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function get() {
|
|
18
|
+
return _index[key];
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
var _index2 = require("./locales/index.cjs");
|
|
23
|
+
Object.keys(_index2).forEach(function (key) {
|
|
24
|
+
if (key === "default" || key === "__esModule") return;
|
|
25
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
26
|
+
if (key in exports && exports[key] === _index2[key]) return;
|
|
27
|
+
Object.defineProperty(exports, key, {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function get() {
|
|
30
|
+
return _index2[key];
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
var _translations = _interopRequireWildcard(require("./messages/index.cjs"));
|
|
35
|
+
exports.translations = _translations;
|
|
36
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
|
37
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
package/build/index.d.ts
ADDED
package/build/index.mjs
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Locale = void 0;
|
|
7
|
+
var _isNotUndefined = require("@technically/is-not-undefined");
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
9
|
+
|
|
10
|
+
var KNOWN_SCRIPTS = ['hant', 'hans'];
|
|
11
|
+
var KNOWN_RTL_LANGUAGES = ['ar', 'he', 'ur'];
|
|
12
|
+
var LANG_CODE_REGEX = /^[a-z]{2,3}$/;
|
|
13
|
+
var CACHE = new Map();
|
|
14
|
+
var Locale;
|
|
15
|
+
(function (_Locale) {
|
|
16
|
+
/**
|
|
17
|
+
* Shortest possible slug that can identify a locale for the given newsroom.
|
|
18
|
+
* This can be full language code or lowercase region code if they're unanbiguously identify a locale
|
|
19
|
+
* or full locale slug if nothing else applies.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
function from(locale) {
|
|
23
|
+
if (typeof locale === 'object') {
|
|
24
|
+
return locale;
|
|
25
|
+
}
|
|
26
|
+
var cached = CACHE.get(locale);
|
|
27
|
+
if (cached) {
|
|
28
|
+
return cached;
|
|
29
|
+
}
|
|
30
|
+
var [lang, ...rest] = locale.toLowerCase().split(/[_-]/g);
|
|
31
|
+
if (!lang.match(LANG_CODE_REGEX)) {
|
|
32
|
+
throw new Error("Invalid locale code provided: \"".concat(locale, "\"."));
|
|
33
|
+
}
|
|
34
|
+
var scripts = rest.filter(s => KNOWN_SCRIPTS.includes(s));
|
|
35
|
+
var regions = rest.filter(s => !KNOWN_SCRIPTS.includes(s)).map(region => region.toUpperCase());
|
|
36
|
+
if (scripts.length > 1) {
|
|
37
|
+
throw new Error("Unable to parse the provided locale code: \"".concat(locale, "\". Multiple script codes detected."));
|
|
38
|
+
}
|
|
39
|
+
if (regions.length > 1) {
|
|
40
|
+
throw new Error("Unable to parse the provided locale code: \"".concat(locale, "\". Multiple region codes detected."));
|
|
41
|
+
}
|
|
42
|
+
var scriptCode = scripts[0];
|
|
43
|
+
var regionCode = regions[0];
|
|
44
|
+
var code = [lang.toLowerCase(), regionCode, scriptCode ? toPascalCase(scriptCode) : undefined].filter(_isNotUndefined.isNotUndefined).join('_');
|
|
45
|
+
var isoCode = code.replace('_', '-');
|
|
46
|
+
var obj = {
|
|
47
|
+
code,
|
|
48
|
+
isoCode,
|
|
49
|
+
slug: isoCode.toLowerCase(),
|
|
50
|
+
direction: KNOWN_RTL_LANGUAGES.includes(lang) ? 'rtl' : 'ltr',
|
|
51
|
+
lang,
|
|
52
|
+
region: regionCode,
|
|
53
|
+
script: scriptCode ? toPascalCase(scriptCode) : undefined
|
|
54
|
+
};
|
|
55
|
+
CACHE.set(obj.code, obj);
|
|
56
|
+
CACHE.set(obj.isoCode, obj);
|
|
57
|
+
CACHE.set(obj.slug, obj);
|
|
58
|
+
return obj;
|
|
59
|
+
}
|
|
60
|
+
_Locale.from = from;
|
|
61
|
+
function isValid(locale) {
|
|
62
|
+
try {
|
|
63
|
+
Locale.from(locale);
|
|
64
|
+
} catch (_unused) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
_Locale.isValid = isValid;
|
|
70
|
+
function isLanguageCode(locale, langCode) {
|
|
71
|
+
return Locale.from(locale).lang === langCode.toLowerCase();
|
|
72
|
+
}
|
|
73
|
+
_Locale.isLanguageCode = isLanguageCode;
|
|
74
|
+
function isRegionCode(locale, regionCode) {
|
|
75
|
+
return Locale.from(locale).region === regionCode.toUpperCase();
|
|
76
|
+
}
|
|
77
|
+
_Locale.isRegionCode = isRegionCode;
|
|
78
|
+
function isRegionIndependent(locale) {
|
|
79
|
+
return !Locale.from(locale).region;
|
|
80
|
+
}
|
|
81
|
+
_Locale.isRegionIndependent = isRegionIndependent;
|
|
82
|
+
function isEqual(a, b) {
|
|
83
|
+
var al = Locale.from(a);
|
|
84
|
+
var bl = Locale.from(b);
|
|
85
|
+
return al.lang === bl.lang && al.region === bl.region && al.script === bl.script;
|
|
86
|
+
}
|
|
87
|
+
_Locale.isEqual = isEqual;
|
|
88
|
+
function isSameLang(a, b) {
|
|
89
|
+
return Locale.from(a).lang === Locale.from(b).lang;
|
|
90
|
+
}
|
|
91
|
+
_Locale.isSameLang = isSameLang;
|
|
92
|
+
function isSameRegion(a, b) {
|
|
93
|
+
var al = Locale.from(a);
|
|
94
|
+
var bl = Locale.from(b);
|
|
95
|
+
if (!al.region || !bl.region) {
|
|
96
|
+
// It's never the same region, if it's not defined.
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
return al.region === bl.region;
|
|
100
|
+
}
|
|
101
|
+
_Locale.isSameRegion = isSameRegion;
|
|
102
|
+
})(Locale || (exports.Locale = Locale = {}));
|
|
103
|
+
function toPascalCase(input) {
|
|
104
|
+
return "".concat(input.substring(0, 1).toUpperCase()).concat(input.substring(1).toLowerCase());
|
|
105
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export interface Locale {
|
|
2
|
+
/**
|
|
3
|
+
* Full locale code with underscores.
|
|
4
|
+
* Example: 'nl_BE'
|
|
5
|
+
*/
|
|
6
|
+
code: Locale.Code;
|
|
7
|
+
/**
|
|
8
|
+
* Full locale code in ISO format (with dashes instead of underscores)
|
|
9
|
+
* Example: 'nl-BE'
|
|
10
|
+
*/
|
|
11
|
+
isoCode: Locale.IsoCode;
|
|
12
|
+
/**
|
|
13
|
+
* URL-friendly case-insensitive locale code.
|
|
14
|
+
* Example: 'nl-be'
|
|
15
|
+
*/
|
|
16
|
+
slug: Locale.UrlSlug;
|
|
17
|
+
/**
|
|
18
|
+
* Language code.
|
|
19
|
+
* Examples: `nl`, `en`, `fr`.
|
|
20
|
+
*/
|
|
21
|
+
lang: Locale.LangCode;
|
|
22
|
+
/**
|
|
23
|
+
* Text direction for the locale.
|
|
24
|
+
*/
|
|
25
|
+
direction: 'ltr' | 'rtl';
|
|
26
|
+
/**
|
|
27
|
+
* Locale region code.
|
|
28
|
+
* E.g. `en-US` has `US` as region, while `en` is region independent and does not have a `regionCode`.
|
|
29
|
+
*/
|
|
30
|
+
region: Locale.RegionCode | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Locale script code.
|
|
33
|
+
* E.g. `zh-Hant` has `Hant` as script code (this is not a region).
|
|
34
|
+
*/
|
|
35
|
+
script: Locale.ScriptCode | undefined;
|
|
36
|
+
}
|
|
37
|
+
export declare namespace Locale {
|
|
38
|
+
export type AnyCode = Code | IsoCode | UrlSlug;
|
|
39
|
+
/**
|
|
40
|
+
* Shortest possible slug that can identify a locale for the given newsroom.
|
|
41
|
+
* This can be full language code or lowercase region code if they're unanbiguously identify a locale
|
|
42
|
+
* or full locale slug if nothing else applies.
|
|
43
|
+
*/
|
|
44
|
+
export type AnySlug = UrlSlug | LangCode | LowercaseRegionCode;
|
|
45
|
+
type LowercaseRegionCode = `${Lowercase<string>}`;
|
|
46
|
+
export type Code = `${LangCode}` | `${LangCode}_${RegionCode}` | `${LangCode}_${ScriptCode}` | `${LangCode}_${ScriptCode}_${RegionCode}`;
|
|
47
|
+
export type IsoCode = `${LangCode}` | `${LangCode}-${RegionCode}` | `${LangCode}-${ScriptCode}` | `${LangCode}-${ScriptCode}-${RegionCode}`;
|
|
48
|
+
export type UrlSlug = string;
|
|
49
|
+
export type LangCode = `${Lowercase<string>}`;
|
|
50
|
+
export type RegionCode = `${Uppercase<string>}`;
|
|
51
|
+
export type ScriptCode = string;
|
|
52
|
+
export function from(locale: AnyCode | Locale): Locale;
|
|
53
|
+
export function isValid(locale: AnyCode | string): boolean;
|
|
54
|
+
export function isLanguageCode(locale: AnyCode | Locale, langCode: LangCode): boolean;
|
|
55
|
+
export function isRegionCode(locale: AnyCode | Locale, regionCode: Locale.RegionCode | LowercaseRegionCode): boolean;
|
|
56
|
+
export function isRegionIndependent(locale: AnyCode | Locale): boolean;
|
|
57
|
+
export function isEqual(a: Locale | AnyCode, b: Locale | AnyCode): boolean;
|
|
58
|
+
export function isSameLang(a: Locale | AnyCode, b: Locale | AnyCode): boolean;
|
|
59
|
+
export function isSameRegion(a: Locale | AnyCode, b: Locale | AnyCode): boolean;
|
|
60
|
+
export {};
|
|
61
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
2
|
+
|
|
3
|
+
import { isNotUndefined } from '@technically/is-not-undefined';
|
|
4
|
+
var KNOWN_SCRIPTS = ['hant', 'hans'];
|
|
5
|
+
var KNOWN_RTL_LANGUAGES = ['ar', 'he', 'ur'];
|
|
6
|
+
var LANG_CODE_REGEX = /^[a-z]{2,3}$/;
|
|
7
|
+
var CACHE = new Map();
|
|
8
|
+
export var Locale;
|
|
9
|
+
(function (_Locale) {
|
|
10
|
+
/**
|
|
11
|
+
* Shortest possible slug that can identify a locale for the given newsroom.
|
|
12
|
+
* This can be full language code or lowercase region code if they're unanbiguously identify a locale
|
|
13
|
+
* or full locale slug if nothing else applies.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
function from(locale) {
|
|
17
|
+
if (typeof locale === 'object') {
|
|
18
|
+
return locale;
|
|
19
|
+
}
|
|
20
|
+
var cached = CACHE.get(locale);
|
|
21
|
+
if (cached) {
|
|
22
|
+
return cached;
|
|
23
|
+
}
|
|
24
|
+
var [lang, ...rest] = locale.toLowerCase().split(/[_-]/g);
|
|
25
|
+
if (!lang.match(LANG_CODE_REGEX)) {
|
|
26
|
+
throw new Error("Invalid locale code provided: \"".concat(locale, "\"."));
|
|
27
|
+
}
|
|
28
|
+
var scripts = rest.filter(s => KNOWN_SCRIPTS.includes(s));
|
|
29
|
+
var regions = rest.filter(s => !KNOWN_SCRIPTS.includes(s)).map(region => region.toUpperCase());
|
|
30
|
+
if (scripts.length > 1) {
|
|
31
|
+
throw new Error("Unable to parse the provided locale code: \"".concat(locale, "\". Multiple script codes detected."));
|
|
32
|
+
}
|
|
33
|
+
if (regions.length > 1) {
|
|
34
|
+
throw new Error("Unable to parse the provided locale code: \"".concat(locale, "\". Multiple region codes detected."));
|
|
35
|
+
}
|
|
36
|
+
var scriptCode = scripts[0];
|
|
37
|
+
var regionCode = regions[0];
|
|
38
|
+
var code = [lang.toLowerCase(), regionCode, scriptCode ? toPascalCase(scriptCode) : undefined].filter(isNotUndefined).join('_');
|
|
39
|
+
var isoCode = code.replace('_', '-');
|
|
40
|
+
var obj = {
|
|
41
|
+
code,
|
|
42
|
+
isoCode,
|
|
43
|
+
slug: isoCode.toLowerCase(),
|
|
44
|
+
direction: KNOWN_RTL_LANGUAGES.includes(lang) ? 'rtl' : 'ltr',
|
|
45
|
+
lang,
|
|
46
|
+
region: regionCode,
|
|
47
|
+
script: scriptCode ? toPascalCase(scriptCode) : undefined
|
|
48
|
+
};
|
|
49
|
+
CACHE.set(obj.code, obj);
|
|
50
|
+
CACHE.set(obj.isoCode, obj);
|
|
51
|
+
CACHE.set(obj.slug, obj);
|
|
52
|
+
return obj;
|
|
53
|
+
}
|
|
54
|
+
_Locale.from = from;
|
|
55
|
+
function isValid(locale) {
|
|
56
|
+
try {
|
|
57
|
+
Locale.from(locale);
|
|
58
|
+
} catch (_unused) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
_Locale.isValid = isValid;
|
|
64
|
+
function isLanguageCode(locale, langCode) {
|
|
65
|
+
return Locale.from(locale).lang === langCode.toLowerCase();
|
|
66
|
+
}
|
|
67
|
+
_Locale.isLanguageCode = isLanguageCode;
|
|
68
|
+
function isRegionCode(locale, regionCode) {
|
|
69
|
+
return Locale.from(locale).region === regionCode.toUpperCase();
|
|
70
|
+
}
|
|
71
|
+
_Locale.isRegionCode = isRegionCode;
|
|
72
|
+
function isRegionIndependent(locale) {
|
|
73
|
+
return !Locale.from(locale).region;
|
|
74
|
+
}
|
|
75
|
+
_Locale.isRegionIndependent = isRegionIndependent;
|
|
76
|
+
function isEqual(a, b) {
|
|
77
|
+
var al = Locale.from(a);
|
|
78
|
+
var bl = Locale.from(b);
|
|
79
|
+
return al.lang === bl.lang && al.region === bl.region && al.script === bl.script;
|
|
80
|
+
}
|
|
81
|
+
_Locale.isEqual = isEqual;
|
|
82
|
+
function isSameLang(a, b) {
|
|
83
|
+
return Locale.from(a).lang === Locale.from(b).lang;
|
|
84
|
+
}
|
|
85
|
+
_Locale.isSameLang = isSameLang;
|
|
86
|
+
function isSameRegion(a, b) {
|
|
87
|
+
var al = Locale.from(a);
|
|
88
|
+
var bl = Locale.from(b);
|
|
89
|
+
if (!al.region || !bl.region) {
|
|
90
|
+
// It's never the same region, if it's not defined.
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
return al.region === bl.region;
|
|
94
|
+
}
|
|
95
|
+
_Locale.isSameRegion = isSameRegion;
|
|
96
|
+
})(Locale || (Locale = {}));
|
|
97
|
+
function toPascalCase(input) {
|
|
98
|
+
return "".concat(input.substring(0, 1).toUpperCase()).concat(input.substring(1).toLowerCase());
|
|
99
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _Locale = require("./Locale.cjs");
|
|
4
|
+
var _supportedLocales = require("./supportedLocales.cjs");
|
|
5
|
+
describe('Locale', () => {
|
|
6
|
+
it('should throw when created from invalid code', () => {
|
|
7
|
+
expect(() => _Locale.Locale.from('1234')).toThrow('Invalid locale code provided: "1234".');
|
|
8
|
+
expect(() => _Locale.Locale.from('*!en-US')).toThrow('Invalid locale code provided: "*!en-US".');
|
|
9
|
+
expect(() => _Locale.Locale.from('zh-Hant-Hans')).toThrow('Unable to parse the provided locale code: "zh-Hant-Hans". Multiple script codes detected.');
|
|
10
|
+
expect(() => _Locale.Locale.from('en_US_GB')).toThrow('Unable to parse the provided locale code: "en_US_GB". Multiple region codes detected.');
|
|
11
|
+
});
|
|
12
|
+
it('should accept valid codes', () => {
|
|
13
|
+
expect(() => _Locale.Locale.from('en')).not.toThrow();
|
|
14
|
+
expect(() => _Locale.Locale.from('en-us')).not.toThrow();
|
|
15
|
+
expect(() => _Locale.Locale.from('en-US')).not.toThrow();
|
|
16
|
+
expect(() => _Locale.Locale.from('EN-US')).not.toThrow();
|
|
17
|
+
expect(() => _Locale.Locale.from('en_us')).not.toThrow();
|
|
18
|
+
expect(() => _Locale.Locale.from('en_US')).not.toThrow();
|
|
19
|
+
expect(() => _Locale.Locale.from('EN_US')).not.toThrow();
|
|
20
|
+
expect(() => _Locale.Locale.from('zh-Hant')).not.toThrow();
|
|
21
|
+
expect(() => _Locale.Locale.from('zh-Hans')).not.toThrow();
|
|
22
|
+
expect(() => _Locale.Locale.from('es-419')).not.toThrow();
|
|
23
|
+
expect(() => _Locale.Locale.from('es_419')).not.toThrow();
|
|
24
|
+
});
|
|
25
|
+
it('should cache created locale objects', () => {
|
|
26
|
+
expect(_Locale.Locale.from('en_US')).toBe(_Locale.Locale.from('en-us'));
|
|
27
|
+
});
|
|
28
|
+
it('should provide underscore code', () => {
|
|
29
|
+
expect(_Locale.Locale.from('en').code).toBe('en');
|
|
30
|
+
expect(_Locale.Locale.from('en-us').code).toBe('en_US');
|
|
31
|
+
expect(_Locale.Locale.from('en-US').code).toBe('en_US');
|
|
32
|
+
expect(_Locale.Locale.from('EN-US').code).toBe('en_US');
|
|
33
|
+
expect(_Locale.Locale.from('en_us').code).toBe('en_US');
|
|
34
|
+
expect(_Locale.Locale.from('en_US').code).toBe('en_US');
|
|
35
|
+
expect(_Locale.Locale.from('EN_US').code).toBe('en_US');
|
|
36
|
+
});
|
|
37
|
+
it('should provide ISO code', () => {
|
|
38
|
+
expect(_Locale.Locale.from('en').isoCode).toBe('en');
|
|
39
|
+
expect(_Locale.Locale.from('en-us').isoCode).toBe('en-US');
|
|
40
|
+
expect(_Locale.Locale.from('en-US').isoCode).toBe('en-US');
|
|
41
|
+
expect(_Locale.Locale.from('EN-US').isoCode).toBe('en-US');
|
|
42
|
+
expect(_Locale.Locale.from('en_us').isoCode).toBe('en-US');
|
|
43
|
+
expect(_Locale.Locale.from('en_US').isoCode).toBe('en-US');
|
|
44
|
+
expect(_Locale.Locale.from('EN_US').isoCode).toBe('en-US');
|
|
45
|
+
});
|
|
46
|
+
it('should provide URL slug', () => {
|
|
47
|
+
expect(_Locale.Locale.from('en').slug).toBe('en');
|
|
48
|
+
expect(_Locale.Locale.from('en-us').slug).toBe('en-us');
|
|
49
|
+
expect(_Locale.Locale.from('en-US').slug).toBe('en-us');
|
|
50
|
+
expect(_Locale.Locale.from('EN-US').slug).toBe('en-us');
|
|
51
|
+
expect(_Locale.Locale.from('en_us').slug).toBe('en-us');
|
|
52
|
+
expect(_Locale.Locale.from('en_US').slug).toBe('en-us');
|
|
53
|
+
expect(_Locale.Locale.from('EN_US').slug).toBe('en-us');
|
|
54
|
+
});
|
|
55
|
+
it('should provide language code', () => {
|
|
56
|
+
expect(_Locale.Locale.from('en').lang).toBe('en');
|
|
57
|
+
expect(_Locale.Locale.from('en-us').lang).toBe('en');
|
|
58
|
+
expect(_Locale.Locale.from('en-US').lang).toBe('en');
|
|
59
|
+
expect(_Locale.Locale.from('EN-US').lang).toBe('en');
|
|
60
|
+
expect(_Locale.Locale.from('en_us').lang).toBe('en');
|
|
61
|
+
expect(_Locale.Locale.from('en_US').lang).toBe('en');
|
|
62
|
+
expect(_Locale.Locale.from('EN_US').lang).toBe('en');
|
|
63
|
+
});
|
|
64
|
+
it('should provide region code', () => {
|
|
65
|
+
expect(_Locale.Locale.from('en').region).toBe(undefined);
|
|
66
|
+
expect(_Locale.Locale.from('en-us').region).toBe('US');
|
|
67
|
+
expect(_Locale.Locale.from('en-US').region).toBe('US');
|
|
68
|
+
expect(_Locale.Locale.from('EN-US').region).toBe('US');
|
|
69
|
+
expect(_Locale.Locale.from('en_us').region).toBe('US');
|
|
70
|
+
expect(_Locale.Locale.from('en_US').region).toBe('US');
|
|
71
|
+
expect(_Locale.Locale.from('EN_US').region).toBe('US');
|
|
72
|
+
});
|
|
73
|
+
it('should check if two locales are equal', () => {
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
75
|
+
var en_US = _Locale.Locale.from('en-US');
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
77
|
+
var en_US2 = _Locale.Locale.from('en-US');
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
79
|
+
var de_DE = _Locale.Locale.from('de-DE');
|
|
80
|
+
expect(_Locale.Locale.isEqual(en_US, en_US2)).toBe(true);
|
|
81
|
+
expect(_Locale.Locale.isEqual(en_US2, en_US)).toBe(true);
|
|
82
|
+
expect(_Locale.Locale.isEqual(en_US, de_DE)).toBe(false);
|
|
83
|
+
expect(_Locale.Locale.isEqual(de_DE, en_US)).toBe(false);
|
|
84
|
+
expect(_Locale.Locale.isEqual('en_US', 'en-us')).toBe(true);
|
|
85
|
+
expect(_Locale.Locale.isEqual('en_US', _Locale.Locale.from('en_US'))).toBe(true);
|
|
86
|
+
});
|
|
87
|
+
it('should check if two locales are of the same language', () => {
|
|
88
|
+
expect(_Locale.Locale.isSameLang(_Locale.Locale.from('en-US'), _Locale.Locale.from('en-GB'))).toBe(true);
|
|
89
|
+
expect(_Locale.Locale.isSameLang('en-US', _Locale.Locale.from('en-GB'))).toBe(true);
|
|
90
|
+
expect(_Locale.Locale.isSameLang('en-us', 'en_GB')).toBe(true);
|
|
91
|
+
expect(_Locale.Locale.isSameLang('en-FR', 'en')).toBe(true);
|
|
92
|
+
expect(_Locale.Locale.isSameLang('zh_Hant', 'zh_Hans')).toBe(true);
|
|
93
|
+
expect(_Locale.Locale.isSameLang('en_US', 'de_DE')).toBe(false);
|
|
94
|
+
expect(_Locale.Locale.isSameLang('en_US', 'fr')).toBe(false);
|
|
95
|
+
});
|
|
96
|
+
it('should check if two locales are of the same region', () => {
|
|
97
|
+
expect(_Locale.Locale.isSameRegion(_Locale.Locale.from('en-US'), _Locale.Locale.from('es-US'))).toBe(true);
|
|
98
|
+
expect(_Locale.Locale.isSameRegion('en-US', _Locale.Locale.from('es-US'))).toBe(true);
|
|
99
|
+
expect(_Locale.Locale.isSameRegion('en-us', 'es_US')).toBe(true);
|
|
100
|
+
|
|
101
|
+
// Region-independent locales (with no region) are never considered of the same region.
|
|
102
|
+
expect(_Locale.Locale.isSameRegion('en', 'en')).toBe(false);
|
|
103
|
+
expect(_Locale.Locale.isSameRegion('en-US', 'es_ES')).toBe(false);
|
|
104
|
+
expect(_Locale.Locale.isSameRegion('en_US', 'de_DE')).toBe(false);
|
|
105
|
+
expect(_Locale.Locale.isSameRegion('en_US', 'fr')).toBe(false);
|
|
106
|
+
});
|
|
107
|
+
it('should tell language direction', () => {
|
|
108
|
+
expect(_Locale.Locale.from('ar_AR').direction).toBe('rtl');
|
|
109
|
+
expect(_Locale.Locale.from('he').direction).toBe('rtl');
|
|
110
|
+
expect(_Locale.Locale.from('ur-pk').direction).toBe('rtl');
|
|
111
|
+
expect(_Locale.Locale.from('en_GB').direction).toBe('ltr');
|
|
112
|
+
expect(_Locale.Locale.from('fr').direction).toBe('ltr');
|
|
113
|
+
expect(_Locale.Locale.from('zh_Hant').direction).toBe('ltr');
|
|
114
|
+
});
|
|
115
|
+
it('should work for all supported locales', () => {
|
|
116
|
+
_supportedLocales.supportedLocales.forEach(code => {
|
|
117
|
+
expect(() => _Locale.Locale.from(code)).not.toThrow();
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { Locale } from "./Locale.mjs";
|
|
2
|
+
import { supportedLocales } from "./supportedLocales.mjs";
|
|
3
|
+
describe('Locale', () => {
|
|
4
|
+
it('should throw when created from invalid code', () => {
|
|
5
|
+
expect(() => Locale.from('1234')).toThrow('Invalid locale code provided: "1234".');
|
|
6
|
+
expect(() => Locale.from('*!en-US')).toThrow('Invalid locale code provided: "*!en-US".');
|
|
7
|
+
expect(() => Locale.from('zh-Hant-Hans')).toThrow('Unable to parse the provided locale code: "zh-Hant-Hans". Multiple script codes detected.');
|
|
8
|
+
expect(() => Locale.from('en_US_GB')).toThrow('Unable to parse the provided locale code: "en_US_GB". Multiple region codes detected.');
|
|
9
|
+
});
|
|
10
|
+
it('should accept valid codes', () => {
|
|
11
|
+
expect(() => Locale.from('en')).not.toThrow();
|
|
12
|
+
expect(() => Locale.from('en-us')).not.toThrow();
|
|
13
|
+
expect(() => Locale.from('en-US')).not.toThrow();
|
|
14
|
+
expect(() => Locale.from('EN-US')).not.toThrow();
|
|
15
|
+
expect(() => Locale.from('en_us')).not.toThrow();
|
|
16
|
+
expect(() => Locale.from('en_US')).not.toThrow();
|
|
17
|
+
expect(() => Locale.from('EN_US')).not.toThrow();
|
|
18
|
+
expect(() => Locale.from('zh-Hant')).not.toThrow();
|
|
19
|
+
expect(() => Locale.from('zh-Hans')).not.toThrow();
|
|
20
|
+
expect(() => Locale.from('es-419')).not.toThrow();
|
|
21
|
+
expect(() => Locale.from('es_419')).not.toThrow();
|
|
22
|
+
});
|
|
23
|
+
it('should cache created locale objects', () => {
|
|
24
|
+
expect(Locale.from('en_US')).toBe(Locale.from('en-us'));
|
|
25
|
+
});
|
|
26
|
+
it('should provide underscore code', () => {
|
|
27
|
+
expect(Locale.from('en').code).toBe('en');
|
|
28
|
+
expect(Locale.from('en-us').code).toBe('en_US');
|
|
29
|
+
expect(Locale.from('en-US').code).toBe('en_US');
|
|
30
|
+
expect(Locale.from('EN-US').code).toBe('en_US');
|
|
31
|
+
expect(Locale.from('en_us').code).toBe('en_US');
|
|
32
|
+
expect(Locale.from('en_US').code).toBe('en_US');
|
|
33
|
+
expect(Locale.from('EN_US').code).toBe('en_US');
|
|
34
|
+
});
|
|
35
|
+
it('should provide ISO code', () => {
|
|
36
|
+
expect(Locale.from('en').isoCode).toBe('en');
|
|
37
|
+
expect(Locale.from('en-us').isoCode).toBe('en-US');
|
|
38
|
+
expect(Locale.from('en-US').isoCode).toBe('en-US');
|
|
39
|
+
expect(Locale.from('EN-US').isoCode).toBe('en-US');
|
|
40
|
+
expect(Locale.from('en_us').isoCode).toBe('en-US');
|
|
41
|
+
expect(Locale.from('en_US').isoCode).toBe('en-US');
|
|
42
|
+
expect(Locale.from('EN_US').isoCode).toBe('en-US');
|
|
43
|
+
});
|
|
44
|
+
it('should provide URL slug', () => {
|
|
45
|
+
expect(Locale.from('en').slug).toBe('en');
|
|
46
|
+
expect(Locale.from('en-us').slug).toBe('en-us');
|
|
47
|
+
expect(Locale.from('en-US').slug).toBe('en-us');
|
|
48
|
+
expect(Locale.from('EN-US').slug).toBe('en-us');
|
|
49
|
+
expect(Locale.from('en_us').slug).toBe('en-us');
|
|
50
|
+
expect(Locale.from('en_US').slug).toBe('en-us');
|
|
51
|
+
expect(Locale.from('EN_US').slug).toBe('en-us');
|
|
52
|
+
});
|
|
53
|
+
it('should provide language code', () => {
|
|
54
|
+
expect(Locale.from('en').lang).toBe('en');
|
|
55
|
+
expect(Locale.from('en-us').lang).toBe('en');
|
|
56
|
+
expect(Locale.from('en-US').lang).toBe('en');
|
|
57
|
+
expect(Locale.from('EN-US').lang).toBe('en');
|
|
58
|
+
expect(Locale.from('en_us').lang).toBe('en');
|
|
59
|
+
expect(Locale.from('en_US').lang).toBe('en');
|
|
60
|
+
expect(Locale.from('EN_US').lang).toBe('en');
|
|
61
|
+
});
|
|
62
|
+
it('should provide region code', () => {
|
|
63
|
+
expect(Locale.from('en').region).toBe(undefined);
|
|
64
|
+
expect(Locale.from('en-us').region).toBe('US');
|
|
65
|
+
expect(Locale.from('en-US').region).toBe('US');
|
|
66
|
+
expect(Locale.from('EN-US').region).toBe('US');
|
|
67
|
+
expect(Locale.from('en_us').region).toBe('US');
|
|
68
|
+
expect(Locale.from('en_US').region).toBe('US');
|
|
69
|
+
expect(Locale.from('EN_US').region).toBe('US');
|
|
70
|
+
});
|
|
71
|
+
it('should check if two locales are equal', () => {
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
73
|
+
var en_US = Locale.from('en-US');
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
75
|
+
var en_US2 = Locale.from('en-US');
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
77
|
+
var de_DE = Locale.from('de-DE');
|
|
78
|
+
expect(Locale.isEqual(en_US, en_US2)).toBe(true);
|
|
79
|
+
expect(Locale.isEqual(en_US2, en_US)).toBe(true);
|
|
80
|
+
expect(Locale.isEqual(en_US, de_DE)).toBe(false);
|
|
81
|
+
expect(Locale.isEqual(de_DE, en_US)).toBe(false);
|
|
82
|
+
expect(Locale.isEqual('en_US', 'en-us')).toBe(true);
|
|
83
|
+
expect(Locale.isEqual('en_US', Locale.from('en_US'))).toBe(true);
|
|
84
|
+
});
|
|
85
|
+
it('should check if two locales are of the same language', () => {
|
|
86
|
+
expect(Locale.isSameLang(Locale.from('en-US'), Locale.from('en-GB'))).toBe(true);
|
|
87
|
+
expect(Locale.isSameLang('en-US', Locale.from('en-GB'))).toBe(true);
|
|
88
|
+
expect(Locale.isSameLang('en-us', 'en_GB')).toBe(true);
|
|
89
|
+
expect(Locale.isSameLang('en-FR', 'en')).toBe(true);
|
|
90
|
+
expect(Locale.isSameLang('zh_Hant', 'zh_Hans')).toBe(true);
|
|
91
|
+
expect(Locale.isSameLang('en_US', 'de_DE')).toBe(false);
|
|
92
|
+
expect(Locale.isSameLang('en_US', 'fr')).toBe(false);
|
|
93
|
+
});
|
|
94
|
+
it('should check if two locales are of the same region', () => {
|
|
95
|
+
expect(Locale.isSameRegion(Locale.from('en-US'), Locale.from('es-US'))).toBe(true);
|
|
96
|
+
expect(Locale.isSameRegion('en-US', Locale.from('es-US'))).toBe(true);
|
|
97
|
+
expect(Locale.isSameRegion('en-us', 'es_US')).toBe(true);
|
|
98
|
+
|
|
99
|
+
// Region-independent locales (with no region) are never considered of the same region.
|
|
100
|
+
expect(Locale.isSameRegion('en', 'en')).toBe(false);
|
|
101
|
+
expect(Locale.isSameRegion('en-US', 'es_ES')).toBe(false);
|
|
102
|
+
expect(Locale.isSameRegion('en_US', 'de_DE')).toBe(false);
|
|
103
|
+
expect(Locale.isSameRegion('en_US', 'fr')).toBe(false);
|
|
104
|
+
});
|
|
105
|
+
it('should tell language direction', () => {
|
|
106
|
+
expect(Locale.from('ar_AR').direction).toBe('rtl');
|
|
107
|
+
expect(Locale.from('he').direction).toBe('rtl');
|
|
108
|
+
expect(Locale.from('ur-pk').direction).toBe('rtl');
|
|
109
|
+
expect(Locale.from('en_GB').direction).toBe('ltr');
|
|
110
|
+
expect(Locale.from('fr').direction).toBe('ltr');
|
|
111
|
+
expect(Locale.from('zh_Hant').direction).toBe('ltr');
|
|
112
|
+
});
|
|
113
|
+
it('should work for all supported locales', () => {
|
|
114
|
+
supportedLocales.forEach(code => {
|
|
115
|
+
expect(() => Locale.from(code)).not.toThrow();
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
});
|