@siemens/element-translate-cli 47.1.0-next.1

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.
@@ -0,0 +1,119 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { spawn } from 'child_process';
4
+ import * as fs from 'fs';
5
+ import { dirname } from 'path';
6
+
7
+ import { XMLParser } from 'fast-xml-parser';
8
+
9
+ const configFile = process.argv[2] || './element-translate.conf.json';
10
+ const toolConfig = JSON.parse(fs.readFileSync(configFile));
11
+
12
+ console.log('Running localize-extract');
13
+ const args = ['-s', toolConfig.files, '-f', 'xlf2', '-o', 'messages.xlf'];
14
+ const extract = spawn('localize-extract', args, {
15
+ stdio: ['ignore', 'ignore', 'inherit'],
16
+ shell: true
17
+ });
18
+ extract.on('error', error => console.log(`error: ${error.message}`));
19
+
20
+ extract.on('close', () => {
21
+ processMessages();
22
+ generateFiles();
23
+ fs.rmSync('messages.xlf');
24
+ });
25
+
26
+ function prepareConfigs() {
27
+ for (const config of toolConfig.configs) {
28
+ config.keys = new Set();
29
+ config.translations = {};
30
+ }
31
+ }
32
+
33
+ function getConfigs(notes) {
34
+ if (!Array.isArray(notes)) {
35
+ notes = [notes];
36
+ }
37
+ const configs = [];
38
+ for (const note of notes) {
39
+ if (note['@_category'] !== 'location') {
40
+ continue;
41
+ }
42
+ const location = note['#text'];
43
+
44
+ for (const config of toolConfig.configs) {
45
+ if (location?.startsWith(config.locationPrefix)) {
46
+ configs.push(config);
47
+ }
48
+ }
49
+ }
50
+ return configs;
51
+ }
52
+
53
+ function processMessages() {
54
+ console.log('Completed running localize-extract');
55
+ console.log('Generating translation keys');
56
+
57
+ const parser = new XMLParser({ ignoreAttributes: false });
58
+ const messages = parser.parse(fs.readFileSync('./messages.xlf').toString('utf-8'));
59
+
60
+ prepareConfigs();
61
+
62
+ for (const unit of messages.xliff.file.unit) {
63
+ const key = unit['@_id'];
64
+ const value = unit.segment.source.toString(); // can be number
65
+
66
+ // ignore some bogus output
67
+ if (key.match(/^\d+$/)) {
68
+ continue;
69
+ }
70
+
71
+ const configs = getConfigs(unit.notes.note);
72
+ for (const config of configs) {
73
+ config.keys.add(key);
74
+ config.translations[key] = value;
75
+ }
76
+ }
77
+ }
78
+
79
+ function generateFiles() {
80
+ for (const config of toolConfig.configs) {
81
+ // only update if there are keys found, i.e. don't overwrite when not all libs are build
82
+ if (!config.keys.size) {
83
+ console.log(`Skipping ${config.name} since no keys found`);
84
+ continue;
85
+ }
86
+
87
+ const keys = Array.from(config.keys);
88
+ keys.sort();
89
+
90
+ // for objects, keys are iterated in order they're added, so...
91
+ const tmp = config.translations;
92
+ config.translations = {};
93
+ for (const key of keys) {
94
+ config.translations[key] = tmp[key];
95
+ }
96
+
97
+ const interfaceLine = keys.map(key => ` '${key}'?: string;`);
98
+ const interfaceFull = `/* eslint-disable */
99
+
100
+ // Auto-generated file. Run 'yarn update-translatable-keys' to update.
101
+
102
+ export interface ${config.keysInterfaceName} {
103
+ ${interfaceLine.join('\n')}
104
+ }
105
+ `;
106
+
107
+ fs.mkdirSync(dirname(config.keysFile), { recursive: true });
108
+ fs.writeFileSync(config.keysFile, interfaceFull);
109
+
110
+ fs.mkdirSync(dirname(config.messagesFile), { recursive: true });
111
+ fs.writeFileSync(config.messagesFile, JSON.stringify(config.translations, null, 2));
112
+
113
+ console.log(`Completed generating code for ${config.name}:`);
114
+ console.log(` - ${config.keysFile}`);
115
+ console.log(` - ${config.messagesFile}`);
116
+ }
117
+
118
+ console.log('Completed generating translation keys');
119
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@siemens/element-translate-cli",
3
+ "description": "Element translation abstraction layer CLI.",
4
+ "version": "47.1.0-next.1",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git@github.com:siemens/element.git"
10
+ },
11
+ "author": {
12
+ "name": "Siemens AG",
13
+ "email": "info@siemens.com"
14
+ },
15
+ "homepage": "https://github.com/siemens/element",
16
+ "bugs": "https://github.com/siemens/element/issues/new",
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "bin": {
21
+ "update-translatable-keys": "bin/update-translatable-keys.js"
22
+ },
23
+ "dependencies": {
24
+ "fast-xml-parser": "4.5.0"
25
+ }
26
+ }