forked from live-codes/livecodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n-import.mjs
More file actions
215 lines (174 loc) · 6.94 KB
/
Copy pathi18n-import.mjs
File metadata and controls
215 lines (174 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { LokaliseApi } from '@lokalise/node-api';
import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
import prettier from 'prettier';
import { exit } from 'process';
import { autoGeneratedWarning, prettierConfig, sortedJSONify } from './i18n-export.js';
const outDir = path.resolve('src/livecodes/i18n/locales');
const tmpDir = path.join(outDir, 'tmp');
const api = new LokaliseApi({
apiKey: process.env.LOKALISE_API_TOKEN,
});
const projectID = process.env.LOKALISE_PROJECT_ID;
/**
* Expand the flattened translation object (KV pairs) to a nested object.
* @param {string} source Path to the source file.
* @param {Set<string>} validKeys Set of valid keys.
* @returns Record<string, unknown>
*/
const generateTranslationObject = async (source, validKeys) => {
const translations = JSON.parse(await fs.promises.readFile(source, 'utf-8'));
const translationObject = {};
for (const key in translations) {
if (!validKeys.has(key)) {
continue;
}
const keys = key.split('.');
const lastKey = keys.pop();
let currentObject = translationObject;
keys.forEach((k) => {
if (!currentObject[k]) {
currentObject[k] = {};
}
currentObject = currentObject[k];
});
currentObject[lastKey] = translations[key].replace(/tag-/g, '');
}
return translationObject;
};
const importFromLokalise = async () => {
const ciMode = process.env.CI === 'true';
const forceLocalMode = process.argv.slice(2).includes('--force');
const useLocalResourcesMode = process.argv.slice(2).includes('--local');
if (!ciMode && !forceLocalMode) {
console.error('This script is intended to be run in CI mode or with --force flag.');
exit(1);
}
const branchName = process.argv[2];
if (!branchName) {
console.error('Branch name is required');
exit(1);
}
// Make a tmp directory to store the downloaded files
const lokaliseTempDir = path.resolve(process.env.LOKALISE_TEMP);
if (!useLocalResourcesMode) {
console.log('Fetching translations from Lokalise...');
const fullProjectId = `${projectID}:${branchName}`;
const process = await api.files().async_download(fullProjectId, {
format: 'json',
original_filenames: true,
json_unescaped_slashes: true,
replace_breaks: false,
placeholder_format: 'i18n',
});
// Wait until process is finished
const timeout = 60000;
const delay = 2500;
const startTime = Date.now();
/** @type {import("@lokalise/node-api").DownloadedFileProcessDetails} */
let response;
while (true) {
const processInfo = await api
.queuedProcesses()
.get(process.process_id, { project_id: fullProjectId });
if (processInfo.status === 'finished') {
response = processInfo.details;
break;
}
if (Date.now() - startTime > timeout) {
console.error('Timeout exceeded. Aborting...');
exit(1);
}
await new Promise((resolve) => setTimeout(resolve, delay));
}
console.log(`Downloading zip file from ${response.download_url}`);
const zipPath = path.join(lokaliseTempDir, 'locales.zip');
const zipFile = await fetch(response.download_url);
await fs.promises.writeFile(zipPath, Buffer.from(await zipFile.arrayBuffer()));
console.log(`Extracting zip file to ${lokaliseTempDir}...`);
execSync(`unzip -o ${zipPath} -d ${lokaliseTempDir}`);
await fs.promises.unlink(zipPath);
}
const languages = await fs.promises.readdir(lokaliseTempDir);
console.log(
`Extracted languages to tmp directory, ${languages.length} languages (including English) found.`,
);
console.log('Checking if translation keys are outdated...');
/** @type {{[s: string]: {[s: string]: string}}} */
const localTranslation = {};
/** @type {{[s: string]: Set<string>}} */
const lokaliseTranslation = {};
// Read the local translation files as baseline
execSync('npm run i18n-export -- --save-temp', { stdio: 'pipe' });
const localNamespaces = (await fs.promises.readdir(tmpDir)).filter((file) =>
file.endsWith('.lokalise.json'),
);
for (const file of localNamespaces) {
const namespace = file.split('.')[0];
const filePath = path.join(tmpDir, file);
/** @type {{[s: string]: {notes: string, translation: string}}} */
const fileContent = JSON.parse(await fs.promises.readFile(filePath, 'utf-8'));
localTranslation[namespace] = {};
for (const key in fileContent) {
localTranslation[namespace][key] = fileContent[key].translation;
}
}
// Read current source on Lokalise and cherry-pick the keys
const enPath = path.join(lokaliseTempDir, 'en');
const enFiles = await fs.promises.readdir(enPath);
for (const file of enFiles) {
const namespace = file.split('.')[0];
const filePath = path.join(enPath, file);
/** @type {{[s: string]: string}} */
const fileContent = JSON.parse(await fs.promises.readFile(filePath, 'utf-8'));
lokaliseTranslation[namespace] = new Set();
for (const key in fileContent) {
if (!localTranslation[namespace][key]) {
console.warn(
`Skipping: Key ${key} in namespace ${namespace} is missing in local translation.`,
);
continue;
} else if (localTranslation[namespace][key] !== fileContent[key]) {
console.warn(`Skipping: Key ${key} in namespace ${namespace} is outdated.`);
continue;
}
lokaliseTranslation[namespace].add(key);
}
}
for (let language of languages) {
const languagePath = path.join(lokaliseTempDir, language);
if (!(await fs.promises.stat(languagePath)).isDirectory() || language === 'en') {
continue;
}
language = language.replace(/_/g, '-');
const outLanguagePath = path.join(outDir, language);
console.log(`Importing language ${language}...`);
await fs.promises.mkdir(outLanguagePath, { recursive: true });
const files = await fs.promises.readdir(languagePath);
const filePromises = files.map(async (file) => {
const source = path.join(languagePath, file);
const target = path.join(outLanguagePath, file.replace('.lokalise.json', '.ts'));
const namespace = file.split('.')[0];
const name = namespace === 'translation' ? 'translation' : 'languageInfo';
const type = namespace === 'translation' ? 'I18nTranslation' : 'I18nLangInfoTranslation';
const translationObject = await generateTranslationObject(
source,
lokaliseTranslation[namespace],
);
const sortedTranslationObject = sortedJSONify(translationObject);
const code = `${autoGeneratedWarning}
import type { ${type} } from '../models';
const ${name}: ${type} = ${sortedTranslationObject};
export default ${name};
`;
const translationContent = await prettier.format(code, {
parser: 'typescript',
...prettierConfig,
});
return fs.promises.writeFile(target, translationContent);
});
await Promise.all(filePromises);
}
};
importFromLokalise();