@shoelace-style/localize 3.2.2 → 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,10 @@
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
+
3
8
  ## 3.2.2
4
9
 
5
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.
package/dist/index.js CHANGED
@@ -56,7 +56,14 @@ export class LocalizeController {
56
56
  return `${this.host.dir || documentDirection}`.toLowerCase();
57
57
  }
58
58
  lang() {
59
- 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
+ }
60
67
  }
61
68
  getTranslationData(lang) {
62
69
  var _a, _b;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shoelace-style/localize",
3
- "version": "3.2.2",
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
@@ -127,10 +127,27 @@ export class LocalizeController<
127
127
 
128
128
  /**
129
129
  * Gets the host element's language as determined by the `lang` attribute. The return value is transformed to
130
- * 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.
131
132
  */
132
133
  lang() {
133
- 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
+ }
134
151
  }
135
152
 
136
153
  private getTranslationData(lang: string) {