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 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.14",
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.16",
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.21.0"
33
+ "tsx": "^4.22.4"
34
34
  },
35
35
  "devDependencies": {
36
- "@types/node": "^22.13.5",
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
+ }
@@ -0,0 +1,5 @@
1
+ export interface ILoaderOptions {
2
+ packageName: string;
3
+ locationTranslationFile: string[];
4
+ localLang: any
5
+ }
@@ -0,0 +1,3 @@
1
+ export interface ITranslations {
2
+ [locale: string]: { [key: string]: string };
3
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { TranslationLoader } from "@translator/core/loaders/translation.loader";
2
+
3
+ export {
4
+ TranslationLoader,
5
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "ENOENT:NoSuchFileOrDir": "ENOENT:NoSuchFileOrDir",
3
+ "readdir": "readdir",
4
+ "mongoServerError": "An Error is occurs of Mongo server"
5
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "ENOENT:NoSuchFileOrDir": "ENOENT:NoSuchFileOrDir",
3
+ "readdir": "readdir"
4
+ }
package/tsconfig.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "module": "ESNext",
8
8
  "pretty": true,
9
9
  "target": "ESNext",
10
- "moduleResolution": "node",
10
+ "moduleResolution": "node10",
11
11
  "declaration": true,
12
12
  "isolatedModules": true,
13
13
  "noEmit": false,
package/tsup.config.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ name: "opticore-translator",
5
+ format: ["cjs", "esm"],
6
+ entry: ['src/index.ts'] ,
7
+ dts: true,
8
+ shims: true,
9
+ skipNodeModulesBundle: true,
10
+ clean: true
11
+ });