@shoelace-style/localize 1.1.2 → 1.1.6
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/README.md +5 -3
- package/dist/fast.d.ts +4 -4
- package/dist/fast.js +6 -12
- package/package.json +1 -1
- package/src/fast.ts +11 -11
package/README.md
CHANGED
|
@@ -206,13 +206,13 @@ import { localize, translate as t, formatDate as d, formatNumber as n } from '@s
|
|
|
206
206
|
|
|
207
207
|
const template = html<MyElement>`
|
|
208
208
|
<!-- Term -->
|
|
209
|
-
${t('hello')}
|
|
209
|
+
${x => t(x, 'hello')}
|
|
210
210
|
|
|
211
211
|
<!-- Date -->
|
|
212
|
-
${d('2021-09-15 14:00:00 ET'), { month: 'long', day: 'numeric', year: 'numeric' }}
|
|
212
|
+
${x => d(x, '2021-09-15 14:00:00 ET'), { month: 'long', day: 'numeric', year: 'numeric' }}
|
|
213
213
|
|
|
214
214
|
<!-- Currency -->
|
|
215
|
-
${n(1000, { style: 'currency', currency: 'USD'})}
|
|
215
|
+
${x => n(x, 1000, { style: 'currency', currency: 'USD'})}
|
|
216
216
|
`;
|
|
217
217
|
|
|
218
218
|
@customElement({
|
|
@@ -224,6 +224,8 @@ export class MyElement extends FASTElement {
|
|
|
224
224
|
}
|
|
225
225
|
```
|
|
226
226
|
|
|
227
|
+
**Note:** This directive requires a context to be passed as the first argument. This is required because of a limitation in FAST's rendering engine that causes inconsistencies when a directive is used as `${t('example')}` vs. `${x => t('example')}`.
|
|
228
|
+
|
|
227
229
|
### No Library (Advanced)
|
|
228
230
|
|
|
229
231
|
To use this without a custom element library, you'll need to follow this pattern.
|
package/dist/fast.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { CaptureType } from '@microsoft/fast-element';
|
|
1
|
+
import type { CaptureType, ExecutionContext, FASTElement } from '@microsoft/fast-element';
|
|
2
2
|
import type { FunctionParams, Translation } from './';
|
|
3
3
|
export declare function localize(): (targetClass: any) => any;
|
|
4
|
-
export declare function translate<K extends keyof Translation
|
|
5
|
-
export declare function formatDate<TSource = any>(date: Date | string, options?: Intl.DateTimeFormatOptions): CaptureType<TSource>;
|
|
6
|
-
export declare function formatNumber<TSource = any>(number: number | string, options?: Intl.DateTimeFormatOptions): CaptureType<TSource>;
|
|
4
|
+
export declare function translate<TSource, K extends keyof Translation>(context: ExecutionContext | FASTElement | HTMLElement, key: K, ...args: FunctionParams<Translation[K]>): CaptureType<TSource>;
|
|
5
|
+
export declare function formatDate<TSource = any>(context: ExecutionContext | FASTElement | HTMLElement, date: Date | string, options?: Intl.DateTimeFormatOptions): CaptureType<TSource>;
|
|
6
|
+
export declare function formatNumber<TSource = any>(context: ExecutionContext | FASTElement | HTMLElement, number: number | string, options?: Intl.DateTimeFormatOptions): CaptureType<TSource>;
|
package/dist/fast.js
CHANGED
|
@@ -40,18 +40,12 @@ function getLang(source) {
|
|
|
40
40
|
}
|
|
41
41
|
return lang;
|
|
42
42
|
}
|
|
43
|
-
export function translate(key, ...args) {
|
|
44
|
-
return (
|
|
45
|
-
return t(getLang(source), key, ...args);
|
|
46
|
-
};
|
|
43
|
+
export function translate(context, key, ...args) {
|
|
44
|
+
return t(getLang(context), key, ...args);
|
|
47
45
|
}
|
|
48
|
-
export function formatDate(date, options) {
|
|
49
|
-
return (
|
|
50
|
-
return d(getLang(source), date, options);
|
|
51
|
-
};
|
|
46
|
+
export function formatDate(context, date, options) {
|
|
47
|
+
return d(getLang(context), date, options);
|
|
52
48
|
}
|
|
53
|
-
export function formatNumber(number, options) {
|
|
54
|
-
return (
|
|
55
|
-
return n(getLang(source), number, options);
|
|
56
|
-
};
|
|
49
|
+
export function formatNumber(context, number, options) {
|
|
50
|
+
return n(getLang(context), number, options);
|
|
57
51
|
}
|
package/package.json
CHANGED
package/src/fast.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { connectedElements, detectLanguage, translate as t, formatDate as d, formatNumber as n } from './';
|
|
2
|
-
import type { CaptureType } from '@microsoft/fast-element';
|
|
2
|
+
import type { CaptureType, ExecutionContext, FASTElement } from '@microsoft/fast-element';
|
|
3
3
|
import type { FunctionParams, Translation } from './';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -72,10 +72,12 @@ function getLang(source: any) {
|
|
|
72
72
|
*
|
|
73
73
|
* Formats a number using the element's current language.
|
|
74
74
|
*/
|
|
75
|
-
export function translate<K extends keyof Translation
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
export function translate<TSource, K extends keyof Translation>(
|
|
76
|
+
context: ExecutionContext | FASTElement | HTMLElement,
|
|
77
|
+
key: K,
|
|
78
|
+
...args: FunctionParams<Translation[K]>
|
|
79
|
+
): CaptureType<TSource> {
|
|
80
|
+
return t(getLang(context), key, ...args);
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
/**
|
|
@@ -84,12 +86,11 @@ export function translate<K extends keyof Translation, TSource = any>(key: K, ..
|
|
|
84
86
|
* Formats a date using the element's current language.
|
|
85
87
|
*/
|
|
86
88
|
export function formatDate<TSource = any>(
|
|
89
|
+
context: ExecutionContext | FASTElement | HTMLElement,
|
|
87
90
|
date: Date | string,
|
|
88
91
|
options?: Intl.DateTimeFormatOptions
|
|
89
92
|
): CaptureType<TSource> {
|
|
90
|
-
return (
|
|
91
|
-
return d(getLang(source), date, options);
|
|
92
|
-
};
|
|
93
|
+
return d(getLang(context), date, options);
|
|
93
94
|
}
|
|
94
95
|
|
|
95
96
|
/**
|
|
@@ -98,10 +99,9 @@ export function formatDate<TSource = any>(
|
|
|
98
99
|
* Formats a number using the element's current language.
|
|
99
100
|
*/
|
|
100
101
|
export function formatNumber<TSource = any>(
|
|
102
|
+
context: ExecutionContext | FASTElement | HTMLElement,
|
|
101
103
|
number: number | string,
|
|
102
104
|
options?: Intl.DateTimeFormatOptions
|
|
103
105
|
): CaptureType<TSource> {
|
|
104
|
-
return (
|
|
105
|
-
return n(getLang(source), number, options);
|
|
106
|
-
};
|
|
106
|
+
return n(getLang(context), number, options);
|
|
107
107
|
}
|