opticore-translator 1.0.6 → 1.0.8
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 +10 -15
- package/dist/index.d.cts +3 -7
- package/dist/index.d.ts +3 -7
- package/dist/index.js +10 -15
- package/example/index.ts +1 -3
- package/package.json +1 -1
- package/tsconfig.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -38,13 +38,15 @@ module.exports = __toCommonJS(index_exports);
|
|
|
38
38
|
var import_fs = __toESM(require("fs"), 1);
|
|
39
39
|
var import_path = __toESM(require("path"), 1);
|
|
40
40
|
var import_opticore_http_response = require("opticore-http-response");
|
|
41
|
+
var import_opticore_logger = require("opticore-logger");
|
|
41
42
|
var TranslationLoader = class {
|
|
43
|
+
static translations = {};
|
|
44
|
+
static loggerConfig = new import_opticore_logger.LoggerCore();
|
|
42
45
|
/**
|
|
43
46
|
*
|
|
44
47
|
* @param directory
|
|
45
|
-
* @param loggerConfig
|
|
46
48
|
*/
|
|
47
|
-
static loadTranslations(directory
|
|
49
|
+
static loadTranslations(directory) {
|
|
48
50
|
const files = import_fs.default.readdirSync(directory);
|
|
49
51
|
files.forEach((file) => {
|
|
50
52
|
if (file.endsWith(".json")) {
|
|
@@ -52,9 +54,8 @@ var TranslationLoader = class {
|
|
|
52
54
|
const filePath = import_path.default.join(directory, file);
|
|
53
55
|
try {
|
|
54
56
|
this.translations[locale] = JSON.parse(import_fs.default.readFileSync(filePath, "utf-8"));
|
|
55
|
-
loggerConfig.success("Translation Loaded", `Translations loaded for locale: ${locale}`);
|
|
56
57
|
} catch (error) {
|
|
57
|
-
loggerConfig.error(
|
|
58
|
+
this.loggerConfig.error(
|
|
58
59
|
error.message,
|
|
59
60
|
`Error loading translations for ${locale}`,
|
|
60
61
|
"Translation error",
|
|
@@ -69,37 +70,31 @@ var TranslationLoader = class {
|
|
|
69
70
|
*
|
|
70
71
|
* @param key
|
|
71
72
|
* @param localeLanguage
|
|
72
|
-
* @param loggerConfig
|
|
73
73
|
* @param params
|
|
74
74
|
*
|
|
75
75
|
* using example :
|
|
76
76
|
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
77
77
|
*/
|
|
78
|
-
static t(key, localeLanguage,
|
|
79
|
-
var _a;
|
|
78
|
+
static t(key, localeLanguage, params = {}) {
|
|
80
79
|
const localeKey = `message.translation.${localeLanguage}`;
|
|
81
|
-
const message =
|
|
82
|
-
return this.formatMessage(message,
|
|
80
|
+
const message = this.translations[localeKey]?.[key] || key;
|
|
81
|
+
return this.formatMessage(message, params);
|
|
83
82
|
}
|
|
84
83
|
/**
|
|
85
84
|
*
|
|
86
85
|
* @param template
|
|
87
|
-
* @param loggerConfig
|
|
88
86
|
* @param params
|
|
89
87
|
*/
|
|
90
|
-
static formatMessage(template,
|
|
88
|
+
static formatMessage(template, params) {
|
|
91
89
|
return template.replace(/\{(\w+)\}/g, (_, key) => {
|
|
92
90
|
const value = params[key];
|
|
93
91
|
if (Array.isArray(value)) {
|
|
94
92
|
return value.join(", ");
|
|
95
93
|
}
|
|
96
|
-
|
|
97
|
-
loggerConfig.info("Formatting message", `Replacing {${key}} with: ${formattedValue}`);
|
|
98
|
-
return formattedValue;
|
|
94
|
+
return value !== void 0 ? value.toString() : `{${key}}`;
|
|
99
95
|
});
|
|
100
96
|
}
|
|
101
97
|
};
|
|
102
|
-
TranslationLoader.translations = {};
|
|
103
98
|
// Annotate the CommonJS export names for ESM import in node:
|
|
104
99
|
0 && (module.exports = {
|
|
105
100
|
TranslationLoader
|
package/dist/index.d.cts
CHANGED
|
@@ -1,28 +1,24 @@
|
|
|
1
|
-
import { LoggerCore } from 'opticore-logger';
|
|
2
|
-
|
|
3
1
|
declare class TranslationLoader {
|
|
4
2
|
private static translations;
|
|
3
|
+
private static loggerConfig;
|
|
5
4
|
/**
|
|
6
5
|
*
|
|
7
6
|
* @param directory
|
|
8
|
-
* @param loggerConfig
|
|
9
7
|
*/
|
|
10
|
-
static loadTranslations(directory: string
|
|
8
|
+
static loadTranslations(directory: string): void;
|
|
11
9
|
/**
|
|
12
10
|
*
|
|
13
11
|
* @param key
|
|
14
12
|
* @param localeLanguage
|
|
15
|
-
* @param loggerConfig
|
|
16
13
|
* @param params
|
|
17
14
|
*
|
|
18
15
|
* using example :
|
|
19
16
|
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
20
17
|
*/
|
|
21
|
-
static t(key: string, localeLanguage: string,
|
|
18
|
+
static t(key: string, localeLanguage: string, params?: Record<any, any>): string;
|
|
22
19
|
/**
|
|
23
20
|
*
|
|
24
21
|
* @param template
|
|
25
|
-
* @param loggerConfig
|
|
26
22
|
* @param params
|
|
27
23
|
*/
|
|
28
24
|
private static formatMessage;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,28 +1,24 @@
|
|
|
1
|
-
import { LoggerCore } from 'opticore-logger';
|
|
2
|
-
|
|
3
1
|
declare class TranslationLoader {
|
|
4
2
|
private static translations;
|
|
3
|
+
private static loggerConfig;
|
|
5
4
|
/**
|
|
6
5
|
*
|
|
7
6
|
* @param directory
|
|
8
|
-
* @param loggerConfig
|
|
9
7
|
*/
|
|
10
|
-
static loadTranslations(directory: string
|
|
8
|
+
static loadTranslations(directory: string): void;
|
|
11
9
|
/**
|
|
12
10
|
*
|
|
13
11
|
* @param key
|
|
14
12
|
* @param localeLanguage
|
|
15
|
-
* @param loggerConfig
|
|
16
13
|
* @param params
|
|
17
14
|
*
|
|
18
15
|
* using example :
|
|
19
16
|
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
20
17
|
*/
|
|
21
|
-
static t(key: string, localeLanguage: string,
|
|
18
|
+
static t(key: string, localeLanguage: string, params?: Record<any, any>): string;
|
|
22
19
|
/**
|
|
23
20
|
*
|
|
24
21
|
* @param template
|
|
25
|
-
* @param loggerConfig
|
|
26
22
|
* @param params
|
|
27
23
|
*/
|
|
28
24
|
private static formatMessage;
|
package/dist/index.js
CHANGED
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import path from "path";
|
|
4
4
|
import { HttpStatusCode } from "opticore-http-response";
|
|
5
|
+
import { LoggerCore } from "opticore-logger";
|
|
5
6
|
var TranslationLoader = class {
|
|
7
|
+
static translations = {};
|
|
8
|
+
static loggerConfig = new LoggerCore();
|
|
6
9
|
/**
|
|
7
10
|
*
|
|
8
11
|
* @param directory
|
|
9
|
-
* @param loggerConfig
|
|
10
12
|
*/
|
|
11
|
-
static loadTranslations(directory
|
|
13
|
+
static loadTranslations(directory) {
|
|
12
14
|
const files = fs.readdirSync(directory);
|
|
13
15
|
files.forEach((file) => {
|
|
14
16
|
if (file.endsWith(".json")) {
|
|
@@ -16,9 +18,8 @@ var TranslationLoader = class {
|
|
|
16
18
|
const filePath = path.join(directory, file);
|
|
17
19
|
try {
|
|
18
20
|
this.translations[locale] = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
19
|
-
loggerConfig.success("Translation Loaded", `Translations loaded for locale: ${locale}`);
|
|
20
21
|
} catch (error) {
|
|
21
|
-
loggerConfig.error(
|
|
22
|
+
this.loggerConfig.error(
|
|
22
23
|
error.message,
|
|
23
24
|
`Error loading translations for ${locale}`,
|
|
24
25
|
"Translation error",
|
|
@@ -33,37 +34,31 @@ var TranslationLoader = class {
|
|
|
33
34
|
*
|
|
34
35
|
* @param key
|
|
35
36
|
* @param localeLanguage
|
|
36
|
-
* @param loggerConfig
|
|
37
37
|
* @param params
|
|
38
38
|
*
|
|
39
39
|
* using example :
|
|
40
40
|
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
41
41
|
*/
|
|
42
|
-
static t(key, localeLanguage,
|
|
43
|
-
var _a;
|
|
42
|
+
static t(key, localeLanguage, params = {}) {
|
|
44
43
|
const localeKey = `message.translation.${localeLanguage}`;
|
|
45
|
-
const message =
|
|
46
|
-
return this.formatMessage(message,
|
|
44
|
+
const message = this.translations[localeKey]?.[key] || key;
|
|
45
|
+
return this.formatMessage(message, params);
|
|
47
46
|
}
|
|
48
47
|
/**
|
|
49
48
|
*
|
|
50
49
|
* @param template
|
|
51
|
-
* @param loggerConfig
|
|
52
50
|
* @param params
|
|
53
51
|
*/
|
|
54
|
-
static formatMessage(template,
|
|
52
|
+
static formatMessage(template, params) {
|
|
55
53
|
return template.replace(/\{(\w+)\}/g, (_, key) => {
|
|
56
54
|
const value = params[key];
|
|
57
55
|
if (Array.isArray(value)) {
|
|
58
56
|
return value.join(", ");
|
|
59
57
|
}
|
|
60
|
-
|
|
61
|
-
loggerConfig.info("Formatting message", `Replacing {${key}} with: ${formattedValue}`);
|
|
62
|
-
return formattedValue;
|
|
58
|
+
return value !== void 0 ? value.toString() : `{${key}}`;
|
|
63
59
|
});
|
|
64
60
|
}
|
|
65
61
|
};
|
|
66
|
-
TranslationLoader.translations = {};
|
|
67
62
|
export {
|
|
68
63
|
TranslationLoader
|
|
69
64
|
};
|
package/example/index.ts
CHANGED
|
@@ -2,9 +2,7 @@ import {TranslationLoader} from "../src";
|
|
|
2
2
|
import path from "path";
|
|
3
3
|
|
|
4
4
|
((): void => {
|
|
5
|
-
TranslationLoader.loadTranslations(path.join('src', 'utils', 'translations')
|
|
6
|
-
//const msg: string = TranslationLoader.t("mongoServerError", "en");
|
|
7
|
-
|
|
5
|
+
TranslationLoader.loadTranslations(path.join('src', 'utils', 'translations'));
|
|
8
6
|
console.log("path is :", path.join(process.cwd(), 'src', 'utils', 'translations'));
|
|
9
7
|
console.log("mongoServerError is :", TranslationLoader.t("mongoServerError", "en", { dbHost: '127.0.0.1' }));
|
|
10
8
|
})();
|
package/package.json
CHANGED