@stamhoofd/backend-i18n 2.119.0 → 2.120.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 +3 -2
- package/dist/I18n.d.ts.map +1 -1
- package/dist/I18n.js +32 -34
- package/dist/I18n.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -10
- package/dist/index.js.map +1 -1
- package/package.json +20 -7
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/src/I18n.ts +0 -243
- package/src/index.ts +0 -23
package/dist/I18n.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { DecodedRequest, Request } from '@simonbackx/simple-endpoints';
|
|
2
|
-
import { Country
|
|
1
|
+
import type { DecodedRequest, Request } from '@simonbackx/simple-endpoints';
|
|
2
|
+
import { Country } from '@stamhoofd/types/Country';
|
|
3
|
+
import { Language } from '@stamhoofd/types/Language';
|
|
3
4
|
export declare class I18n {
|
|
4
5
|
static loadedLocales: Map<string, Map<string, string>>;
|
|
5
6
|
static defaultLanguage: Language;
|
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;
|
|
1
|
+
{"version":3,"file":"I18n.d.ts","sourceRoot":"","sources":["../src/I18n.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AAM5E,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAMrD,qBAAa,IAAI;IACb,MAAM,CAAC,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAA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;IAInD,gBAAgB;;;yBAGG,MAAM;yCACU,MAAM;qCACV,MAAM;MACnC;CACL"}
|
package/dist/I18n.js
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { logger, StyledText } from '@simonbackx/simple-logging';
|
|
2
|
+
import { countries, languages } from '@stamhoofd/locales';
|
|
3
|
+
import { appToUri } from '@stamhoofd/structures';
|
|
4
|
+
import { promises as fs } from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import { Country } from '@stamhoofd/types/Country';
|
|
7
|
+
import { Language } from '@stamhoofd/types/Language';
|
|
8
|
+
import { createRequire } from 'node:module';
|
|
9
|
+
// Polyfill require.resolve, since import.meta.resolve is not supported by vitest
|
|
10
|
+
const require = createRequire(import.meta.url);
|
|
11
|
+
export class I18n {
|
|
11
12
|
static loadedLocales = new Map();
|
|
12
|
-
static defaultLanguage =
|
|
13
|
-
static defaultCountry =
|
|
13
|
+
static defaultLanguage = Language.Dutch;
|
|
14
|
+
static defaultCountry = Country.Belgium;
|
|
14
15
|
static async load() {
|
|
15
|
-
await
|
|
16
|
+
await logger.setContext({
|
|
16
17
|
prefixes: [
|
|
17
|
-
new
|
|
18
|
+
new StyledText('[I18n] ').addClass('i18n', 'tag'),
|
|
18
19
|
],
|
|
19
20
|
tags: ['i18n'],
|
|
20
21
|
}, async () => {
|
|
@@ -22,12 +23,12 @@ class I18n {
|
|
|
22
23
|
if (!STAMHOOFD.translationNamespace) {
|
|
23
24
|
throw new Error('STAMHOOFD.translationNamespace is not set');
|
|
24
25
|
}
|
|
25
|
-
const directory =
|
|
26
|
-
const files = (await
|
|
26
|
+
const directory = path.dirname(require.resolve('@stamhoofd/locales/locales/' + STAMHOOFD.translationNamespace + '/nl-BE.json'));
|
|
27
|
+
const files = (await fs.readdir(directory, { withFileTypes: true }))
|
|
27
28
|
.filter(dirent => !dirent.isDirectory());
|
|
28
29
|
for (const file of files) {
|
|
29
30
|
const locale = file.name.substr(0, file.name.length - 5);
|
|
30
|
-
const messages = await
|
|
31
|
+
const messages = await import(directory + '/' + file.name, { with: { type: 'json' } });
|
|
31
32
|
this.loadedLocales.set(locale, this.loadRecursive(messages.default));
|
|
32
33
|
}
|
|
33
34
|
console.log('Loaded all locales');
|
|
@@ -50,21 +51,21 @@ class I18n {
|
|
|
50
51
|
static get validLocales() {
|
|
51
52
|
// todo: make platform specific
|
|
52
53
|
return (STAMHOOFD.locales ?? {
|
|
53
|
-
[
|
|
54
|
+
[Country.Belgium]: [Language.Dutch],
|
|
54
55
|
});
|
|
55
56
|
}
|
|
56
57
|
static isValidLocale(locale) {
|
|
57
58
|
if (locale.length == 5 && locale.substr(2, 1) == '-') {
|
|
58
59
|
const l = locale.substr(0, 2).toLowerCase();
|
|
59
60
|
const c = locale.substr(3, 2).toUpperCase();
|
|
60
|
-
if (
|
|
61
|
+
if (languages.includes(l) && countries.includes(c)) {
|
|
61
62
|
return this.validLocales[c]?.includes(l) ?? false;
|
|
62
63
|
}
|
|
63
64
|
}
|
|
64
65
|
return false;
|
|
65
66
|
}
|
|
66
|
-
language =
|
|
67
|
-
country =
|
|
67
|
+
language = Language.Dutch;
|
|
68
|
+
country = Country.Belgium;
|
|
68
69
|
// Reference to messages in loadedLocales
|
|
69
70
|
messages;
|
|
70
71
|
get locale() {
|
|
@@ -91,20 +92,20 @@ class I18n {
|
|
|
91
92
|
const validLocales = I18n.validLocales;
|
|
92
93
|
if (!(this.country in validLocales)) {
|
|
93
94
|
// Find first coutnry with same language
|
|
94
|
-
for (const country of
|
|
95
|
+
for (const country of countries) {
|
|
95
96
|
if (validLocales[country]?.includes(this.language)) {
|
|
96
97
|
this.country = country;
|
|
97
98
|
return;
|
|
98
99
|
}
|
|
99
100
|
}
|
|
100
101
|
// Fallback
|
|
101
|
-
this.country =
|
|
102
|
+
this.country = countries[0];
|
|
102
103
|
this.language = validLocales[this.country][0];
|
|
103
104
|
return;
|
|
104
105
|
}
|
|
105
106
|
if (!validLocales[this.country]?.includes(this.language)) {
|
|
106
|
-
if (validLocales[this.country]?.includes(
|
|
107
|
-
this.language =
|
|
107
|
+
if (validLocales[this.country]?.includes(Language.English)) {
|
|
108
|
+
this.language = Language.English;
|
|
108
109
|
return;
|
|
109
110
|
}
|
|
110
111
|
this.language = validLocales[this.country][0];
|
|
@@ -148,11 +149,11 @@ class I18n {
|
|
|
148
149
|
const locale = localeSplit[0];
|
|
149
150
|
if (locale.length === 2) {
|
|
150
151
|
// Language
|
|
151
|
-
if (
|
|
152
|
+
if (languages.includes(locale)) {
|
|
152
153
|
// Use a default country
|
|
153
154
|
// Country can get overriden when matching a organization
|
|
154
155
|
// using .setCountry(country) method
|
|
155
|
-
const i18n = new I18n(locale,
|
|
156
|
+
const i18n = new I18n(locale, Country.Belgium);
|
|
156
157
|
request._cached_i18n = i18n;
|
|
157
158
|
return i18n;
|
|
158
159
|
}
|
|
@@ -178,17 +179,15 @@ class I18n {
|
|
|
178
179
|
return this.replace(this.messages.get(key) ?? key, replace);
|
|
179
180
|
}
|
|
180
181
|
escapeRegex(string) {
|
|
181
|
-
return string.replace(/[
|
|
182
|
+
return string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
182
183
|
}
|
|
183
184
|
replace(text, replace) {
|
|
184
185
|
if (!replace) {
|
|
185
186
|
return text;
|
|
186
187
|
}
|
|
187
188
|
for (const key in replace) {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
text = text.replace(new RegExp('\\{' + this.escapeRegex(key) + '\\}', 'g'), value.toString());
|
|
191
|
-
}
|
|
189
|
+
const value = replace[key];
|
|
190
|
+
text = text.replace(new RegExp('\\{' + this.escapeRegex(key) + '\\}', 'g'), value.toString());
|
|
192
191
|
}
|
|
193
192
|
return text;
|
|
194
193
|
}
|
|
@@ -196,12 +195,11 @@ class I18n {
|
|
|
196
195
|
return localizedDomain[this.country] ?? localizedDomain[''];
|
|
197
196
|
}
|
|
198
197
|
localizedDomains = {
|
|
199
|
-
adminUrl: () => 'https://' + STAMHOOFD.domains.dashboard + '/' +
|
|
198
|
+
adminUrl: () => 'https://' + STAMHOOFD.domains.dashboard + '/' + appToUri('admin'),
|
|
200
199
|
dashboard: STAMHOOFD.domains.dashboard,
|
|
201
200
|
marketing: () => this.getDomain(STAMHOOFD.domains.marketing),
|
|
202
201
|
defaultTransactionalEmail: () => this.getDomain(STAMHOOFD.domains.defaultTransactionalEmail ?? { ['']: 'stamhoofd.be' }),
|
|
203
202
|
defaultBroadcastEmail: () => this.getDomain(STAMHOOFD.domains.defaultBroadcastEmail ?? { ['']: 'stamhoofd.email' }),
|
|
204
203
|
};
|
|
205
204
|
}
|
|
206
|
-
exports.I18n = I18n;
|
|
207
205
|
//# sourceMappingURL=I18n.js.map
|
package/dist/I18n.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"I18n.js","sourceRoot":"","sources":["../src/I18n.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"I18n.js","sourceRoot":"","sources":["../src/I18n.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,MAAM,IAAI,CAAC;AACpC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,iFAAiF;AACjF,MAAM,OAAO,GAAG,aAAa,CAAC,MA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IAAI,CAAC,OAAO,CAAC,IAAI,eAAe,CAAC,EAAE,CAAC,CAAC;IAChE,CAAC;IAED,gBAAgB,GAAG;QACf,QAAQ,EAAE,GAAG,EAAE,CAAC,UAAU,GAAG,SAAS,CAAC,OAAO,CAAC,SAAS,GAAG,GAAG,GAAG,QAAQ,CAAC,OAAO,CAAC;QAClF,SAAS,EAAE,SAAS,CAAC,OAAO,CAAC,SAAS;QACtC,SAAS,EAAE,GAAW,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,OAAO,CAAC,SAAS,CAAC;QACpE,yBAAyB,EAAE,GAAW,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,OAAO,CAAC,yBAAyB,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,cAAc,EAAE,CAAC;QAChI,qBAAqB,EAAE,GAAW,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,OAAO,CAAC,qBAAqB,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,iBAAiB,EAAE,CAAC;KAC9H,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { I18n } from
|
|
2
|
-
export * from
|
|
3
|
-
declare module '@simonbackx/simple-endpoints
|
|
1
|
+
import { I18n } from './I18n.js';
|
|
2
|
+
export * from './I18n.js';
|
|
3
|
+
declare module '@simonbackx/simple-endpoints' {
|
|
4
4
|
interface DecodedRequest<Params, Query, Body> {
|
|
5
5
|
get i18n(): I18n;
|
|
6
6
|
$t(key: string, replace?: Record<string, string>): string;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,cAAc,WAAW,CAAC;AAG1B,OAAO,QAAQ,8BAA8B,CAAC;IAC1C,UAAU,cAAc,CAAC,MAAM,EAAE,KAAK,EAAE,IAAI;QACxC,IAAI,IAAI,IAAI,IAAI,CAAC;QACjB,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC;KAC7D;CACJ"}
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const I18n_1 = require("./I18n");
|
|
6
|
-
tslib_1.__exportStar(require("./I18n"), exports);
|
|
7
|
-
Object.defineProperty(DecodedRequest_1.DecodedRequest.prototype, "i18n", {
|
|
1
|
+
import { DecodedRequest } from '@simonbackx/simple-endpoints';
|
|
2
|
+
import { I18n } from './I18n.js';
|
|
3
|
+
export * from './I18n.js';
|
|
4
|
+
Object.defineProperty(DecodedRequest.prototype, 'i18n', {
|
|
8
5
|
get() {
|
|
9
|
-
return
|
|
10
|
-
}
|
|
6
|
+
return I18n.fromRequest(this);
|
|
7
|
+
},
|
|
11
8
|
});
|
|
12
|
-
|
|
9
|
+
DecodedRequest.prototype.$t = function (key, replace) {
|
|
13
10
|
return this.i18n.$t(key, replace);
|
|
14
11
|
};
|
|
15
12
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,cAAc,WAAW,CAAC;AAU1B,MAAM,CAAC,cAAc,CAAC,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE;IACpD,GAAG;QACC,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAClC,CAAC;CACJ,CAAC,CAAC;AAEH,cAAc,CAAC,SAAS,CAAC,EAAE,GAAG,UAAyE,GAAW,EAAE,OAAgC;IAChJ,OAAO,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AACtC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,22 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stamhoofd/backend-i18n",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"
|
|
3
|
+
"version": "2.120.0",
|
|
4
|
+
"type": "module",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
|
+
"main": "./dist/index.js",
|
|
6
7
|
"license": "UNLICENCED",
|
|
7
8
|
"sideEffects": false,
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"require": "./dist/index.js",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./*": {
|
|
16
|
+
"types": "./dist/*.d.ts",
|
|
17
|
+
"require": "./dist/*.js",
|
|
18
|
+
"import": "./dist/*.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
8
21
|
"files": [
|
|
9
|
-
"dist"
|
|
10
|
-
"src"
|
|
22
|
+
"dist"
|
|
11
23
|
],
|
|
12
24
|
"scripts": {
|
|
13
|
-
"build": "tsc
|
|
25
|
+
"build": "tsc --build tsconfig.build.json",
|
|
26
|
+
"lint": "eslint"
|
|
14
27
|
},
|
|
15
28
|
"peerDependencies": {
|
|
16
|
-
"@simonbackx/simple-endpoints": "
|
|
29
|
+
"@simonbackx/simple-endpoints": "1.21.0"
|
|
17
30
|
},
|
|
18
31
|
"publishConfig": {
|
|
19
32
|
"access": "public"
|
|
20
33
|
},
|
|
21
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "f38f79c15ce16b0c8c14743ff3eb61feda5a18d4"
|
|
22
35
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"root":["../src/i18n.ts","../src/index.ts","../../../../environment.d.ts","../../../../jest-extended.d.ts","../../../stamhoofd.d.ts"],"version":"5.8.3"}
|
package/src/I18n.ts
DELETED
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
import { DecodedRequest, Request } from '@simonbackx/simple-endpoints';
|
|
2
|
-
import { logger, StyledText } from '@simonbackx/simple-logging';
|
|
3
|
-
import { countries, languages } from '@stamhoofd/locales';
|
|
4
|
-
import { appToUri, Country, Language } from '@stamhoofd/structures';
|
|
5
|
-
import { promises as fs } from 'fs';
|
|
6
|
-
import path from 'path';
|
|
7
|
-
|
|
8
|
-
export class I18n {
|
|
9
|
-
static loadedLocales: Map<string, Map<string, string>> = new Map();
|
|
10
|
-
static defaultLanguage = Language.Dutch;
|
|
11
|
-
static defaultCountry = Country.Belgium;
|
|
12
|
-
|
|
13
|
-
static async load() {
|
|
14
|
-
await logger.setContext({
|
|
15
|
-
prefixes: [
|
|
16
|
-
new StyledText('[I18n] ').addClass('i18n', 'tag'),
|
|
17
|
-
],
|
|
18
|
-
tags: ['i18n'],
|
|
19
|
-
}, async () => {
|
|
20
|
-
console.log('Loading locales...');
|
|
21
|
-
|
|
22
|
-
if (!STAMHOOFD.translationNamespace) {
|
|
23
|
-
throw new Error('STAMHOOFD.translationNamespace is not set');
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const directory = path.dirname(require.resolve('@stamhoofd/locales')) + '/' + STAMHOOFD.translationNamespace;
|
|
27
|
-
const files = (await fs.readdir(directory, { withFileTypes: true }))
|
|
28
|
-
.filter(dirent => !dirent.isDirectory());
|
|
29
|
-
|
|
30
|
-
for (const file of files) {
|
|
31
|
-
const locale = file.name.substr(0, file.name.length - 5);
|
|
32
|
-
|
|
33
|
-
const messages = await import(directory + '/' + file.name);
|
|
34
|
-
this.loadedLocales.set(locale, this.loadRecursive(messages.default));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
console.log('Loaded all locales');
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
static loadRecursive(messages: any, prefix: string | null = null): Map<string, string> {
|
|
42
|
-
const map = new Map();
|
|
43
|
-
for (const key in messages) {
|
|
44
|
-
const element = messages[key];
|
|
45
|
-
if (typeof (element) !== 'string') {
|
|
46
|
-
const map2 = this.loadRecursive(element, (prefix ? prefix + '.' : '') + key);
|
|
47
|
-
map2.forEach((val, key) => map.set(key, val));
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
map.set((prefix ? prefix + '.' : '') + key, element);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return map;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
static get validLocales() {
|
|
57
|
-
// todo: make platform specific
|
|
58
|
-
return (STAMHOOFD.locales ?? {
|
|
59
|
-
[Country.Belgium]: [Language.Dutch],
|
|
60
|
-
}) as Partial<Record<Country, Language[]>>;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
static isValidLocale(locale: string) {
|
|
64
|
-
if (locale.length == 5 && locale.substr(2, 1) == '-') {
|
|
65
|
-
const l = locale.substr(0, 2).toLowerCase() as Language;
|
|
66
|
-
const c = locale.substr(3, 2).toUpperCase();
|
|
67
|
-
|
|
68
|
-
if (languages.includes(l) && (countries as string[]).includes(c)) {
|
|
69
|
-
return this.validLocales[c as Country]?.includes(l) ?? false;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return false;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
language = Language.Dutch;
|
|
76
|
-
country = Country.Belgium;
|
|
77
|
-
|
|
78
|
-
// Reference to messages in loadedLocales
|
|
79
|
-
messages: Map<string, string>;
|
|
80
|
-
|
|
81
|
-
get locale() {
|
|
82
|
-
return this.language + '-' + this.country;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
constructor(language: Language, country: Country) {
|
|
86
|
-
this.language = language;
|
|
87
|
-
this.country = country;
|
|
88
|
-
this.correctLanguageCountryCombination();
|
|
89
|
-
|
|
90
|
-
const m = I18n.loadedLocales.get(this.locale);
|
|
91
|
-
if (!m) {
|
|
92
|
-
if (STAMHOOFD.environment === 'test') {
|
|
93
|
-
this.messages = new Map();
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
throw new Error('Locale not loaded when creating I18n for ' + language + '-' + country);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
this.messages = m;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
correctLanguageCountryCombination() {
|
|
103
|
-
if (I18n.isValidLocale(this.locale)) {
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
const validLocales = I18n.validLocales;
|
|
107
|
-
|
|
108
|
-
if (!(this.country in validLocales)) {
|
|
109
|
-
// Find first coutnry with same language
|
|
110
|
-
for (const country of countries) {
|
|
111
|
-
if (validLocales[country]?.includes(this.language)) {
|
|
112
|
-
this.country = country;
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// Fallback
|
|
118
|
-
this.country = countries[0];
|
|
119
|
-
this.language = validLocales[this.country]![0];
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (!validLocales[this.country]?.includes(this.language)) {
|
|
124
|
-
if (validLocales[this.country]?.includes(Language.English)) {
|
|
125
|
-
this.language = Language.English;
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
this.language = validLocales[this.country]![0];
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
switchToLocale(options: {
|
|
134
|
-
language?: Language;
|
|
135
|
-
country?: Country;
|
|
136
|
-
}) {
|
|
137
|
-
this.country = options.country ?? this.country;
|
|
138
|
-
this.language = options.language ?? this.language;
|
|
139
|
-
this.correctLanguageCountryCombination();
|
|
140
|
-
|
|
141
|
-
const m = I18n.loadedLocales.get(this.locale);
|
|
142
|
-
if (!m) {
|
|
143
|
-
if (STAMHOOFD.environment === 'test') {
|
|
144
|
-
this.messages = new Map();
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
throw new Error('Locale not loaded, when switching to locale ' + this.language + '-' + this.country);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
this.messages = m;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
static fromRequest(request: Request | DecodedRequest<any, any, any>): I18n {
|
|
154
|
-
if ((request as any)._cached_i18n) {
|
|
155
|
-
return (request as any)._cached_i18n;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// Try using custom property
|
|
159
|
-
const localeHeader = request.headers['x-locale'];
|
|
160
|
-
if (localeHeader && typeof localeHeader === 'string' && this.isValidLocale(localeHeader)) {
|
|
161
|
-
const l = localeHeader.substr(0, 2).toLowerCase() as Language;
|
|
162
|
-
const c = localeHeader.substr(3, 2).toUpperCase() as Country;
|
|
163
|
-
|
|
164
|
-
const i18n = new I18n(l, c);
|
|
165
|
-
(request as any)._cached_i18n = i18n;
|
|
166
|
-
return i18n;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// Try using accept-language defaults
|
|
170
|
-
const acceptLanguage = request.headers['accept-language'];
|
|
171
|
-
if (acceptLanguage) {
|
|
172
|
-
const splitted = acceptLanguage.split(',');
|
|
173
|
-
|
|
174
|
-
// Loop all countries and languages in the header, until we find a valid one
|
|
175
|
-
for (const item of splitted) {
|
|
176
|
-
const trimmed = item.trim();
|
|
177
|
-
const localeSplit = trimmed.split(';');
|
|
178
|
-
const locale = localeSplit[0];
|
|
179
|
-
|
|
180
|
-
if (locale.length === 2) {
|
|
181
|
-
// Language
|
|
182
|
-
if (languages.includes(locale as Language)) {
|
|
183
|
-
// Use a default country
|
|
184
|
-
// Country can get overriden when matching a organization
|
|
185
|
-
// using .setCountry(country) method
|
|
186
|
-
const i18n = new I18n(locale as Language, Country.Belgium);
|
|
187
|
-
(request as any)._cached_i18n = i18n;
|
|
188
|
-
return i18n;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
else if (locale.length === 5 && this.isValidLocale(locale)) {
|
|
192
|
-
const l = locale.substr(0, 2).toLowerCase() as Language;
|
|
193
|
-
const c = locale.substr(3, 2).toUpperCase() as Country;
|
|
194
|
-
|
|
195
|
-
// Lang + country
|
|
196
|
-
const i18n = new I18n(l, c);
|
|
197
|
-
(request as any)._cached_i18n = i18n;
|
|
198
|
-
return i18n;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
const i18n = new I18n(this.defaultLanguage, this.defaultCountry);
|
|
203
|
-
(request as any)._cached_i18n = i18n;
|
|
204
|
-
return i18n;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
t(key: string, replace?: Record<string, string>) {
|
|
208
|
-
return this.$t(key, replace);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
$t(key: string, replace?: Record<string, string>) {
|
|
212
|
-
return this.replace(this.messages.get(key) ?? key, replace);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
escapeRegex(string) {
|
|
216
|
-
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
replace(text: string, replace?: Record<string, string | { toString(): string }>) {
|
|
220
|
-
if (!replace) {
|
|
221
|
-
return text;
|
|
222
|
-
}
|
|
223
|
-
for (const key in replace) {
|
|
224
|
-
if (replace.hasOwnProperty(key)) {
|
|
225
|
-
const value = replace[key];
|
|
226
|
-
text = text.replace(new RegExp('\\{' + this.escapeRegex(key) + '\\}', 'g'), value.toString());
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
return text;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
getDomain(localizedDomain: LocalizedDomain): string {
|
|
233
|
-
return localizedDomain[this.country] ?? localizedDomain[''];
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
localizedDomains = {
|
|
237
|
-
adminUrl: () => 'https://' + STAMHOOFD.domains.dashboard + '/' + appToUri('admin'),
|
|
238
|
-
dashboard: STAMHOOFD.domains.dashboard,
|
|
239
|
-
marketing: (): string => this.getDomain(STAMHOOFD.domains.marketing),
|
|
240
|
-
defaultTransactionalEmail: (): string => this.getDomain(STAMHOOFD.domains.defaultTransactionalEmail ?? { ['']: 'stamhoofd.be' }),
|
|
241
|
-
defaultBroadcastEmail: (): string => this.getDomain(STAMHOOFD.domains.defaultBroadcastEmail ?? { ['']: 'stamhoofd.email' }),
|
|
242
|
-
};
|
|
243
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { DecodedRequest } from "@simonbackx/simple-endpoints/dist/src/DecodedRequest";
|
|
2
|
-
import { I18n } from "./I18n";
|
|
3
|
-
|
|
4
|
-
export * from "./I18n"
|
|
5
|
-
|
|
6
|
-
// Extend decoded request here
|
|
7
|
-
declare module '@simonbackx/simple-endpoints/dist/src/DecodedRequest' {
|
|
8
|
-
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
|
|
9
|
-
interface DecodedRequest<Params, Query, Body> {
|
|
10
|
-
get i18n(): I18n
|
|
11
|
-
$t(key: string, replace?: Record<string, string>): string
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
Object.defineProperty(DecodedRequest.prototype, "i18n", {
|
|
16
|
-
get() {
|
|
17
|
-
return I18n.fromRequest(this)
|
|
18
|
-
}
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
DecodedRequest.prototype.$t = function<Params, Query, Body>(this: DecodedRequest<Params, Query, Body>, key: string, replace?: Record<string, string>): string {
|
|
22
|
-
return this.i18n.$t(key, replace)
|
|
23
|
-
}
|