@shoelace-style/localize 3.2.1 → 3.2.3

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 CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## 3.2.3
4
+
5
+ - Fixed a bug where invalid language tags, e.g. `auto` as set by Chrome Translate's "Detect language" feature, caused `date()`, `number()`, and `relativeTime()` to throw a `RangeError`
6
+ - Fixed a bug where underscores in locale identifiers, e.g. `en_US`, caused `date()`, `number()`, and `relativeTime()` to throw a `RangeError`
7
+
8
+ ## 3.2.2
9
+
10
+ - 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.
11
+ - Fixed a type error in `update()` where connected elements were incorrectly annotated as `LitElement`
12
+
3
13
  ## 3.2.1
4
14
 
5
15
  - 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 = (typeof MutationObserver !== "undefined" && typeof document !== "undefined" && typeof document.documentElement !== "undefined");
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((el) => {
38
+ [...connectedElements.keys()].map(el => {
37
39
  if (typeof el.requestUpdate === 'function') {
38
40
  el.requestUpdate();
39
41
  }
@@ -54,13 +56,26 @@ export class LocalizeController {
54
56
  return `${this.host.dir || documentDirection}`.toLowerCase();
55
57
  }
56
58
  lang() {
57
- return `${this.host.lang || documentLanguage}`.toLowerCase();
59
+ const lang = `${this.host.lang || documentLanguage}`.toLowerCase().replace(/_/g, '-');
60
+ try {
61
+ new Intl.Locale(lang);
62
+ return lang;
63
+ }
64
+ catch (_a) {
65
+ return fallback ? fallback.$code.toLowerCase() : 'en';
66
+ }
58
67
  }
59
68
  getTranslationData(lang) {
60
69
  var _a, _b;
61
- const locale = new Intl.Locale(lang.replace(/_/g, '-'));
62
- const language = locale === null || locale === void 0 ? void 0 : locale.language.toLowerCase();
63
- const region = (_b = (_a = locale === null || locale === void 0 ? void 0 : locale.region) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : '';
70
+ let locale;
71
+ try {
72
+ locale = new Intl.Locale(lang.replace(/_/g, '-'));
73
+ }
74
+ catch (_c) {
75
+ return { locale: undefined, language: '', region: '', primary: undefined, secondary: undefined };
76
+ }
77
+ const language = locale.language.toLowerCase();
78
+ const region = (_b = (_a = locale.region) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : '';
64
79
  const primary = translations.get(`${language}-${region}`);
65
80
  const secondary = translations.get(language);
66
81
  return { locale, language, region, primary, secondary };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shoelace-style/localize",
3
- "version": "3.2.1",
3
+ "version": "3.2.3",
4
4
  "description": "A micro library for localizing custom elements using Lit's Reactive Controller model.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
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 = (typeof MutationObserver !== "undefined" && typeof document !== "undefined" && typeof document.documentElement !== "undefined")
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((el: LitElement) => {
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<UserTranslation extends Translation = DefaultTranslation>
101
- implements ReactiveController
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) {
@@ -125,18 +127,45 @@ export class LocalizeController<UserTranslation extends Translation = DefaultTra
125
127
 
126
128
  /**
127
129
  * Gets the host element's language as determined by the `lang` attribute. The return value is transformed to
128
- * lowercase.
130
+ * lowercase. If the resolved value isn't a valid language tag, the fallback translation's language is returned
131
+ * instead so it can be passed safely to the `Intl` APIs.
129
132
  */
130
133
  lang() {
131
- return `${this.host.lang || documentLanguage}`.toLowerCase();
134
+ //
135
+ // Convert "en_US" to "en-US". Note that both underscores and dashes are allowed per spec, but underscores result in
136
+ // a RangeError in the Intl APIs. See: https://unicode.org/reports/tr35/#unicode-locale-identifier
137
+ //
138
+ const lang = `${this.host.lang || documentLanguage}`.toLowerCase().replace(/_/g, '-');
139
+
140
+ //
141
+ // Some environments set `<html lang>` to an invalid language tag at runtime, e.g. Chrome Translate's "Detect
142
+ // language" sets it to `auto`. Passing an invalid tag to `Intl.NumberFormat` et al throws a RangeError, so we fall
143
+ // back to the fallback translation's language when the tag is invalid.
144
+ //
145
+ try {
146
+ new Intl.Locale(lang);
147
+ return lang;
148
+ } catch {
149
+ return fallback ? fallback.$code.toLowerCase() : 'en';
150
+ }
132
151
  }
133
152
 
134
153
  private getTranslationData(lang: string) {
154
+ //
135
155
  // Convert "en_US" to "en-US". Note that both underscores and dashes are allowed per spec, but underscores result in
136
156
  // a RangeError by the call to `new Intl.Locale()`. See: https://unicode.org/reports/tr35/#unicode-locale-identifier
137
- const locale = new Intl.Locale(lang.replace(/_/g, '-'));
138
- const language = locale?.language.toLowerCase();
139
- const region = locale?.region?.toLowerCase() ?? '';
157
+ //
158
+ // Some environments (such as Chrome's built-in page translation) set `<html lang>` to abnormal values, e.g.
159
+ // `en-x-mtfrom-nb`. This try/catch guards against this so we fall through to the fallback translation.
160
+ //
161
+ let locale: Intl.Locale | undefined;
162
+ try {
163
+ locale = new Intl.Locale(lang.replace(/_/g, '-'));
164
+ } catch {
165
+ return { locale: undefined, language: '', region: '', primary: undefined, secondary: undefined };
166
+ }
167
+ const language = locale.language.toLowerCase();
168
+ const region = locale.region?.toLowerCase() ?? '';
140
169
  const primary = <UserTranslation>translations.get(`${language}-${region}`);
141
170
  const secondary = <UserTranslation>translations.get(language);
142
171