@stamhoofd/backend-i18n 2.82.0 → 2.83.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/dist/I18n.d.ts +8 -6
- package/dist/I18n.d.ts.map +1 -1
- package/dist/I18n.js +5 -5
- package/dist/I18n.js.map +1 -1
- package/package.json +2 -2
- package/src/I18n.ts +14 -14
package/dist/I18n.d.ts
CHANGED
|
@@ -1,27 +1,29 @@
|
|
|
1
1
|
import { DecodedRequest, Request } from '@simonbackx/simple-endpoints';
|
|
2
|
-
import { Country } from '@stamhoofd/structures';
|
|
2
|
+
import { Country, Language } from '@stamhoofd/structures';
|
|
3
3
|
export declare class I18n {
|
|
4
4
|
static loadedLocales: Map<string, Map<string, string>>;
|
|
5
|
-
static defaultLanguage:
|
|
5
|
+
static defaultLanguage: Language;
|
|
6
6
|
static defaultCountry: Country;
|
|
7
7
|
static load(): Promise<void>;
|
|
8
8
|
static loadRecursive(messages: any, prefix?: string | null): Map<string, string>;
|
|
9
9
|
static isValidLocale(locale: string): boolean;
|
|
10
|
-
language:
|
|
10
|
+
language: Language;
|
|
11
11
|
country: Country;
|
|
12
12
|
messages: Map<string, string>;
|
|
13
13
|
get locale(): string;
|
|
14
|
-
constructor(language:
|
|
14
|
+
constructor(language: Language, country: Country);
|
|
15
15
|
correctLanguageCountryCombination(): void;
|
|
16
16
|
switchToLocale(options: {
|
|
17
|
-
language?:
|
|
17
|
+
language?: Language;
|
|
18
18
|
country?: Country;
|
|
19
19
|
}): void;
|
|
20
20
|
static fromRequest(request: Request | DecodedRequest<any, any, any>): I18n;
|
|
21
21
|
t(key: string, replace?: Record<string, string>): string;
|
|
22
22
|
$t(key: string, replace?: Record<string, string>): string;
|
|
23
23
|
escapeRegex(string: any): any;
|
|
24
|
-
replace(text: string, replace?: Record<string, string
|
|
24
|
+
replace(text: string, replace?: Record<string, string | {
|
|
25
|
+
toString(): string;
|
|
26
|
+
}>): string;
|
|
25
27
|
getDomain(localizedDomain: LocalizedDomain): string;
|
|
26
28
|
localizedDomains: {
|
|
27
29
|
adminUrl: string;
|
package/dist/I18n.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"I18n.d.ts","sourceRoot":"","sources":["../src/I18n.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AAGvE,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"I18n.d.ts","sourceRoot":"","sources":["../src/I18n.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AAGvE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAI1D,qBAAa,IAAI;IACb,MAAM,CAAC,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAa;IACnE,MAAM,CAAC,eAAe,WAAkB;IACxC,MAAM,CAAC,cAAc,UAAmB;WAE3B,IAAI;IA4BjB,MAAM,CAAC,aAAa,CAAC,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAE,MAAM,GAAG,IAAW,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;IAetF,MAAM,CAAC,aAAa,CAAC,MAAM,EAAE,MAAM;IAUnC,QAAQ,WAAkB;IAC1B,OAAO,UAAmB;IAG1B,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE9B,IAAI,MAAM,WAET;gBAEW,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO;IAiBhD,iCAAiC;IA8BjC,cAAc,CAAC,OAAO,EAAE;QACpB,QAAQ,CAAC,EAAE,QAAQ,CAAC;QACpB,OAAO,CAAC,EAAE,OAAO,CAAC;KACrB;IAiBD,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,cAAc,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,IAAI;IAsD1E,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAI/C,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAIhD,WAAW,CAAC,MAAM,KAAA;IAIlB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG;QAAE,QAAQ,IAAI,MAAM,CAAA;KAAE,CAAC;IAa/E,SAAS,CAAC,eAAe,EAAE,eAAe,GAAG,MAAM;IAInD,gBAAgB;;;yBAGG,MAAM;yCACU,MAAM;qCACV,MAAM;MACnC;CACL"}
|
package/dist/I18n.js
CHANGED
|
@@ -9,7 +9,7 @@ const fs_1 = require("fs");
|
|
|
9
9
|
const path_1 = tslib_1.__importDefault(require("path"));
|
|
10
10
|
class I18n {
|
|
11
11
|
static loadedLocales = new Map();
|
|
12
|
-
static defaultLanguage =
|
|
12
|
+
static defaultLanguage = structures_1.Language.Dutch;
|
|
13
13
|
static defaultCountry = structures_1.Country.Belgium;
|
|
14
14
|
static async load() {
|
|
15
15
|
await simple_logging_1.logger.setContext({
|
|
@@ -55,7 +55,7 @@ class I18n {
|
|
|
55
55
|
}
|
|
56
56
|
return false;
|
|
57
57
|
}
|
|
58
|
-
language =
|
|
58
|
+
language = structures_1.Language.Dutch;
|
|
59
59
|
country = structures_1.Country.Belgium;
|
|
60
60
|
// Reference to messages in loadedLocales
|
|
61
61
|
messages;
|
|
@@ -93,7 +93,7 @@ class I18n {
|
|
|
93
93
|
this.country = I18n.defaultCountry;
|
|
94
94
|
while (!I18n.isValidLocale(this.locale)) {
|
|
95
95
|
const index = locales_1.countries.indexOf(this.country);
|
|
96
|
-
if (index
|
|
96
|
+
if (index === locales_1.countries.length - 1) {
|
|
97
97
|
// Last country
|
|
98
98
|
this.language = I18n.defaultLanguage;
|
|
99
99
|
this.country = I18n.defaultCountry;
|
|
@@ -138,7 +138,7 @@ class I18n {
|
|
|
138
138
|
const trimmed = item.trim();
|
|
139
139
|
const localeSplit = trimmed.split(';');
|
|
140
140
|
const locale = localeSplit[0];
|
|
141
|
-
if (locale.length
|
|
141
|
+
if (locale.length === 2) {
|
|
142
142
|
// Language
|
|
143
143
|
if (locales_1.languages.includes(locale)) {
|
|
144
144
|
// Use a default country
|
|
@@ -179,7 +179,7 @@ class I18n {
|
|
|
179
179
|
for (const key in replace) {
|
|
180
180
|
if (replace.hasOwnProperty(key)) {
|
|
181
181
|
const value = replace[key];
|
|
182
|
-
text = text.replace(new RegExp('{' + this.escapeRegex(key) + '}', 'g'), value);
|
|
182
|
+
text = text.replace(new RegExp('{' + this.escapeRegex(key) + '}', 'g'), value.toString());
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
return text;
|
package/dist/I18n.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"I18n.js","sourceRoot":"","sources":["../src/I18n.ts"],"names":[],"mappings":";;;;AACA,+DAAgE;AAChE,gDAA0D;AAC1D,
|
|
1
|
+
{"version":3,"file":"I18n.js","sourceRoot":"","sources":["../src/I18n.ts"],"names":[],"mappings":";;;;AACA,+DAAgE;AAChE,gDAA0D;AAC1D,sDAA0D;AAC1D,2BAAoC;AACpC,wDAAwB;AAExB,MAAa,IAAI;IACb,MAAM,CAAC,aAAa,GAAqC,IAAI,GAAG,EAAE,CAAC;IACnE,MAAM,CAAC,eAAe,GAAG,qBAAQ,CAAC,KAAK,CAAC;IACxC,MAAM,CAAC,cAAc,GAAG,oBAAO,CAAC,OAAO,CAAC;IAExC,MAAM,CAAC,KAAK,CAAC,IAAI;QACb,MAAM,uBAAM,CAAC,UAAU,CAAC;YACpB,QAAQ,EAAE;gB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
|
2
2
|
"name": "@stamhoofd/backend-i18n",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.83.0",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"license": "UNLICENCED",
|
|
@@ -18,5 +18,5 @@
|
|
|
18
18
|
"publishConfig": {
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
|
-
"gitHead": "
|
|
21
|
+
"gitHead": "5f116184aad8965d9deda1902e73af7feefa5721"
|
|
22
22
|
}
|
package/src/I18n.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { DecodedRequest, Request } from '@simonbackx/simple-endpoints';
|
|
2
2
|
import { logger, StyledText } from '@simonbackx/simple-logging';
|
|
3
3
|
import { countries, languages } from '@stamhoofd/locales';
|
|
4
|
-
import { Country } from '@stamhoofd/structures';
|
|
4
|
+
import { Country, Language } from '@stamhoofd/structures';
|
|
5
5
|
import { promises as fs } from 'fs';
|
|
6
6
|
import path from 'path';
|
|
7
7
|
|
|
8
8
|
export class I18n {
|
|
9
9
|
static loadedLocales: Map<string, Map<string, string>> = new Map();
|
|
10
|
-
static defaultLanguage =
|
|
10
|
+
static defaultLanguage = Language.Dutch;
|
|
11
11
|
static defaultCountry = Country.Belgium;
|
|
12
12
|
|
|
13
13
|
static async load() {
|
|
@@ -55,7 +55,7 @@ export class I18n {
|
|
|
55
55
|
|
|
56
56
|
static isValidLocale(locale: string) {
|
|
57
57
|
if (locale.length == 5 && locale.substr(2, 1) == '-') {
|
|
58
|
-
const l = locale.substr(0, 2).toLowerCase();
|
|
58
|
+
const l = locale.substr(0, 2).toLowerCase() as Language;
|
|
59
59
|
const c = locale.substr(3, 2).toUpperCase();
|
|
60
60
|
|
|
61
61
|
return languages.includes(l) && (countries as string[]).includes(c);
|
|
@@ -63,7 +63,7 @@ export class I18n {
|
|
|
63
63
|
return false;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
language =
|
|
66
|
+
language = Language.Dutch;
|
|
67
67
|
country = Country.Belgium;
|
|
68
68
|
|
|
69
69
|
// Reference to messages in loadedLocales
|
|
@@ -73,7 +73,7 @@ export class I18n {
|
|
|
73
73
|
return this.language + '-' + this.country;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
constructor(language:
|
|
76
|
+
constructor(language: Language, country: Country) {
|
|
77
77
|
this.language = language;
|
|
78
78
|
this.country = country;
|
|
79
79
|
this.correctLanguageCountryCombination();
|
|
@@ -110,7 +110,7 @@ export class I18n {
|
|
|
110
110
|
this.country = I18n.defaultCountry;
|
|
111
111
|
while (!I18n.isValidLocale(this.locale)) {
|
|
112
112
|
const index = countries.indexOf(this.country);
|
|
113
|
-
if (index
|
|
113
|
+
if (index === countries.length - 1) {
|
|
114
114
|
// Last country
|
|
115
115
|
this.language = I18n.defaultLanguage;
|
|
116
116
|
this.country = I18n.defaultCountry;
|
|
@@ -121,7 +121,7 @@ export class I18n {
|
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
switchToLocale(options: {
|
|
124
|
-
language?:
|
|
124
|
+
language?: Language;
|
|
125
125
|
country?: Country;
|
|
126
126
|
}) {
|
|
127
127
|
this.country = options.country ?? this.country;
|
|
@@ -148,7 +148,7 @@ export class I18n {
|
|
|
148
148
|
// Try using custom property
|
|
149
149
|
const localeHeader = request.headers['x-locale'];
|
|
150
150
|
if (localeHeader && typeof localeHeader === 'string' && this.isValidLocale(localeHeader)) {
|
|
151
|
-
const l = localeHeader.substr(0, 2).toLowerCase();
|
|
151
|
+
const l = localeHeader.substr(0, 2).toLowerCase() as Language;
|
|
152
152
|
const c = localeHeader.substr(3, 2).toUpperCase() as Country;
|
|
153
153
|
|
|
154
154
|
const i18n = new I18n(l, c);
|
|
@@ -167,19 +167,19 @@ export class I18n {
|
|
|
167
167
|
const localeSplit = trimmed.split(';');
|
|
168
168
|
const locale = localeSplit[0];
|
|
169
169
|
|
|
170
|
-
if (locale.length
|
|
170
|
+
if (locale.length === 2) {
|
|
171
171
|
// Language
|
|
172
|
-
if (languages.includes(locale)) {
|
|
172
|
+
if (languages.includes(locale as Language)) {
|
|
173
173
|
// Use a default country
|
|
174
174
|
// Country can get overriden when matching a organization
|
|
175
175
|
// using .setCountry(country) method
|
|
176
|
-
const i18n = new I18n(locale, Country.Belgium);
|
|
176
|
+
const i18n = new I18n(locale as Language, Country.Belgium);
|
|
177
177
|
(request as any)._cached_i18n = i18n;
|
|
178
178
|
return i18n;
|
|
179
179
|
}
|
|
180
180
|
}
|
|
181
181
|
else if (locale.length === 5 && this.isValidLocale(locale)) {
|
|
182
|
-
const l = locale.substr(0, 2).toLowerCase();
|
|
182
|
+
const l = locale.substr(0, 2).toLowerCase() as Language;
|
|
183
183
|
const c = locale.substr(3, 2).toUpperCase() as Country;
|
|
184
184
|
|
|
185
185
|
// Lang + country
|
|
@@ -206,14 +206,14 @@ export class I18n {
|
|
|
206
206
|
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
-
replace(text: string, replace?: Record<string, string>) {
|
|
209
|
+
replace(text: string, replace?: Record<string, string | { toString(): string }>) {
|
|
210
210
|
if (!replace) {
|
|
211
211
|
return text;
|
|
212
212
|
}
|
|
213
213
|
for (const key in replace) {
|
|
214
214
|
if (replace.hasOwnProperty(key)) {
|
|
215
215
|
const value = replace[key];
|
|
216
|
-
text = text.replace(new RegExp('{' + this.escapeRegex(key) + '}', 'g'), value);
|
|
216
|
+
text = text.replace(new RegExp('{' + this.escapeRegex(key) + '}', 'g'), value.toString());
|
|
217
217
|
}
|
|
218
218
|
}
|
|
219
219
|
return text;
|