@shoelace-style/localize 3.2.1 → 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 +5 -0
- package/dist/index.js +13 -5
- package/package.json +1 -1
- package/src/index.ts +25 -13
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
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
|
+
|
|
3
8
|
## 3.2.1
|
|
4
9
|
|
|
5
10
|
- Fixed a bug where the maintainer forgot to merge the PR before publishing
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,9 @@ const translations = new Map();
|
|
|
3
3
|
let fallback;
|
|
4
4
|
let documentDirection = 'ltr';
|
|
5
5
|
let documentLanguage = 'en';
|
|
6
|
-
const isClient =
|
|
6
|
+
const isClient = typeof MutationObserver !== 'undefined' &&
|
|
7
|
+
typeof document !== 'undefined' &&
|
|
8
|
+
typeof document.documentElement !== 'undefined';
|
|
7
9
|
if (isClient) {
|
|
8
10
|
const documentElementObserver = new MutationObserver(update);
|
|
9
11
|
documentDirection = document.documentElement.dir || 'ltr';
|
|
@@ -33,7 +35,7 @@ export function update() {
|
|
|
33
35
|
documentDirection = document.documentElement.dir || 'ltr';
|
|
34
36
|
documentLanguage = document.documentElement.lang || navigator.language;
|
|
35
37
|
}
|
|
36
|
-
[...connectedElements.keys()].map(
|
|
38
|
+
[...connectedElements.keys()].map(el => {
|
|
37
39
|
if (typeof el.requestUpdate === 'function') {
|
|
38
40
|
el.requestUpdate();
|
|
39
41
|
}
|
|
@@ -58,9 +60,15 @@ export class LocalizeController {
|
|
|
58
60
|
}
|
|
59
61
|
getTranslationData(lang) {
|
|
60
62
|
var _a, _b;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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 : '';
|
|
64
72
|
const primary = translations.get(`${language}-${region}`);
|
|
65
73
|
const secondary = translations.get(language);
|
|
66
74
|
return { locale, language, region, primary, secondary };
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -22,14 +22,16 @@ const translations: Map<string, Translation> = new Map();
|
|
|
22
22
|
|
|
23
23
|
let fallback: Translation;
|
|
24
24
|
|
|
25
|
-
|
|
26
25
|
// TODO: We need some way for users to be able to set these on the server.
|
|
27
|
-
let documentDirection = 'ltr'
|
|
26
|
+
let documentDirection = 'ltr';
|
|
28
27
|
|
|
29
28
|
// Fallback for server.
|
|
30
|
-
let documentLanguage = 'en'
|
|
29
|
+
let documentLanguage = 'en';
|
|
31
30
|
|
|
32
|
-
const isClient =
|
|
31
|
+
const isClient =
|
|
32
|
+
typeof MutationObserver !== 'undefined' &&
|
|
33
|
+
typeof document !== 'undefined' &&
|
|
34
|
+
typeof document.documentElement !== 'undefined';
|
|
33
35
|
|
|
34
36
|
if (isClient) {
|
|
35
37
|
const documentElementObserver = new MutationObserver(update);
|
|
@@ -71,9 +73,9 @@ export function update() {
|
|
|
71
73
|
documentLanguage = document.documentElement.lang || navigator.language;
|
|
72
74
|
}
|
|
73
75
|
|
|
74
|
-
[...connectedElements.keys()].map(
|
|
75
|
-
if (typeof el.requestUpdate === 'function') {
|
|
76
|
-
el.requestUpdate();
|
|
76
|
+
[...connectedElements.keys()].map(el => {
|
|
77
|
+
if (typeof (el as Partial<LitElement>).requestUpdate === 'function') {
|
|
78
|
+
(el as LitElement).requestUpdate();
|
|
77
79
|
}
|
|
78
80
|
});
|
|
79
81
|
}
|
|
@@ -97,9 +99,9 @@ export function update() {
|
|
|
97
99
|
* ${this.localize.date('2021-12-03')}
|
|
98
100
|
* ${this.localize.number(1000000)}
|
|
99
101
|
*/
|
|
100
|
-
export class LocalizeController<
|
|
101
|
-
|
|
102
|
-
{
|
|
102
|
+
export class LocalizeController<
|
|
103
|
+
UserTranslation extends Translation = DefaultTranslation
|
|
104
|
+
> implements ReactiveController {
|
|
103
105
|
host: ReactiveControllerHost & HTMLElement;
|
|
104
106
|
|
|
105
107
|
constructor(host: ReactiveControllerHost & HTMLElement) {
|
|
@@ -132,11 +134,21 @@ export class LocalizeController<UserTranslation extends Translation = DefaultTra
|
|
|
132
134
|
}
|
|
133
135
|
|
|
134
136
|
private getTranslationData(lang: string) {
|
|
137
|
+
//
|
|
135
138
|
// Convert "en_US" to "en-US". Note that both underscores and dashes are allowed per spec, but underscores result in
|
|
136
139
|
// a RangeError by the call to `new Intl.Locale()`. See: https://unicode.org/reports/tr35/#unicode-locale-identifier
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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() ?? '';
|
|
140
152
|
const primary = <UserTranslation>translations.get(`${language}-${region}`);
|
|
141
153
|
const secondary = <UserTranslation>translations.get(language);
|
|
142
154
|
|