opticore-translator 1.0.14 → 1.0.15
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.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +4 -4
- package/src/core/loaders/translation.loader.ts +75 -0
- package/src/domains/interfaces/loader.options.interface.ts +5 -0
- package/src/domains/interfaces/translation.interface.ts +3 -0
- package/src/index.ts +5 -0
- package/src/utils/translations/message.translation.en.json +5 -0
- package/src/utils/translations/message.translation.fr.json +4 -0
- package/tsconfig.json +1 -1
- package/tsup.config.ts +11 -0
package/dist/index.cjs
CHANGED
|
@@ -53,7 +53,7 @@ var TranslationLoader = class {
|
|
|
53
53
|
const locale = import_path.default.basename(file, ".json");
|
|
54
54
|
const filePath = import_path.default.join(directory, file);
|
|
55
55
|
try {
|
|
56
|
-
this.translations[locale] = JSON.parse(import_fs.default.readFileSync(filePath, "utf-8"));
|
|
56
|
+
this.translations[locale] = { ...this.translations[locale] || {}, ...JSON.parse(import_fs.default.readFileSync(filePath, "utf-8")) };
|
|
57
57
|
} catch (error) {
|
|
58
58
|
this.loggerConfig.error({
|
|
59
59
|
message: error.message,
|
package/dist/index.js
CHANGED
|
@@ -17,7 +17,7 @@ var TranslationLoader = class {
|
|
|
17
17
|
const locale = path.basename(file, ".json");
|
|
18
18
|
const filePath = path.join(directory, file);
|
|
19
19
|
try {
|
|
20
|
-
this.translations[locale] = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
20
|
+
this.translations[locale] = { ...this.translations[locale] || {}, ...JSON.parse(fs.readFileSync(filePath, "utf-8")) };
|
|
21
21
|
} catch (error) {
|
|
22
22
|
this.loggerConfig.error({
|
|
23
23
|
message: error.message,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opticore-translator",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.15",
|
|
4
4
|
"description": "opticore translator to translate according language",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -27,13 +27,13 @@
|
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"ansi-colors": "^4.1.3",
|
|
29
29
|
"chalk": "^5.4.1",
|
|
30
|
-
"opticore-env-access": "^1.0.
|
|
30
|
+
"opticore-env-access": "^1.0.19",
|
|
31
31
|
"opticore-http-response": "^1.0.10",
|
|
32
32
|
"opticore-logger": "^1.0.30",
|
|
33
|
-
"tsx": "^4.
|
|
33
|
+
"tsx": "^4.22.4"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@types/node": "^
|
|
36
|
+
"@types/node": "^24.13.2",
|
|
37
37
|
"tslib": "^2.8.1",
|
|
38
38
|
"tsup": "^8.4.0",
|
|
39
39
|
"typescript": "^5.4.5"
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { HttpStatusCode } from "opticore-http-response";
|
|
4
|
+
import { LoggerCore } from "opticore-logger";
|
|
5
|
+
import { ITranslations } from "@translator/domains/interfaces/translation.interface";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
export class TranslationLoader {
|
|
12
|
+
private static translations: ITranslations = {} as ITranslations;
|
|
13
|
+
private static loggerConfig: LoggerCore = new LoggerCore();
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
* @param directory
|
|
18
|
+
*/
|
|
19
|
+
public static loadTranslations(directory: string): void {
|
|
20
|
+
const files: string[] = fs.readdirSync(directory);
|
|
21
|
+
|
|
22
|
+
files.forEach((file: string): void => {
|
|
23
|
+
if (file.endsWith(".json")) {
|
|
24
|
+
const locale: string = path.basename(file, ".json");
|
|
25
|
+
const filePath: string = path.join(directory, file);
|
|
26
|
+
try {
|
|
27
|
+
this.translations[locale] = { ...(this.translations[locale] || {}), ...JSON.parse(fs.readFileSync(filePath, "utf-8")) };
|
|
28
|
+
} catch (error: any) {
|
|
29
|
+
this.loggerConfig.error({
|
|
30
|
+
message: error.message,
|
|
31
|
+
title: `Error loading translations for ${locale}`,
|
|
32
|
+
errorType: "Translation error",
|
|
33
|
+
stackTrace: error.stack,
|
|
34
|
+
httpCodeValue: HttpStatusCode.INTERNAL_SERVER_ERROR
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* @param key
|
|
45
|
+
* @param localeLanguage
|
|
46
|
+
* @param params
|
|
47
|
+
*
|
|
48
|
+
* using example :
|
|
49
|
+
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
50
|
+
*/
|
|
51
|
+
public static t(key: string, localeLanguage: string, params: Record<any, any> = {}): string {
|
|
52
|
+
const localeKey: string = `message.translation.${localeLanguage}`;
|
|
53
|
+
const message: string = this.translations[localeKey]?.[key] || key;
|
|
54
|
+
|
|
55
|
+
return this.formatMessage(message, params);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
*
|
|
60
|
+
* @param template
|
|
61
|
+
* @param params
|
|
62
|
+
*/
|
|
63
|
+
private static formatMessage(template: string, params: Record<any, any>): string {
|
|
64
|
+
return template.replace(/\{(\w+)\}/g, (_: string, key: any): string => {
|
|
65
|
+
const value: any = params[key];
|
|
66
|
+
if (Array.isArray(value)) {
|
|
67
|
+
return value.join(", ");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return value !== undefined
|
|
71
|
+
? value.toString()
|
|
72
|
+
: `{${key}}`;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
package/src/index.ts
ADDED
package/tsconfig.json
CHANGED
package/tsup.config.ts
ADDED