opticore-translator 1.0.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.cjs +136 -0
- package/dist/index.d.cts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +99 -0
- package/package.json +39 -0
- package/tsconfig.json +26 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
TranslationLoader: () => TranslationLoader
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
|
|
37
|
+
// src/core/loaders/translation.loader.ts
|
|
38
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
39
|
+
var import_path = __toESM(require("path"), 1);
|
|
40
|
+
var import_opticore_logger = require("opticore-logger");
|
|
41
|
+
var import_opticore_http_response = require("opticore-http-response");
|
|
42
|
+
|
|
43
|
+
// src/core/config/logger/logger.config.ts
|
|
44
|
+
var import_opticore_env_access = require("opticore-env-access");
|
|
45
|
+
var loggerConfig = {
|
|
46
|
+
logLevels: [
|
|
47
|
+
import_opticore_env_access.getEnvVariable.logLevelInfo,
|
|
48
|
+
import_opticore_env_access.getEnvVariable.logLevelWarning,
|
|
49
|
+
import_opticore_env_access.getEnvVariable.logLevelSuccess,
|
|
50
|
+
import_opticore_env_access.getEnvVariable.logLevelError,
|
|
51
|
+
import_opticore_env_access.getEnvVariable.logLevelDebug
|
|
52
|
+
],
|
|
53
|
+
transports: {
|
|
54
|
+
file: {
|
|
55
|
+
enabled: import_opticore_env_access.getEnvVariable.logFileEnabled,
|
|
56
|
+
maxSizeMB: import_opticore_env_access.getEnvVariable.logFileMaxSize,
|
|
57
|
+
rotate: import_opticore_env_access.getEnvVariable.logFileRotate
|
|
58
|
+
},
|
|
59
|
+
console: {
|
|
60
|
+
enabled: import_opticore_env_access.getEnvVariable.logConsoleEnabled
|
|
61
|
+
},
|
|
62
|
+
remote: {
|
|
63
|
+
enabled: import_opticore_env_access.getEnvVariable.logRemoteEnabled,
|
|
64
|
+
endpoint: import_opticore_env_access.getEnvVariable.logRemoteEndPoint
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// src/domains/constants/local/translateLanguage.local.ts
|
|
70
|
+
var import_opticore_env_access2 = require("opticore-env-access");
|
|
71
|
+
var CTranslateLanguageLocal = import_opticore_env_access2.getEnvVariable.defaultLocal;
|
|
72
|
+
|
|
73
|
+
// src/domains/constants/local.constant.ts
|
|
74
|
+
var CLocal = CTranslateLanguageLocal;
|
|
75
|
+
|
|
76
|
+
// src/core/loaders/translation.loader.ts
|
|
77
|
+
var TranslationLoader = class {
|
|
78
|
+
/**
|
|
79
|
+
*
|
|
80
|
+
* @param directory
|
|
81
|
+
* @param defaultLocale
|
|
82
|
+
*/
|
|
83
|
+
static loadTranslations(directory, defaultLocale = CLocal) {
|
|
84
|
+
this.defaultLocale = defaultLocale;
|
|
85
|
+
const files = import_fs.default.readdirSync(directory);
|
|
86
|
+
files.forEach((file) => {
|
|
87
|
+
if (file.endsWith(".json")) {
|
|
88
|
+
const locale = import_path.default.basename(file, ".json");
|
|
89
|
+
const filePath = import_path.default.join(directory, file);
|
|
90
|
+
try {
|
|
91
|
+
this.translations[locale] = JSON.parse(import_fs.default.readFileSync(filePath, "utf-8"));
|
|
92
|
+
this.logger.success("Translation Loaded", `Translations loaded for locale: ${locale}`);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
this.logger.error(error.message, `Error loading translations for ${locale}`, "Translation error", error.stack, import_opticore_http_response.HttpStatusCode.INTERNAL_SERVER_ERROR);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
*
|
|
101
|
+
* @param template
|
|
102
|
+
* @param params
|
|
103
|
+
*/
|
|
104
|
+
static formatMessage(template, params) {
|
|
105
|
+
return template.replace(/\{(\w+)\}/g, (_, key) => {
|
|
106
|
+
const value = params[key];
|
|
107
|
+
if (Array.isArray(value)) {
|
|
108
|
+
return value.join(", ");
|
|
109
|
+
}
|
|
110
|
+
const formattedValue = value !== void 0 ? value.toString() : `{${key}}`;
|
|
111
|
+
this.logger.info("Formatting message", `Replacing {${key}} with: ${formattedValue}`);
|
|
112
|
+
return formattedValue;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
*
|
|
117
|
+
* @param key
|
|
118
|
+
* @param locale
|
|
119
|
+
* @param params
|
|
120
|
+
*
|
|
121
|
+
* using example :
|
|
122
|
+
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
123
|
+
*/
|
|
124
|
+
static t(key, locale = this.defaultLocale, params = {}) {
|
|
125
|
+
var _a, _b;
|
|
126
|
+
const message = ((_a = this.translations[locale]) == null ? void 0 : _a[key]) || ((_b = this.translations[this.defaultLocale]) == null ? void 0 : _b[key]) || key;
|
|
127
|
+
return this.formatMessage(message, params);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
TranslationLoader.translations = {};
|
|
131
|
+
TranslationLoader.defaultLocale = CLocal;
|
|
132
|
+
TranslationLoader.logger = new import_opticore_logger.LoggerCore(loggerConfig);
|
|
133
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
134
|
+
0 && (module.exports = {
|
|
135
|
+
TranslationLoader
|
|
136
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
declare class TranslationLoader {
|
|
2
|
+
private static translations;
|
|
3
|
+
private static defaultLocale;
|
|
4
|
+
private static logger;
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param directory
|
|
8
|
+
* @param defaultLocale
|
|
9
|
+
*/
|
|
10
|
+
static loadTranslations(directory: string, defaultLocale?: string): void;
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* @param template
|
|
14
|
+
* @param params
|
|
15
|
+
*/
|
|
16
|
+
static formatMessage(template: string, params: Record<string, any>): string;
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* @param key
|
|
20
|
+
* @param locale
|
|
21
|
+
* @param params
|
|
22
|
+
*
|
|
23
|
+
* using example :
|
|
24
|
+
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
25
|
+
*/
|
|
26
|
+
static t(key: string, locale?: string, params?: Record<string, string>): string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { TranslationLoader };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
declare class TranslationLoader {
|
|
2
|
+
private static translations;
|
|
3
|
+
private static defaultLocale;
|
|
4
|
+
private static logger;
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param directory
|
|
8
|
+
* @param defaultLocale
|
|
9
|
+
*/
|
|
10
|
+
static loadTranslations(directory: string, defaultLocale?: string): void;
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* @param template
|
|
14
|
+
* @param params
|
|
15
|
+
*/
|
|
16
|
+
static formatMessage(template: string, params: Record<string, any>): string;
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* @param key
|
|
20
|
+
* @param locale
|
|
21
|
+
* @param params
|
|
22
|
+
*
|
|
23
|
+
* using example :
|
|
24
|
+
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
25
|
+
*/
|
|
26
|
+
static t(key: string, locale?: string, params?: Record<string, string>): string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { TranslationLoader };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// src/core/loaders/translation.loader.ts
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { LoggerCore } from "opticore-logger";
|
|
5
|
+
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
|
+
var TranslationLoader = class {
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* @param directory
|
|
45
|
+
* @param defaultLocale
|
|
46
|
+
*/
|
|
47
|
+
static loadTranslations(directory, defaultLocale = CLocal) {
|
|
48
|
+
this.defaultLocale = defaultLocale;
|
|
49
|
+
const files = fs.readdirSync(directory);
|
|
50
|
+
files.forEach((file) => {
|
|
51
|
+
if (file.endsWith(".json")) {
|
|
52
|
+
const locale = path.basename(file, ".json");
|
|
53
|
+
const filePath = path.join(directory, file);
|
|
54
|
+
try {
|
|
55
|
+
this.translations[locale] = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
56
|
+
this.logger.success("Translation Loaded", `Translations loaded for locale: ${locale}`);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
this.logger.error(error.message, `Error loading translations for ${locale}`, "Translation error", error.stack, HttpStatusCode.INTERNAL_SERVER_ERROR);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
*
|
|
65
|
+
* @param template
|
|
66
|
+
* @param params
|
|
67
|
+
*/
|
|
68
|
+
static formatMessage(template, params) {
|
|
69
|
+
return template.replace(/\{(\w+)\}/g, (_, key) => {
|
|
70
|
+
const value = params[key];
|
|
71
|
+
if (Array.isArray(value)) {
|
|
72
|
+
return value.join(", ");
|
|
73
|
+
}
|
|
74
|
+
const formattedValue = value !== void 0 ? value.toString() : `{${key}}`;
|
|
75
|
+
this.logger.info("Formatting message", `Replacing {${key}} with: ${formattedValue}`);
|
|
76
|
+
return formattedValue;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
*
|
|
81
|
+
* @param key
|
|
82
|
+
* @param locale
|
|
83
|
+
* @param params
|
|
84
|
+
*
|
|
85
|
+
* using example :
|
|
86
|
+
* const errorMessage = TranslationLoader.t('mongoServerError', 'en', { dbHost: '127.0.0.1' });
|
|
87
|
+
*/
|
|
88
|
+
static t(key, locale = this.defaultLocale, params = {}) {
|
|
89
|
+
var _a, _b;
|
|
90
|
+
const message = ((_a = this.translations[locale]) == null ? void 0 : _a[key]) || ((_b = this.translations[this.defaultLocale]) == null ? void 0 : _b[key]) || key;
|
|
91
|
+
return this.formatMessage(message, params);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
TranslationLoader.translations = {};
|
|
95
|
+
TranslationLoader.defaultLocale = CLocal;
|
|
96
|
+
TranslationLoader.logger = new LoggerCore(loggerConfig);
|
|
97
|
+
export {
|
|
98
|
+
TranslationLoader
|
|
99
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "opticore-translator",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "opticore translator to translate according language",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
11
|
+
"build": "tsup"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/guyzoum77/opticore-translator.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"opticore"
|
|
19
|
+
],
|
|
20
|
+
"author": "Guy-serge Kouacou",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/guyzoum77/opticore-translator/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/guyzoum77/opticore-translator#readme",
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"ansi-colors": "^4.1.3",
|
|
28
|
+
"chalk": "^5.4.1",
|
|
29
|
+
"opticore-env-access": "^1.0.2",
|
|
30
|
+
"opticore-http-response": "^1.0.3",
|
|
31
|
+
"opticore-logger": "^1.0.11"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/node": "^22.13.5",
|
|
35
|
+
"tslib": "^2.8.1",
|
|
36
|
+
"tsup": "^8.4.0",
|
|
37
|
+
"typescript": "^5.4.5"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"strict": true,
|
|
4
|
+
"noImplicitAny": true,
|
|
5
|
+
"esModuleInterop": true,
|
|
6
|
+
"strictNullChecks": true,
|
|
7
|
+
"module": "commonjs",
|
|
8
|
+
"pretty": true,
|
|
9
|
+
"target": "es2017",
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
"declaration": true,
|
|
12
|
+
"isolatedModules": true,
|
|
13
|
+
"noEmit": false,
|
|
14
|
+
"types": ["reflect-metadata", "node"],
|
|
15
|
+
"experimentalDecorators": true,
|
|
16
|
+
"outDir": "./dist",
|
|
17
|
+
"importHelpers": true,
|
|
18
|
+
"resolveJsonModule": true,
|
|
19
|
+
"baseUrl": "src",
|
|
20
|
+
"paths": {
|
|
21
|
+
"@translator/*": ["*"],
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"include": ["src/**/*.ts", "src/**/*.json", ".env"],
|
|
25
|
+
"exclude": ["node_modules"]
|
|
26
|
+
}
|