@ooneex/translation 1.1.12 → 1.2.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/index.d.ts +31 -5
- package/dist/index.js +45 -2
- package/dist/index.js.map +5 -3
- package/package.json +5 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,37 @@
|
|
|
1
|
+
import { EContainerScope } from "@ooneex/container";
|
|
1
2
|
declare const locales: readonly ["ar", "bg", "cs", "da", "de", "el", "en", "eo", "es", "et", "eu", "fi", "fr", "hu", "hy", "it", "ja", "ko", "lt", "nl", "no", "pl", "pt", "ro", "ru", "sk", "sv", "th", "uk", "zh", "zh-tw"];
|
|
2
|
-
import { Exception } from "@ooneex/exception";
|
|
3
|
-
declare class TranslationException extends Exception {
|
|
4
|
-
constructor(message: string, key: string, data?: Record<string, unknown>);
|
|
5
|
-
}
|
|
6
3
|
type LocaleType = (typeof locales)[number];
|
|
7
4
|
type LocaleInfoType = {
|
|
8
5
|
code: LocaleType;
|
|
9
6
|
region: string | null;
|
|
10
7
|
};
|
|
11
|
-
|
|
8
|
+
type TranslationLeafType = Partial<Record<LocaleType, string>>;
|
|
9
|
+
type TranslationDictType = {
|
|
10
|
+
[key: string]: TranslationDictType | TranslationLeafType;
|
|
11
|
+
};
|
|
12
|
+
type TransParamsType = Record<string, boolean | number | bigint | string>;
|
|
13
|
+
type TransOptionsType = {
|
|
14
|
+
lang?: LocaleType;
|
|
15
|
+
params?: TransParamsType;
|
|
16
|
+
count?: number;
|
|
17
|
+
};
|
|
18
|
+
interface ITranslation {
|
|
19
|
+
getName: () => string;
|
|
20
|
+
getDict: () => TranslationDictType;
|
|
21
|
+
has: (key: string) => boolean;
|
|
22
|
+
trans: (key: string, options?: TransOptionsType) => string;
|
|
23
|
+
}
|
|
24
|
+
type TranslationClassType = new (...args: any[]) => ITranslation;
|
|
25
|
+
declare const decorator: {
|
|
26
|
+
translation: (scope?: EContainerScope) => (target: TranslationClassType) => void;
|
|
27
|
+
};
|
|
28
|
+
declare abstract class Translation implements ITranslation {
|
|
29
|
+
has(key: string): boolean;
|
|
30
|
+
trans(key: string, options?: TransOptionsType): string;
|
|
31
|
+
abstract getDict(): TranslationDictType;
|
|
32
|
+
}
|
|
33
|
+
import { Exception } from "@ooneex/exception";
|
|
34
|
+
declare class TranslationException extends Exception {
|
|
35
|
+
constructor(message: string, key: string, data?: Record<string, unknown>);
|
|
36
|
+
}
|
|
37
|
+
export { locales, decorator, TranslationLeafType, TranslationException, TranslationDictType, TranslationClassType, Translation, TransParamsType, TransOptionsType, LocaleType, LocaleInfoType, ITranslation };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/decorators.ts
|
|
3
|
+
import { container, EContainerScope } from "@ooneex/container";
|
|
4
|
+
var decorator = {
|
|
5
|
+
translation: (scope = EContainerScope.Singleton) => {
|
|
6
|
+
return (target) => {
|
|
7
|
+
container.add(target, scope);
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
};
|
|
1
11
|
// src/locales.ts
|
|
2
12
|
var locales = [
|
|
3
13
|
"ar",
|
|
@@ -32,6 +42,9 @@ var locales = [
|
|
|
32
42
|
"zh",
|
|
33
43
|
"zh-tw"
|
|
34
44
|
];
|
|
45
|
+
// src/Translation.ts
|
|
46
|
+
import { has, trans } from "@ooneex/utils/trans";
|
|
47
|
+
|
|
35
48
|
// src/TranslationException.ts
|
|
36
49
|
import { Exception } from "@ooneex/exception";
|
|
37
50
|
import { HttpStatus } from "@ooneex/http-status";
|
|
@@ -46,9 +59,39 @@ class TranslationException extends Exception {
|
|
|
46
59
|
this.name = "TranslationException";
|
|
47
60
|
}
|
|
48
61
|
}
|
|
62
|
+
|
|
63
|
+
// src/Translation.ts
|
|
64
|
+
var FALLBACK_LOCALE = "en";
|
|
65
|
+
|
|
66
|
+
class Translation {
|
|
67
|
+
has(key) {
|
|
68
|
+
return has(this.getDict(), key);
|
|
69
|
+
}
|
|
70
|
+
trans(key, options = {}) {
|
|
71
|
+
const lang = options.lang ?? FALLBACK_LOCALE;
|
|
72
|
+
const result = trans(this.getDict(), key, {
|
|
73
|
+
lang,
|
|
74
|
+
fallbackLang: FALLBACK_LOCALE,
|
|
75
|
+
params: options.params,
|
|
76
|
+
count: options.count
|
|
77
|
+
});
|
|
78
|
+
if (result.found) {
|
|
79
|
+
return result.value;
|
|
80
|
+
}
|
|
81
|
+
if (result.reason === "key") {
|
|
82
|
+
throw new TranslationException(`Translation key "${key}" not found`, "KEY_NOT_FOUND", {
|
|
83
|
+
key,
|
|
84
|
+
lang
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
throw new TranslationException(`Translation "${key}" is missing for locale "${lang}" and fallback "${FALLBACK_LOCALE}"`, "LOCALE_NOT_FOUND", { key, lang });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
49
90
|
export {
|
|
50
91
|
locales,
|
|
51
|
-
|
|
92
|
+
decorator,
|
|
93
|
+
TranslationException,
|
|
94
|
+
Translation
|
|
52
95
|
};
|
|
53
96
|
|
|
54
|
-
//# debugId=
|
|
97
|
+
//# debugId=5CCB49308EB7696F64756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["src/locales.ts", "src/TranslationException.ts"],
|
|
3
|
+
"sources": ["src/decorators.ts", "src/locales.ts", "src/Translation.ts", "src/TranslationException.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
+
"import { container, EContainerScope } from \"@ooneex/container\";\nimport type { TranslationClassType } from \"./types\";\n\nexport const decorator = {\n translation: (scope: EContainerScope = EContainerScope.Singleton) => {\n return (target: TranslationClassType): void => {\n container.add(target, scope);\n };\n },\n};\n",
|
|
5
6
|
"export const locales = [\n \"ar\",\n \"bg\",\n \"cs\",\n \"da\",\n \"de\",\n \"el\",\n \"en\",\n \"eo\",\n \"es\",\n \"et\",\n \"eu\",\n \"fi\",\n \"fr\",\n \"hu\",\n \"hy\",\n \"it\",\n \"ja\",\n \"ko\",\n \"lt\",\n \"nl\",\n \"no\",\n \"pl\",\n \"pt\",\n \"ro\",\n \"ru\",\n \"sk\",\n \"sv\",\n \"th\",\n \"uk\",\n \"zh\",\n \"zh-tw\",\n] as const;\n",
|
|
7
|
+
"import { has, trans } from \"@ooneex/utils/trans\";\nimport { TranslationException } from \"./TranslationException\";\nimport type { ITranslation, LocaleType, TranslationDictType, TransOptionsType } from \"./types\";\n\nconst FALLBACK_LOCALE: LocaleType = \"en\";\n\nexport abstract class Translation implements ITranslation {\n public has(key: string): boolean {\n return has(this.getDict(), key);\n }\n\n public trans(key: string, options: TransOptionsType = {}): string {\n const lang = options.lang ?? FALLBACK_LOCALE;\n const result = trans(this.getDict(), key, {\n lang,\n fallbackLang: FALLBACK_LOCALE,\n params: options.params,\n count: options.count,\n });\n\n if (result.found) {\n return result.value;\n }\n\n if (result.reason === \"key\") {\n throw new TranslationException(`Translation key \"${key}\" not found`, \"KEY_NOT_FOUND\", {\n key,\n lang,\n });\n }\n\n throw new TranslationException(\n `Translation \"${key}\" is missing for locale \"${lang}\" and fallback \"${FALLBACK_LOCALE}\"`,\n \"LOCALE_NOT_FOUND\",\n { key, lang },\n );\n }\n\n public abstract getName(): string;\n\n public abstract getDict(): TranslationDictType;\n}\n",
|
|
6
8
|
"import { Exception } from \"@ooneex/exception\";\nimport { HttpStatus } from \"@ooneex/http-status\";\n\nexport class TranslationException extends Exception {\n constructor(message: string, key: string, data: Record<string, unknown> = {}) {\n super(message, {\n key,\n status: HttpStatus.Code.NotFound,\n data,\n });\n this.name = \"TranslationException\";\n }\n}\n"
|
|
7
9
|
],
|
|
8
|
-
"mappings": ";
|
|
9
|
-
"debugId": "
|
|
10
|
+
"mappings": ";;AAAA;AAGO,IAAM,YAAY;AAAA,EACvB,aAAa,CAAC,QAAyB,gBAAgB,cAAc;AAAA,IACnE,OAAO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QACA;AAAA,MACF,CAAC;AAAA,IACH;AAAA,IAEA,MAAM,IAAI,qBACR,gBAAgB,+BAA+B,uBAAuB,oBACtE,oBACA,EAAE,KAAK,KAAK,CACd;AAAA;AAMJ;",
|
|
11
|
+
"debugId": "5CCB49308EB7696F64756E2164756E21",
|
|
10
12
|
"names": []
|
|
11
13
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ooneex/translation",
|
|
3
3
|
"description": "Internationalization framework with locale management, translation key resolution, and pluralization support for multi-language applications",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -29,8 +29,10 @@
|
|
|
29
29
|
"wuchale": "bunx wuchale"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@ooneex/
|
|
33
|
-
"@ooneex/
|
|
32
|
+
"@ooneex/container": "^1.5.1",
|
|
33
|
+
"@ooneex/exception": "^1.2.10",
|
|
34
|
+
"@ooneex/http-status": "^1.1.11",
|
|
35
|
+
"@ooneex/utils": "^0.5.0"
|
|
34
36
|
},
|
|
35
37
|
"devDependencies": {},
|
|
36
38
|
"keywords": [
|