@node-yalc/i18n 0.0.0-stage → 0.0.3
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-engine.d.ts +7 -0
- package/dist/i18n-engine.js +30 -0
- package/dist/i18n-engine.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/package.json +7 -6
- package/src/i18n-engine.ts +32 -0
- package/src/index.ts +1 -0
- package/README.md +0 -3
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare class I18nEngine {
|
|
2
|
+
private translations;
|
|
3
|
+
private defaultLocale;
|
|
4
|
+
constructor(defaultLocale?: string);
|
|
5
|
+
registerTranslations(locale: string, dict: Record<string, string>): void;
|
|
6
|
+
translate(key: string, params?: Record<string, any>, locale?: string): string;
|
|
7
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.I18nEngine = void 0;
|
|
4
|
+
class I18nEngine {
|
|
5
|
+
constructor(defaultLocale = 'en') {
|
|
6
|
+
this.translations = new Map();
|
|
7
|
+
this.defaultLocale = defaultLocale;
|
|
8
|
+
}
|
|
9
|
+
registerTranslations(locale, dict) {
|
|
10
|
+
if (!this.translations.has(locale)) {
|
|
11
|
+
this.translations.set(locale, new Map());
|
|
12
|
+
}
|
|
13
|
+
const map = this.translations.get(locale);
|
|
14
|
+
for (const [k, v] of Object.entries(dict)) {
|
|
15
|
+
map.set(k, v);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
translate(key, params = {}, locale) {
|
|
19
|
+
const targetLocale = locale || this.defaultLocale;
|
|
20
|
+
let template = this.translations.get(targetLocale)?.get(key) ||
|
|
21
|
+
this.translations.get(this.defaultLocale)?.get(key) ||
|
|
22
|
+
key;
|
|
23
|
+
for (const [paramKey, paramVal] of Object.entries(params)) {
|
|
24
|
+
template = template.replace(new RegExp(`{{\\s*${paramKey}\\s*}}`, 'g'), String(paramVal));
|
|
25
|
+
}
|
|
26
|
+
return template;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.I18nEngine = I18nEngine;
|
|
30
|
+
//# sourceMappingURL=i18n-engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i18n-engine.js","sourceRoot":"","sources":["../../../i18n/src/i18n-engine.ts"],"names":[],"mappings":";;;AAAA,MAAa,UAAU;IAIrB,YAAY,gBAAwB,IAAI;QAHhC,iBAAY,GAAqC,IAAI,GAAG,EAAE,CAAC;QAIjE,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;IACrC,CAAC;IAEM,oBAAoB,CAAC,MAAc,EAAE,IAA4B;QACtE,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;YACnC,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,GAAG,EAAE,CAAC,CAAC;QAC3C,CAAC;QACD,MAAM,GAAG,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,MAAM,CAAE,CAAC;QAC3C,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YAC1C,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAChB,CAAC;IACH,CAAC;IAEM,SAAS,CAAC,GAAW,EAAE,SAA8B,EAAE,EAAE,MAAe;QAC7E,MAAM,YAAY,GAAG,MAAM,IAAI,IAAI,CAAC,aAAa,CAAC;QAClD,IAAI,QAAQ,GACV,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,YAAY,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC;YAC7C,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC;YACnD,GAAG,CAAC;QAEN,KAAK,MAAM,CAAC,QAAQ,EAAE,QAAQ,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;YAC1D,QAAQ,GAAG,QAAQ,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,SAAS,QAAQ,QAAQ,EAAE,GAAG,CAAC,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;QAC5F,CAAC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF;AA/BD,gCA+BC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './i18n-engine';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../i18n/src/index.ts"],"names":[],"mappings":";;;AAAA,wDAA8B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@node-yalc/i18n",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"type": "module"
|
|
7
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export class I18nEngine {
|
|
2
|
+
private translations: Map<string, Map<string, string>> = new Map();
|
|
3
|
+
private defaultLocale: string;
|
|
4
|
+
|
|
5
|
+
constructor(defaultLocale: string = 'en') {
|
|
6
|
+
this.defaultLocale = defaultLocale;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
public registerTranslations(locale: string, dict: Record<string, string>): void {
|
|
10
|
+
if (!this.translations.has(locale)) {
|
|
11
|
+
this.translations.set(locale, new Map());
|
|
12
|
+
}
|
|
13
|
+
const map = this.translations.get(locale)!;
|
|
14
|
+
for (const [k, v] of Object.entries(dict)) {
|
|
15
|
+
map.set(k, v);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public translate(key: string, params: Record<string, any> = {}, locale?: string): string {
|
|
20
|
+
const targetLocale = locale || this.defaultLocale;
|
|
21
|
+
let template =
|
|
22
|
+
this.translations.get(targetLocale)?.get(key) ||
|
|
23
|
+
this.translations.get(this.defaultLocale)?.get(key) ||
|
|
24
|
+
key;
|
|
25
|
+
|
|
26
|
+
for (const [paramKey, paramVal] of Object.entries(params)) {
|
|
27
|
+
template = template.replace(new RegExp(`{{\\s*${paramKey}\\s*}}`, 'g'), String(paramVal));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return template;
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './i18n-engine';
|
package/README.md
DELETED