@shoelace-style/localize 2.0.0 → 2.1.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 +21 -0
- package/README.md +3 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +9 -3
- package/package.json +2 -4
- package/src/index.ts +21 -5
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 2.1.3
|
|
4
|
+
|
|
5
|
+
- Renamed `updateLocalizedTerms()` to `update()` (forgive me SemVer, but nobody was using this I promise)
|
|
6
|
+
|
|
7
|
+
## 2.1.2
|
|
8
|
+
|
|
9
|
+
- Removed all dependencies
|
|
10
|
+
|
|
11
|
+
## 2.1.1
|
|
12
|
+
|
|
13
|
+
- Change import to ensure only types get used
|
|
14
|
+
|
|
15
|
+
## 2.1.0
|
|
16
|
+
|
|
17
|
+
- Added relative time method to
|
|
18
|
+
|
|
19
|
+
## 2.0.0
|
|
20
|
+
|
|
21
|
+
- Reworked the library to use the [ReactiveController](https://lit.dev/docs/composition/controllers/) interface
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Shoelace: Localize
|
|
2
2
|
|
|
3
|
-
This micro library does not aim to replicate a full-blown localization tool. For that, you should use something like [i18next](https://www.i18next.com/). What this library _does_ do is provide a lightweight, framework-agnostic mechanism for sharing and applying translations across one or more custom elements in a component library.
|
|
3
|
+
This zero-dependency micro library does not aim to replicate a full-blown localization tool. For that, you should use something like [i18next](https://www.i18next.com/). What this library _does_ do is provide a lightweight, framework-agnostic mechanism for sharing and applying translations across one or more custom elements in a component library.
|
|
4
4
|
|
|
5
5
|
Included are methods for translating terms, dates, currencies, and numbers and a [Reactive Controller](https://lit.dev/docs/composition/controllers/) that can be used as a mixin in Lit and other supportive component authoring libraries.
|
|
6
6
|
|
|
@@ -193,7 +193,8 @@ export class MyElement extends LitElement {
|
|
|
193
193
|
## Advantages
|
|
194
194
|
|
|
195
195
|
- Extremely lightweight
|
|
196
|
-
|
|
196
|
+
- Zero dependencies
|
|
197
|
+
- Version 2.1 measures 726 bytes (yes, _bytes_) after minify + gzip
|
|
197
198
|
- Uses existing platform features
|
|
198
199
|
- Supports simple terms, plurals, and complex translations
|
|
199
200
|
- Fun fact: some languages have [six plural forms](https://lingohub.com/blog/2019/02/pluralization) and this will support that
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ReactiveController, ReactiveControllerHost } from 'lit';
|
|
1
|
+
import type { ReactiveController, ReactiveControllerHost } from 'lit';
|
|
2
2
|
export declare type FunctionParams<T> = T extends (...args: infer U) => string ? U : never;
|
|
3
3
|
export interface Translation {
|
|
4
4
|
$code: string;
|
|
@@ -10,7 +10,8 @@ export declare function registerTranslation(...translation: Translation[]): void
|
|
|
10
10
|
export declare function term<K extends keyof Translation>(lang: string, key: K, ...args: FunctionParams<Translation[K]>): any;
|
|
11
11
|
export declare function date(lang: string, dateToFormat: Date | string, options?: Intl.DateTimeFormatOptions): string;
|
|
12
12
|
export declare function number(lang: string, numberToFormat: number | string, options?: Intl.NumberFormatOptions): string;
|
|
13
|
-
export declare function
|
|
13
|
+
export declare function relativeTime(lang: string, value: number, unit: Intl.RelativeTimeFormatUnit, options?: Intl.RelativeTimeFormatOptions): string;
|
|
14
|
+
export declare function update(): void;
|
|
14
15
|
export declare class LocalizeController implements ReactiveController {
|
|
15
16
|
host: ReactiveControllerHost & HTMLElement;
|
|
16
17
|
constructor(host: ReactiveControllerHost & HTMLElement);
|
|
@@ -19,4 +20,5 @@ export declare class LocalizeController implements ReactiveController {
|
|
|
19
20
|
term<K extends keyof Translation>(key: K, ...args: FunctionParams<Translation[K]>): any;
|
|
20
21
|
date(dateToFormat: Date | string, options?: Intl.DateTimeFormatOptions): string;
|
|
21
22
|
number(numberToFormat: number | string, options?: Intl.NumberFormatOptions): string;
|
|
23
|
+
relativeTime(value: number, unit: Intl.RelativeTimeFormatUnit, options?: Intl.RelativeTimeFormatOptions): string;
|
|
22
24
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const connectedElements = new Set();
|
|
2
|
-
const documentElementObserver = new MutationObserver(
|
|
2
|
+
const documentElementObserver = new MutationObserver(update);
|
|
3
3
|
const translations = new Map();
|
|
4
4
|
let documentLanguage = document.documentElement.lang || navigator.language;
|
|
5
5
|
let fallback;
|
|
@@ -15,7 +15,7 @@ export function registerTranslation(...translation) {
|
|
|
15
15
|
fallback = t;
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
|
-
|
|
18
|
+
update();
|
|
19
19
|
}
|
|
20
20
|
export function term(lang, key, ...args) {
|
|
21
21
|
const code = lang.toLowerCase().slice(0, 2);
|
|
@@ -49,7 +49,10 @@ export function number(lang, numberToFormat, options) {
|
|
|
49
49
|
numberToFormat = Number(numberToFormat);
|
|
50
50
|
return isNaN(numberToFormat) ? '' : new Intl.NumberFormat(lang, options).format(numberToFormat);
|
|
51
51
|
}
|
|
52
|
-
export function
|
|
52
|
+
export function relativeTime(lang, value, unit, options) {
|
|
53
|
+
return new Intl.RelativeTimeFormat(lang, options).format(value, unit);
|
|
54
|
+
}
|
|
55
|
+
export function update() {
|
|
53
56
|
documentLanguage = document.documentElement.lang || navigator.language;
|
|
54
57
|
[...connectedElements.keys()].map((el) => {
|
|
55
58
|
if (typeof el.requestUpdate === 'function') {
|
|
@@ -77,4 +80,7 @@ export class LocalizeController {
|
|
|
77
80
|
number(numberToFormat, options) {
|
|
78
81
|
return number(this.host.lang || documentLanguage, numberToFormat, options);
|
|
79
82
|
}
|
|
83
|
+
relativeTime(value, unit, options) {
|
|
84
|
+
return relativeTime(this.host.lang || documentLanguage, value, unit, options);
|
|
85
|
+
}
|
|
80
86
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shoelace-style/localize",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.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",
|
|
@@ -34,9 +34,7 @@
|
|
|
34
34
|
"homepage": "https://github.com/shoelace-style/localize#readme",
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"del": "^6.0.0",
|
|
37
|
+
"lit": "^2.0.2",
|
|
37
38
|
"typescript": "^4.4.3"
|
|
38
|
-
},
|
|
39
|
-
"dependencies": {
|
|
40
|
-
"lit": "^2.0.2"
|
|
41
39
|
}
|
|
42
40
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LitElement, ReactiveController, ReactiveControllerHost } from 'lit';
|
|
1
|
+
import type { LitElement, ReactiveController, ReactiveControllerHost } from 'lit';
|
|
2
2
|
|
|
3
3
|
export type FunctionParams<T> = T extends (...args: infer U) => string ? U : never;
|
|
4
4
|
|
|
@@ -10,7 +10,7 @@ export interface Translation {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
const connectedElements = new Set<HTMLElement>();
|
|
13
|
-
const documentElementObserver = new MutationObserver(
|
|
13
|
+
const documentElementObserver = new MutationObserver(update);
|
|
14
14
|
const translations: Map<string, Translation> = new Map();
|
|
15
15
|
let documentLanguage = document.documentElement.lang || navigator.language;
|
|
16
16
|
let fallback: Translation;
|
|
@@ -35,7 +35,7 @@ export function registerTranslation(...translation: Translation[]) {
|
|
|
35
35
|
}
|
|
36
36
|
});
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
update();
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
//
|
|
@@ -85,9 +85,21 @@ export function number(lang: string, numberToFormat: number | string, options?:
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
//
|
|
88
|
-
//
|
|
88
|
+
// Formats a relative date using the specified locale.
|
|
89
89
|
//
|
|
90
|
-
export function
|
|
90
|
+
export function relativeTime(
|
|
91
|
+
lang: string,
|
|
92
|
+
value: number,
|
|
93
|
+
unit: Intl.RelativeTimeFormatUnit,
|
|
94
|
+
options?: Intl.RelativeTimeFormatOptions
|
|
95
|
+
) {
|
|
96
|
+
return new Intl.RelativeTimeFormat(lang, options).format(value, unit);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
//
|
|
100
|
+
// Updates all localized elements that are currently connected
|
|
101
|
+
//
|
|
102
|
+
export function update() {
|
|
91
103
|
documentLanguage = document.documentElement.lang || navigator.language;
|
|
92
104
|
|
|
93
105
|
[...connectedElements.keys()].map((el: LitElement) => {
|
|
@@ -142,4 +154,8 @@ export class LocalizeController implements ReactiveController {
|
|
|
142
154
|
number(numberToFormat: number | string, options?: Intl.NumberFormatOptions) {
|
|
143
155
|
return number(this.host.lang || documentLanguage, numberToFormat, options);
|
|
144
156
|
}
|
|
157
|
+
|
|
158
|
+
relativeTime(value: number, unit: Intl.RelativeTimeFormatUnit, options?: Intl.RelativeTimeFormatOptions) {
|
|
159
|
+
return relativeTime(this.host.lang || documentLanguage, value, unit, options);
|
|
160
|
+
}
|
|
145
161
|
}
|