opticore-translator 1.0.2 → 1.0.4
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 +23 -61
- package/dist/index.d.cts +14 -14
- package/dist/index.d.ts +14 -14
- package/dist/index.js +23 -60
- package/example/index.ts +10 -0
- package/package.json +3 -2
package/dist/index.cjs
CHANGED
|
@@ -30,7 +30,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
CLocal: () => CLocal,
|
|
34
33
|
TranslationLoader: () => TranslationLoader
|
|
35
34
|
});
|
|
36
35
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -38,51 +37,14 @@ module.exports = __toCommonJS(index_exports);
|
|
|
38
37
|
// src/core/loaders/translation.loader.ts
|
|
39
38
|
var import_fs = __toESM(require("fs"), 1);
|
|
40
39
|
var import_path = __toESM(require("path"), 1);
|
|
41
|
-
var import_opticore_logger = require("opticore-logger");
|
|
42
40
|
var import_opticore_http_response = require("opticore-http-response");
|
|
43
|
-
|
|
44
|
-
// src/core/config/logger/logger.config.ts
|
|
45
|
-
var import_opticore_env_access = require("opticore-env-access");
|
|
46
|
-
var loggerConfig = {
|
|
47
|
-
logLevels: [
|
|
48
|
-
import_opticore_env_access.getEnvVariable.logLevelInfo,
|
|
49
|
-
import_opticore_env_access.getEnvVariable.logLevelWarning,
|
|
50
|
-
import_opticore_env_access.getEnvVariable.logLevelSuccess,
|
|
51
|
-
import_opticore_env_access.getEnvVariable.logLevelError,
|
|
52
|
-
import_opticore_env_access.getEnvVariable.logLevelDebug
|
|
53
|
-
],
|
|
54
|
-
transports: {
|
|
55
|
-
file: {
|
|
56
|
-
enabled: import_opticore_env_access.getEnvVariable.logFileEnabled,
|
|
57
|
-
maxSizeMB: import_opticore_env_access.getEnvVariable.logFileMaxSize,
|
|
58
|
-
rotate: import_opticore_env_access.getEnvVariable.logFileRotate
|
|
59
|
-
},
|
|
60
|
-
console: {
|
|
61
|
-
enabled: import_opticore_env_access.getEnvVariable.logConsoleEnabled
|
|
62
|
-
},
|
|
63
|
-
remote: {
|
|
64
|
-
enabled: import_opticore_env_access.getEnvVariable.logRemoteEnabled,
|
|
65
|
-
endpoint: import_opticore_env_access.getEnvVariable.logRemoteEndPoint
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
// src/domains/constants/local/translateLanguage.local.ts
|
|
71
|
-
var import_opticore_env_access2 = require("opticore-env-access");
|
|
72
|
-
var CTranslateLanguageLocal = import_opticore_env_access2.getEnvVariable.defaultLocal;
|
|
73
|
-
|
|
74
|
-
// src/domains/constants/local.constant.ts
|
|
75
|
-
var CLocal = CTranslateLanguageLocal;
|
|
76
|
-
|
|
77
|
-
// src/core/loaders/translation.loader.ts
|
|
78
41
|
var TranslationLoader = class {
|
|
79
42
|
/**
|
|
80
43
|
*
|
|
81
44
|
* @param directory
|
|
82
|
-
* @param
|
|
45
|
+
* @param loggerConfig
|
|
83
46
|
*/
|
|
84
|
-
static loadTranslations(directory,
|
|
85
|
-
this.defaultLocale = defaultLocale;
|
|
47
|
+
static loadTranslations(directory, loggerConfig) {
|
|
86
48
|
const files = import_fs.default.readdirSync(directory);
|
|
87
49
|
files.forEach((file) => {
|
|
88
50
|
if (file.endsWith(".json")) {
|
|
@@ -90,9 +52,9 @@ var TranslationLoader = class {
|
|
|
90
52
|
const filePath = import_path.default.join(directory, file);
|
|
91
53
|
try {
|
|
92
54
|
this.translations[locale] = JSON.parse(import_fs.default.readFileSync(filePath, "utf-8"));
|
|
93
|
-
|
|
55
|
+
loggerConfig.success("Translation Loaded", `Translations loaded for locale: ${locale}`);
|
|
94
56
|
} catch (error) {
|
|
95
|
-
|
|
57
|
+
loggerConfig.error(
|
|
96
58
|
error.message,
|
|
97
59
|
`Error loading translations for ${locale}`,
|
|
98
60
|
"Translation error",
|
|
@@ -103,42 +65,42 @@ var TranslationLoader = class {
|
|
|
103
65
|
}
|
|
104
66
|
});
|
|
105
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
*
|
|
70
|
+
* @param key
|
|
71
|
+
* @param locale
|
|
72
|
+
* @param loggerConfig
|
|
73
|
+
* @param params
|
|
74
|
+
*
|
|
75
|
+
* using example :
|
|
76
|
+
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
77
|
+
*/
|
|
78
|
+
static t(key, locale, loggerConfig, params = {}) {
|
|
79
|
+
var _a;
|
|
80
|
+
const localeKey = `message.translation.${locale}`;
|
|
81
|
+
const message = ((_a = this.translations[localeKey]) == null ? void 0 : _a[key]) || key;
|
|
82
|
+
return this.formatMessage(message, loggerConfig, params);
|
|
83
|
+
}
|
|
106
84
|
/**
|
|
107
85
|
*
|
|
108
86
|
* @param template
|
|
87
|
+
* @param loggerConfig
|
|
109
88
|
* @param params
|
|
110
89
|
*/
|
|
111
|
-
static formatMessage(template, params) {
|
|
90
|
+
static formatMessage(template, loggerConfig, params) {
|
|
112
91
|
return template.replace(/\{(\w+)\}/g, (_, key) => {
|
|
113
92
|
const value = params[key];
|
|
114
93
|
if (Array.isArray(value)) {
|
|
115
94
|
return value.join(", ");
|
|
116
95
|
}
|
|
117
96
|
const formattedValue = value !== void 0 ? value.toString() : `{${key}}`;
|
|
118
|
-
|
|
97
|
+
loggerConfig.info("Formatting message", `Replacing {${key}} with: ${formattedValue}`);
|
|
119
98
|
return formattedValue;
|
|
120
99
|
});
|
|
121
100
|
}
|
|
122
|
-
/**
|
|
123
|
-
*
|
|
124
|
-
* @param key
|
|
125
|
-
* @param locale
|
|
126
|
-
* @param params
|
|
127
|
-
*
|
|
128
|
-
* using example :
|
|
129
|
-
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
130
|
-
*/
|
|
131
|
-
static t(key, locale = this.defaultLocale, params = {}) {
|
|
132
|
-
var _a, _b;
|
|
133
|
-
const message = ((_a = this.translations[locale]) == null ? void 0 : _a[key]) || ((_b = this.translations[this.defaultLocale]) == null ? void 0 : _b[key]) || key;
|
|
134
|
-
return this.formatMessage(message, params);
|
|
135
|
-
}
|
|
136
101
|
};
|
|
137
102
|
TranslationLoader.translations = {};
|
|
138
|
-
TranslationLoader.defaultLocale = CLocal;
|
|
139
|
-
TranslationLoader.logger = new import_opticore_logger.LoggerCore(loggerConfig);
|
|
140
103
|
// Annotate the CommonJS export names for ESM import in node:
|
|
141
104
|
0 && (module.exports = {
|
|
142
|
-
CLocal,
|
|
143
105
|
TranslationLoader
|
|
144
106
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
+
import { LoggerCore } from 'opticore-logger';
|
|
2
|
+
|
|
1
3
|
declare class TranslationLoader {
|
|
2
4
|
private static translations;
|
|
3
|
-
private static defaultLocale;
|
|
4
|
-
private static logger;
|
|
5
5
|
/**
|
|
6
6
|
*
|
|
7
7
|
* @param directory
|
|
8
|
-
* @param
|
|
9
|
-
*/
|
|
10
|
-
static loadTranslations(directory: string, defaultLocale?: string): void;
|
|
11
|
-
/**
|
|
12
|
-
*
|
|
13
|
-
* @param template
|
|
14
|
-
* @param params
|
|
8
|
+
* @param loggerConfig
|
|
15
9
|
*/
|
|
16
|
-
static
|
|
10
|
+
static loadTranslations(directory: string, loggerConfig: LoggerCore): void;
|
|
17
11
|
/**
|
|
18
12
|
*
|
|
19
13
|
* @param key
|
|
20
14
|
* @param locale
|
|
15
|
+
* @param loggerConfig
|
|
21
16
|
* @param params
|
|
22
17
|
*
|
|
23
18
|
* using example :
|
|
24
19
|
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
25
20
|
*/
|
|
26
|
-
static t(key: string, locale
|
|
21
|
+
static t(key: string, locale: string, loggerConfig: LoggerCore, params?: Record<any, any>): string;
|
|
22
|
+
/**
|
|
23
|
+
*
|
|
24
|
+
* @param template
|
|
25
|
+
* @param loggerConfig
|
|
26
|
+
* @param params
|
|
27
|
+
*/
|
|
28
|
+
private static formatMessage;
|
|
27
29
|
}
|
|
28
30
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
export { CLocal, TranslationLoader };
|
|
31
|
+
export { TranslationLoader };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
+
import { LoggerCore } from 'opticore-logger';
|
|
2
|
+
|
|
1
3
|
declare class TranslationLoader {
|
|
2
4
|
private static translations;
|
|
3
|
-
private static defaultLocale;
|
|
4
|
-
private static logger;
|
|
5
5
|
/**
|
|
6
6
|
*
|
|
7
7
|
* @param directory
|
|
8
|
-
* @param
|
|
9
|
-
*/
|
|
10
|
-
static loadTranslations(directory: string, defaultLocale?: string): void;
|
|
11
|
-
/**
|
|
12
|
-
*
|
|
13
|
-
* @param template
|
|
14
|
-
* @param params
|
|
8
|
+
* @param loggerConfig
|
|
15
9
|
*/
|
|
16
|
-
static
|
|
10
|
+
static loadTranslations(directory: string, loggerConfig: LoggerCore): void;
|
|
17
11
|
/**
|
|
18
12
|
*
|
|
19
13
|
* @param key
|
|
20
14
|
* @param locale
|
|
15
|
+
* @param loggerConfig
|
|
21
16
|
* @param params
|
|
22
17
|
*
|
|
23
18
|
* using example :
|
|
24
19
|
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
25
20
|
*/
|
|
26
|
-
static t(key: string, locale
|
|
21
|
+
static t(key: string, locale: string, loggerConfig: LoggerCore, params?: Record<any, any>): string;
|
|
22
|
+
/**
|
|
23
|
+
*
|
|
24
|
+
* @param template
|
|
25
|
+
* @param loggerConfig
|
|
26
|
+
* @param params
|
|
27
|
+
*/
|
|
28
|
+
private static formatMessage;
|
|
27
29
|
}
|
|
28
30
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
export { CLocal, TranslationLoader };
|
|
31
|
+
export { TranslationLoader };
|
package/dist/index.js
CHANGED
|
@@ -1,51 +1,14 @@
|
|
|
1
1
|
// src/core/loaders/translation.loader.ts
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import path from "path";
|
|
4
|
-
import { LoggerCore } from "opticore-logger";
|
|
5
4
|
import { HttpStatusCode } from "opticore-http-response";
|
|
6
|
-
|
|
7
|
-
// src/core/config/logger/logger.config.ts
|
|
8
|
-
import { getEnvVariable } from "opticore-env-access";
|
|
9
|
-
var loggerConfig = {
|
|
10
|
-
logLevels: [
|
|
11
|
-
getEnvVariable.logLevelInfo,
|
|
12
|
-
getEnvVariable.logLevelWarning,
|
|
13
|
-
getEnvVariable.logLevelSuccess,
|
|
14
|
-
getEnvVariable.logLevelError,
|
|
15
|
-
getEnvVariable.logLevelDebug
|
|
16
|
-
],
|
|
17
|
-
transports: {
|
|
18
|
-
file: {
|
|
19
|
-
enabled: getEnvVariable.logFileEnabled,
|
|
20
|
-
maxSizeMB: getEnvVariable.logFileMaxSize,
|
|
21
|
-
rotate: getEnvVariable.logFileRotate
|
|
22
|
-
},
|
|
23
|
-
console: {
|
|
24
|
-
enabled: getEnvVariable.logConsoleEnabled
|
|
25
|
-
},
|
|
26
|
-
remote: {
|
|
27
|
-
enabled: getEnvVariable.logRemoteEnabled,
|
|
28
|
-
endpoint: getEnvVariable.logRemoteEndPoint
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
// src/domains/constants/local/translateLanguage.local.ts
|
|
34
|
-
import { getEnvVariable as getEnvVariable2 } from "opticore-env-access";
|
|
35
|
-
var CTranslateLanguageLocal = getEnvVariable2.defaultLocal;
|
|
36
|
-
|
|
37
|
-
// src/domains/constants/local.constant.ts
|
|
38
|
-
var CLocal = CTranslateLanguageLocal;
|
|
39
|
-
|
|
40
|
-
// src/core/loaders/translation.loader.ts
|
|
41
5
|
var TranslationLoader = class {
|
|
42
6
|
/**
|
|
43
7
|
*
|
|
44
8
|
* @param directory
|
|
45
|
-
* @param
|
|
9
|
+
* @param loggerConfig
|
|
46
10
|
*/
|
|
47
|
-
static loadTranslations(directory,
|
|
48
|
-
this.defaultLocale = defaultLocale;
|
|
11
|
+
static loadTranslations(directory, loggerConfig) {
|
|
49
12
|
const files = fs.readdirSync(directory);
|
|
50
13
|
files.forEach((file) => {
|
|
51
14
|
if (file.endsWith(".json")) {
|
|
@@ -53,9 +16,9 @@ var TranslationLoader = class {
|
|
|
53
16
|
const filePath = path.join(directory, file);
|
|
54
17
|
try {
|
|
55
18
|
this.translations[locale] = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
56
|
-
|
|
19
|
+
loggerConfig.success("Translation Loaded", `Translations loaded for locale: ${locale}`);
|
|
57
20
|
} catch (error) {
|
|
58
|
-
|
|
21
|
+
loggerConfig.error(
|
|
59
22
|
error.message,
|
|
60
23
|
`Error loading translations for ${locale}`,
|
|
61
24
|
"Translation error",
|
|
@@ -66,41 +29,41 @@ var TranslationLoader = class {
|
|
|
66
29
|
}
|
|
67
30
|
});
|
|
68
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
*
|
|
34
|
+
* @param key
|
|
35
|
+
* @param locale
|
|
36
|
+
* @param loggerConfig
|
|
37
|
+
* @param params
|
|
38
|
+
*
|
|
39
|
+
* using example :
|
|
40
|
+
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
41
|
+
*/
|
|
42
|
+
static t(key, locale, loggerConfig, params = {}) {
|
|
43
|
+
var _a;
|
|
44
|
+
const localeKey = `message.translation.${locale}`;
|
|
45
|
+
const message = ((_a = this.translations[localeKey]) == null ? void 0 : _a[key]) || key;
|
|
46
|
+
return this.formatMessage(message, loggerConfig, params);
|
|
47
|
+
}
|
|
69
48
|
/**
|
|
70
49
|
*
|
|
71
50
|
* @param template
|
|
51
|
+
* @param loggerConfig
|
|
72
52
|
* @param params
|
|
73
53
|
*/
|
|
74
|
-
static formatMessage(template, params) {
|
|
54
|
+
static formatMessage(template, loggerConfig, params) {
|
|
75
55
|
return template.replace(/\{(\w+)\}/g, (_, key) => {
|
|
76
56
|
const value = params[key];
|
|
77
57
|
if (Array.isArray(value)) {
|
|
78
58
|
return value.join(", ");
|
|
79
59
|
}
|
|
80
60
|
const formattedValue = value !== void 0 ? value.toString() : `{${key}}`;
|
|
81
|
-
|
|
61
|
+
loggerConfig.info("Formatting message", `Replacing {${key}} with: ${formattedValue}`);
|
|
82
62
|
return formattedValue;
|
|
83
63
|
});
|
|
84
64
|
}
|
|
85
|
-
/**
|
|
86
|
-
*
|
|
87
|
-
* @param key
|
|
88
|
-
* @param locale
|
|
89
|
-
* @param params
|
|
90
|
-
*
|
|
91
|
-
* using example :
|
|
92
|
-
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
93
|
-
*/
|
|
94
|
-
static t(key, locale = this.defaultLocale, params = {}) {
|
|
95
|
-
var _a, _b;
|
|
96
|
-
const message = ((_a = this.translations[locale]) == null ? void 0 : _a[key]) || ((_b = this.translations[this.defaultLocale]) == null ? void 0 : _b[key]) || key;
|
|
97
|
-
return this.formatMessage(message, params);
|
|
98
|
-
}
|
|
99
65
|
};
|
|
100
66
|
TranslationLoader.translations = {};
|
|
101
|
-
TranslationLoader.defaultLocale = CLocal;
|
|
102
|
-
TranslationLoader.logger = new LoggerCore(loggerConfig);
|
|
103
67
|
export {
|
|
104
|
-
CLocal,
|
|
105
68
|
TranslationLoader
|
|
106
69
|
};
|
package/example/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import {TranslationLoader} from "../src";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
((): void => {
|
|
5
|
+
TranslationLoader.loadTranslations(path.join('src', 'utils', 'translations'), 'en');
|
|
6
|
+
//const msg: string = TranslationLoader.t("mongoServerError", "en");
|
|
7
|
+
|
|
8
|
+
console.log("path is :", path.join(process.cwd(), 'src', 'utils', 'translations'));
|
|
9
|
+
console.log("mongoServerError is :", TranslationLoader.t("mongoServerError", "en", { dbHost: '127.0.0.1' }));
|
|
10
|
+
})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opticore-translator",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "opticore translator to translate according language",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
"chalk": "^5.4.1",
|
|
30
30
|
"opticore-env-access": "^1.0.2",
|
|
31
31
|
"opticore-http-response": "^1.0.3",
|
|
32
|
-
"opticore-logger": "^1.0.13"
|
|
32
|
+
"opticore-logger": "^1.0.13",
|
|
33
|
+
"tsx": "^4.19.3"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
36
|
"@types/node": "^22.13.5",
|