@shoelace-style/localize 3.0.2 → 3.0.4
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 +8 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/package.json +1 -1
- package/src/index.ts +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 3.0.4
|
|
4
|
+
|
|
5
|
+
- Ensure return values of translation functions are always a string
|
|
6
|
+
|
|
7
|
+
## 3.0.3
|
|
8
|
+
|
|
9
|
+
- Fixed a bug where regional locales stopped working
|
|
10
|
+
|
|
3
11
|
## 3.0.2
|
|
4
12
|
|
|
5
13
|
- Fixed a parsing bug in extended language codes [#16](https://github.com/shoelace-style/localize/issues/16)
|
package/dist/index.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export declare class LocalizeController<UserTranslation extends Translation = De
|
|
|
17
17
|
hostDisconnected(): void;
|
|
18
18
|
dir(): string;
|
|
19
19
|
lang(): string;
|
|
20
|
-
term<K extends keyof UserTranslation>(key: K, ...args: FunctionParams<UserTranslation[K]>):
|
|
20
|
+
term<K extends keyof UserTranslation>(key: K, ...args: FunctionParams<UserTranslation[K]>): string;
|
|
21
21
|
date(dateToFormat: Date | string, options?: Intl.DateTimeFormatOptions): string;
|
|
22
22
|
number(numberToFormat: number | string, options?: Intl.NumberFormatOptions): string;
|
|
23
23
|
relativeTime(value: number, unit: Intl.RelativeTimeFormatUnit, options?: Intl.RelativeTimeFormatOptions): string;
|
package/dist/index.js
CHANGED
|
@@ -54,7 +54,7 @@ export class LocalizeController {
|
|
|
54
54
|
const locale = new Intl.Locale(this.lang());
|
|
55
55
|
const language = locale === null || locale === void 0 ? void 0 : locale.language.toLowerCase();
|
|
56
56
|
const region = (_b = (_a = locale === null || locale === void 0 ? void 0 : locale.region) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : '';
|
|
57
|
-
const primary = translations.get(region);
|
|
57
|
+
const primary = translations.get(`${language}-${region}`);
|
|
58
58
|
const secondary = translations.get(language);
|
|
59
59
|
let term;
|
|
60
60
|
if (primary && primary[key]) {
|
|
@@ -68,7 +68,7 @@ export class LocalizeController {
|
|
|
68
68
|
}
|
|
69
69
|
else {
|
|
70
70
|
console.error(`No translation found for: ${String(key)}`);
|
|
71
|
-
return key;
|
|
71
|
+
return String(key);
|
|
72
72
|
}
|
|
73
73
|
if (typeof term === 'function') {
|
|
74
74
|
return term(...args);
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -115,11 +115,11 @@ export class LocalizeController<UserTranslation extends Translation = DefaultTra
|
|
|
115
115
|
return `${this.host.lang || documentLanguage}`.toLowerCase();
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
term<K extends keyof UserTranslation>(key: K, ...args: FunctionParams<UserTranslation[K]>) {
|
|
118
|
+
term<K extends keyof UserTranslation>(key: K, ...args: FunctionParams<UserTranslation[K]>): string {
|
|
119
119
|
const locale = new Intl.Locale(this.lang());
|
|
120
120
|
const language = locale?.language.toLowerCase();
|
|
121
121
|
const region = locale?.region?.toLowerCase() ?? '';
|
|
122
|
-
const primary = <UserTranslation>translations.get(region);
|
|
122
|
+
const primary = <UserTranslation>translations.get(`${language}-${region}`);
|
|
123
123
|
const secondary = <UserTranslation>translations.get(language);
|
|
124
124
|
let term: any;
|
|
125
125
|
|
|
@@ -132,7 +132,7 @@ export class LocalizeController<UserTranslation extends Translation = DefaultTra
|
|
|
132
132
|
term = fallback[key as keyof Translation];
|
|
133
133
|
} else {
|
|
134
134
|
console.error(`No translation found for: ${String(key)}`);
|
|
135
|
-
return key;
|
|
135
|
+
return String(key);
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
if (typeof term === 'function') {
|
|
@@ -143,19 +143,19 @@ export class LocalizeController<UserTranslation extends Translation = DefaultTra
|
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
/** Outputs a localized date in the specified format. */
|
|
146
|
-
date(dateToFormat: Date | string, options?: Intl.DateTimeFormatOptions) {
|
|
146
|
+
date(dateToFormat: Date | string, options?: Intl.DateTimeFormatOptions): string {
|
|
147
147
|
dateToFormat = new Date(dateToFormat);
|
|
148
148
|
return new Intl.DateTimeFormat(this.lang(), options).format(dateToFormat);
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
/** Outputs a localized number in the specified format. */
|
|
152
|
-
number(numberToFormat: number | string, options?: Intl.NumberFormatOptions) {
|
|
152
|
+
number(numberToFormat: number | string, options?: Intl.NumberFormatOptions): string {
|
|
153
153
|
numberToFormat = Number(numberToFormat);
|
|
154
154
|
return isNaN(numberToFormat) ? '' : new Intl.NumberFormat(this.lang(), options).format(numberToFormat);
|
|
155
155
|
}
|
|
156
156
|
|
|
157
157
|
/** Outputs a localized time in relative format. */
|
|
158
|
-
relativeTime(value: number, unit: Intl.RelativeTimeFormatUnit, options?: Intl.RelativeTimeFormatOptions) {
|
|
158
|
+
relativeTime(value: number, unit: Intl.RelativeTimeFormatUnit, options?: Intl.RelativeTimeFormatOptions): string {
|
|
159
159
|
return new Intl.RelativeTimeFormat(this.lang(), options).format(value, unit);
|
|
160
160
|
}
|
|
161
161
|
}
|