@deriv-com/translations 1.0.1 → 1.1.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.
@@ -5,7 +5,28 @@ const path = require("path");
5
5
  const crc32 = require("crc-32").str;
6
6
  const fs = require("fs");
7
7
  const glob = require("glob");
8
- const DOMParser = require("@xmldom/xmldom").DOMParser;
8
+ const { DOMParser } = require("@xmldom/xmldom");
9
+ const { Command } = require("commander");
10
+
11
+ const program = new Command();
12
+
13
+ let SOURCE_DIRECTORY = 'src'
14
+
15
+ program
16
+ .version("0.1.0")
17
+ .description("Build translation source.")
18
+ .option("-v, --verbose", "Displays the list of paths to be compiled and shows all the message and it's hash.")
19
+ .argument('<srcDir>', "source directory path i.e './src' which is by default './src'")
20
+ .description('source directory of the app from where the script will start finding for strings.')
21
+ .action((str) => {
22
+ SOURCE_DIRECTORY = str.replace(/^\.*\/+|\/+$/g, '');
23
+ });
24
+
25
+
26
+ program.parse(process.argv);
27
+
28
+ const options = program.opts();
29
+
9
30
 
10
31
  const getRegexPattern = () =>
11
32
  /(i18n_default_text={?|localize\()\s*(['"])\s*(.*?)(?<!\\)\2\s*/gs;
@@ -41,27 +62,14 @@ const getStringsFromXmlFile = (input) => {
41
62
  };
42
63
 
43
64
  const getTranslatableFiles = () => {
44
- const packages_with_translations = [
45
- "account",
46
- "appstore",
47
- "cashier",
48
- "bot-web-ui",
49
- "core",
50
- "cfd",
51
- "trader",
52
- "bot-skeleton",
53
- "reports",
54
- "shared",
55
- ];
56
65
  const globs = ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx", "**/xml/*.xml"];
57
66
  const file_paths = [];
58
67
 
59
- for (let i = 0; i < packages_with_translations.length; i++) {
60
68
  for (let j = 0; j < globs.length; j++) {
61
69
  let files_found = glob.sync(
62
- `/${packages_with_translations[i]}/src/${globs[j]}`,
70
+ `./${SOURCE_DIRECTORY}/${globs[j]}`,
63
71
  {
64
- root: path.resolve(__dirname, "../.."), // deriv-app/packages/
72
+ root: path.resolve("."),
65
73
  }
66
74
  );
67
75
  files_found = files_found.filter(
@@ -69,7 +77,6 @@ const getTranslatableFiles = () => {
69
77
  );
70
78
  file_paths.push(...files_found);
71
79
  }
72
- }
73
80
 
74
81
  return file_paths;
75
82
  };
@@ -92,6 +99,11 @@ const getKeyHash = (string) => crc32(string);
92
99
  for (let i = 0; i < file_paths.length; i++) {
93
100
  const file_path = file_paths[i];
94
101
 
102
+ if(options.verbose) {
103
+ console.log(file_path);
104
+ }
105
+
106
+
95
107
  try {
96
108
  const file = fs.readFileSync(file_path, "utf8");
97
109
  messages.push(
@@ -106,17 +118,32 @@ const getKeyHash = (string) => crc32(string);
106
118
 
107
119
  // Hash the messages and set the key-value pair for json
108
120
  for (let i = 0; i < messages.length; i++) {
109
- messages_json[getKeyHash(messages[i])] = messages[i];
121
+ const keyHash = getKeyHash(messages[i])
122
+ messages_json[keyHash] = messages[i];
123
+
124
+
125
+ if(options.verbose) {
126
+ console.log('** key_hash : ', keyHash, ' ** message : ',messages[i]);
127
+ }
128
+ }
129
+
130
+ // check if the directory of crowdin exist, if not create the directory of the crowdin first in the root
131
+ const crowdinDir = path.resolve("./crowdin");
132
+
133
+ if (!fs.existsSync(crowdinDir)) {
134
+ fs.mkdirSync(crowdinDir)
110
135
  }
111
136
 
112
137
  // Add to messages.json
113
138
  fs.writeFileSync(
114
- path.resolve(__dirname, "../crowdin/messages.json"),
139
+ path.resolve("./crowdin/messages.json"),
115
140
  JSON.stringify(messages_json),
116
141
  "utf8",
117
142
  (err) => console.log(err)
118
143
  );
144
+
145
+ console.log("********* Translation strings compiled successfully *********")
119
146
  } catch (e) {
120
- console.error(e);
147
+ program.error(e);
121
148
  }
122
149
  })();
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  }
17
17
  },
18
18
  "private": false,
19
- "version": "1.0.1",
19
+ "version": "1.1.0",
20
20
  "scripts": {
21
21
  "dev": "vite",
22
22
  "build": "tsc && vite build && cp ./src/scripts/extract-translations.cjs ./dist/extract-translations.cjs",
@@ -42,13 +42,9 @@
42
42
  "@typescript-eslint/eslint-plugin": "^6.14.0",
43
43
  "@typescript-eslint/parser": "^6.14.0",
44
44
  "@vitejs/plugin-react": "^4.2.1",
45
- "@xmldom/xmldom": "^0.8.10",
46
- "commander": "^12.0.0",
47
- "crc-32": "^1.2.2",
48
45
  "eslint": "^8.55.0",
49
46
  "eslint-plugin-react-hooks": "^4.6.0",
50
47
  "eslint-plugin-react-refresh": "^0.4.5",
51
- "glob": "^10.3.12",
52
48
  "i18next": "^23.7.18",
53
49
  "jsdom": "^24.0.0",
54
50
  "react": "^18.2.0",
@@ -62,5 +58,11 @@
62
58
  },
63
59
  "optionalDependencies": {
64
60
  "@rollup/rollup-linux-x64-gnu": "^4.17.1"
61
+ },
62
+ "dependencies": {
63
+ "@xmldom/xmldom": "^0.8.10",
64
+ "commander": "^12.0.0",
65
+ "crc-32": "^1.2.2",
66
+ "glob": "^10.3.12"
65
67
  }
66
68
  }