@shoelace-style/localize 3.2.0 → 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 +4 -0
- package/dist/index.js +16 -9
- package/package.json +1 -1
- package/src/index.ts +25 -10
package/CHANGELOG.md
CHANGED
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
28
|
-
|
|
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
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
|
-
|
|
24
|
-
let documentLanguage = document.documentElement.lang || navigator.language;
|
|
22
|
+
|
|
25
23
|
let fallback: Translation;
|
|
26
24
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
57
|
-
|
|
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') {
|