@shoelace-style/localize 3.1.2 → 3.2.1

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,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 3.2.1
4
+
5
+ - Fixed a bug where the maintainer forgot to merge the PR before publishing
6
+
7
+ ## 3.2.0
8
+
9
+ - Added support for SSR environments [#25](https://github.com/shoelace-style/localize/pull/25/)
10
+
3
11
  ## 3.1.2
4
12
 
5
13
  - Fixed a bug that caused underscores in locale identifiers to throw a `RangeError`
package/dist/index.js CHANGED
@@ -1,13 +1,18 @@
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
- documentElementObserver.observe(document.documentElement, {
8
- attributes: true,
9
- attributeFilter: ['dir', 'lang']
10
- });
4
+ let documentDirection = 'ltr';
5
+ let documentLanguage = 'en';
6
+ const isClient = (typeof MutationObserver !== "undefined" && typeof document !== "undefined" && typeof document.documentElement !== "undefined");
7
+ if (isClient) {
8
+ const documentElementObserver = new MutationObserver(update);
9
+ documentDirection = document.documentElement.dir || 'ltr';
10
+ documentLanguage = document.documentElement.lang || navigator.language;
11
+ documentElementObserver.observe(document.documentElement, {
12
+ attributes: true,
13
+ attributeFilter: ['dir', 'lang']
14
+ });
15
+ }
11
16
  export function registerTranslation(...translation) {
12
17
  translation.map(t => {
13
18
  const code = t.$code.toLowerCase();
@@ -24,8 +29,10 @@ export function registerTranslation(...translation) {
24
29
  update();
25
30
  }
26
31
  export function update() {
27
- documentDirection = document.documentElement.dir || 'ltr';
28
- documentLanguage = document.documentElement.lang || navigator.language;
32
+ if (isClient) {
33
+ documentDirection = document.documentElement.dir || 'ltr';
34
+ documentLanguage = document.documentElement.lang || navigator.language;
35
+ }
29
36
  [...connectedElements.keys()].map((el) => {
30
37
  if (typeof el.requestUpdate === 'function') {
31
38
  el.requestUpdate();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shoelace-style/localize",
3
- "version": "3.1.2",
3
+ "version": "3.2.1",
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
@@ -18,17 +18,30 @@ 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
- let documentDirection = document.documentElement.dir || 'ltr';
24
- let documentLanguage = document.documentElement.lang || navigator.language;
22
+
25
23
  let fallback: Translation;
26
24
 
27
- // Watch for changes on <html lang>
28
- documentElementObserver.observe(document.documentElement, {
29
- attributes: true,
30
- attributeFilter: ['dir', 'lang']
31
- });
25
+
26
+ // TODO: We need some way for users to be able to set these on the server.
27
+ let documentDirection = 'ltr'
28
+
29
+ // Fallback for server.
30
+ let documentLanguage = 'en'
31
+
32
+ const isClient = (typeof MutationObserver !== "undefined" && typeof document !== "undefined" && typeof document.documentElement !== "undefined")
33
+
34
+ if (isClient) {
35
+ const documentElementObserver = new MutationObserver(update);
36
+ documentDirection = document.documentElement.dir || 'ltr';
37
+ documentLanguage = document.documentElement.lang || navigator.language;
38
+
39
+ // Watch for changes on <html lang>
40
+ documentElementObserver.observe(document.documentElement, {
41
+ attributes: true,
42
+ attributeFilter: ['dir', 'lang']
43
+ });
44
+ }
32
45
 
33
46
  /** Registers one or more translations */
34
47
  export function registerTranslation(...translation: Translation[]) {
@@ -53,8 +66,10 @@ export function registerTranslation(...translation: Translation[]) {
53
66
 
54
67
  /** Updates all localized elements that are currently connected */
55
68
  export function update() {
56
- documentDirection = document.documentElement.dir || 'ltr';
57
- documentLanguage = document.documentElement.lang || navigator.language;
69
+ if (isClient) {
70
+ documentDirection = document.documentElement.dir || 'ltr';
71
+ documentLanguage = document.documentElement.lang || navigator.language;
72
+ }
58
73
 
59
74
  [...connectedElements.keys()].map((el: LitElement) => {
60
75
  if (typeof el.requestUpdate === 'function') {