@shoelace-style/localize 3.2.0 → 3.2.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/CHANGELOG.md +9 -0
- package/dist/index.js +28 -13
- package/package.json +1 -1
- package/src/index.ts +46 -19
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 3.2.2
|
|
4
|
+
|
|
5
|
+
- Fixed a bug where malformed `<html lang>` values caused `Intl.Locale` to throw a `RangeError`. The controller now falls through to the fallback translation instead.
|
|
6
|
+
- Fixed a type error in `update()` where connected elements were incorrectly annotated as `LitElement`
|
|
7
|
+
|
|
8
|
+
## 3.2.1
|
|
9
|
+
|
|
10
|
+
- Fixed a bug where the maintainer forgot to merge the PR before publishing
|
|
11
|
+
|
|
3
12
|
## 3.2.0
|
|
4
13
|
|
|
5
14
|
- Added support for SSR environments [#25](https://github.com/shoelace-style/localize/pull/25/)
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
const connectedElements = new Set();
|
|
2
|
-
const documentElementObserver = new MutationObserver(update);
|
|
3
2
|
const translations = new Map();
|
|
4
|
-
let documentDirection = document.documentElement.dir || 'ltr';
|
|
5
|
-
let documentLanguage = document.documentElement.lang || navigator.language;
|
|
6
3
|
let fallback;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
let documentDirection = 'ltr';
|
|
5
|
+
let documentLanguage = 'en';
|
|
6
|
+
const isClient = typeof MutationObserver !== 'undefined' &&
|
|
7
|
+
typeof document !== 'undefined' &&
|
|
8
|
+
typeof document.documentElement !== 'undefined';
|
|
9
|
+
if (isClient) {
|
|
10
|
+
const documentElementObserver = new MutationObserver(update);
|
|
11
|
+
documentDirection = document.documentElement.dir || 'ltr';
|
|
12
|
+
documentLanguage = document.documentElement.lang || navigator.language;
|
|
13
|
+
documentElementObserver.observe(document.documentElement, {
|
|
14
|
+
attributes: true,
|
|
15
|
+
attributeFilter: ['dir', 'lang']
|
|
16
|
+
});
|
|
17
|
+
}
|
|
11
18
|
export function registerTranslation(...translation) {
|
|
12
19
|
translation.map(t => {
|
|
13
20
|
const code = t.$code.toLowerCase();
|
|
@@ -24,9 +31,11 @@ export function registerTranslation(...translation) {
|
|
|
24
31
|
update();
|
|
25
32
|
}
|
|
26
33
|
export function update() {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
34
|
+
if (isClient) {
|
|
35
|
+
documentDirection = document.documentElement.dir || 'ltr';
|
|
36
|
+
documentLanguage = document.documentElement.lang || navigator.language;
|
|
37
|
+
}
|
|
38
|
+
[...connectedElements.keys()].map(el => {
|
|
30
39
|
if (typeof el.requestUpdate === 'function') {
|
|
31
40
|
el.requestUpdate();
|
|
32
41
|
}
|
|
@@ -51,9 +60,15 @@ export class LocalizeController {
|
|
|
51
60
|
}
|
|
52
61
|
getTranslationData(lang) {
|
|
53
62
|
var _a, _b;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
63
|
+
let locale;
|
|
64
|
+
try {
|
|
65
|
+
locale = new Intl.Locale(lang.replace(/_/g, '-'));
|
|
66
|
+
}
|
|
67
|
+
catch (_c) {
|
|
68
|
+
return { locale: undefined, language: '', region: '', primary: undefined, secondary: undefined };
|
|
69
|
+
}
|
|
70
|
+
const language = locale.language.toLowerCase();
|
|
71
|
+
const region = (_b = (_a = locale.region) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : '';
|
|
57
72
|
const primary = translations.get(`${language}-${region}`);
|
|
58
73
|
const secondary = translations.get(language);
|
|
59
74
|
return { locale, language, region, primary, secondary };
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -18,17 +18,32 @@ export interface ExistsOptions {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
const connectedElements = new Set<HTMLElement>();
|
|
21
|
-
const documentElementObserver = new MutationObserver(update);
|
|
22
21
|
const translations: Map<string, Translation> = new Map();
|
|
23
|
-
|
|
24
|
-
let documentLanguage = document.documentElement.lang || navigator.language;
|
|
22
|
+
|
|
25
23
|
let fallback: Translation;
|
|
26
24
|
|
|
27
|
-
//
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
// TODO: We need some way for users to be able to set these on the server.
|
|
26
|
+
let documentDirection = 'ltr';
|
|
27
|
+
|
|
28
|
+
// Fallback for server.
|
|
29
|
+
let documentLanguage = 'en';
|
|
30
|
+
|
|
31
|
+
const isClient =
|
|
32
|
+
typeof MutationObserver !== 'undefined' &&
|
|
33
|
+
typeof document !== 'undefined' &&
|
|
34
|
+
typeof document.documentElement !== 'undefined';
|
|
35
|
+
|
|
36
|
+
if (isClient) {
|
|
37
|
+
const documentElementObserver = new MutationObserver(update);
|
|
38
|
+
documentDirection = document.documentElement.dir || 'ltr';
|
|
39
|
+
documentLanguage = document.documentElement.lang || navigator.language;
|
|
40
|
+
|
|
41
|
+
// Watch for changes on <html lang>
|
|
42
|
+
documentElementObserver.observe(document.documentElement, {
|
|
43
|
+
attributes: true,
|
|
44
|
+
attributeFilter: ['dir', 'lang']
|
|
45
|
+
});
|
|
46
|
+
}
|
|
32
47
|
|
|
33
48
|
/** Registers one or more translations */
|
|
34
49
|
export function registerTranslation(...translation: Translation[]) {
|
|
@@ -53,12 +68,14 @@ export function registerTranslation(...translation: Translation[]) {
|
|
|
53
68
|
|
|
54
69
|
/** Updates all localized elements that are currently connected */
|
|
55
70
|
export function update() {
|
|
56
|
-
|
|
57
|
-
|
|
71
|
+
if (isClient) {
|
|
72
|
+
documentDirection = document.documentElement.dir || 'ltr';
|
|
73
|
+
documentLanguage = document.documentElement.lang || navigator.language;
|
|
74
|
+
}
|
|
58
75
|
|
|
59
|
-
[...connectedElements.keys()].map(
|
|
60
|
-
if (typeof el.requestUpdate === 'function') {
|
|
61
|
-
el.requestUpdate();
|
|
76
|
+
[...connectedElements.keys()].map(el => {
|
|
77
|
+
if (typeof (el as Partial<LitElement>).requestUpdate === 'function') {
|
|
78
|
+
(el as LitElement).requestUpdate();
|
|
62
79
|
}
|
|
63
80
|
});
|
|
64
81
|
}
|
|
@@ -82,9 +99,9 @@ export function update() {
|
|
|
82
99
|
* ${this.localize.date('2021-12-03')}
|
|
83
100
|
* ${this.localize.number(1000000)}
|
|
84
101
|
*/
|
|
85
|
-
export class LocalizeController<
|
|
86
|
-
|
|
87
|
-
{
|
|
102
|
+
export class LocalizeController<
|
|
103
|
+
UserTranslation extends Translation = DefaultTranslation
|
|
104
|
+
> implements ReactiveController {
|
|
88
105
|
host: ReactiveControllerHost & HTMLElement;
|
|
89
106
|
|
|
90
107
|
constructor(host: ReactiveControllerHost & HTMLElement) {
|
|
@@ -117,11 +134,21 @@ export class LocalizeController<UserTranslation extends Translation = DefaultTra
|
|
|
117
134
|
}
|
|
118
135
|
|
|
119
136
|
private getTranslationData(lang: string) {
|
|
137
|
+
//
|
|
120
138
|
// Convert "en_US" to "en-US". Note that both underscores and dashes are allowed per spec, but underscores result in
|
|
121
139
|
// a RangeError by the call to `new Intl.Locale()`. See: https://unicode.org/reports/tr35/#unicode-locale-identifier
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
140
|
+
//
|
|
141
|
+
// Some environments (such as Chrome's built-in page translation) set `<html lang>` to abnormal values, e.g.
|
|
142
|
+
// `en-x-mtfrom-nb`. This try/catch guards against this so we fall through to the fallback translation.
|
|
143
|
+
//
|
|
144
|
+
let locale: Intl.Locale | undefined;
|
|
145
|
+
try {
|
|
146
|
+
locale = new Intl.Locale(lang.replace(/_/g, '-'));
|
|
147
|
+
} catch {
|
|
148
|
+
return { locale: undefined, language: '', region: '', primary: undefined, secondary: undefined };
|
|
149
|
+
}
|
|
150
|
+
const language = locale.language.toLowerCase();
|
|
151
|
+
const region = locale.region?.toLowerCase() ?? '';
|
|
125
152
|
const primary = <UserTranslation>translations.get(`${language}-${region}`);
|
|
126
153
|
const secondary = <UserTranslation>translations.get(language);
|
|
127
154
|
|