@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 ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ## 2.1.0
4
+
5
+ - Added relative time method to
6
+
7
+ ## 2.0.0
8
+
9
+ - Reworked the library to use the [ReactiveController](https://lit.dev/docs/composition/controllers/) interface
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shoelace-style/localize",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
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
@@ -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
  }