@shoelace-style/localize 2.0.0 → 2.1.0
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 +9 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +6 -0
- package/package.json +1 -1
- package/src/index.ts +16 -0
package/CHANGELOG.md
ADDED
package/dist/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ 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 relativeTime(lang: string, value: number, unit: Intl.RelativeTimeFormatUnit, options?: Intl.RelativeTimeFormatOptions): string;
|
|
13
14
|
export declare function updateLocalizedTerms(): void;
|
|
14
15
|
export declare class LocalizeController implements ReactiveController {
|
|
15
16
|
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
|
@@ -49,6 +49,9 @@ 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 relativeTime(lang, value, unit, options) {
|
|
53
|
+
return new Intl.RelativeTimeFormat(lang, options).format(value, unit);
|
|
54
|
+
}
|
|
52
55
|
export function updateLocalizedTerms() {
|
|
53
56
|
documentLanguage = document.documentElement.lang || navigator.language;
|
|
54
57
|
[...connectedElements.keys()].map((el) => {
|
|
@@ -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
package/src/index.ts
CHANGED
|
@@ -84,6 +84,18 @@ export function number(lang: string, numberToFormat: number | string, options?:
|
|
|
84
84
|
return isNaN(numberToFormat) ? '' : new Intl.NumberFormat(lang, options).format(numberToFormat);
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
+
//
|
|
88
|
+
// Formats a relative date using the specified locale.
|
|
89
|
+
//
|
|
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
|
+
|
|
87
99
|
//
|
|
88
100
|
// Updates the locale for all localized elements that are currently connected
|
|
89
101
|
//
|
|
@@ -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
|
}
|