-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson.js
122 lines (106 loc) · 3.71 KB
/
json.js
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
const fs = require('fs');
const path = require('path');
const { Configuration, OpenAIApi } = require("openai");
const core = require('@actions/core');
const apiKey = core.getInput('openai-token');
const mainLocale = core.getInput('main-locale');
const model = core.getInput('model');
const dir = core.toPlatformPath(core.getInput('locales-path'));
const localeCodes = require('locale-codes');
const verbose = core.getInput('verbose') === 'true';
const dropPluralRegex = /(_one|_two|_few|_many|_other|_zero)$/;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const configuration = new Configuration({
apiKey,
});
const openai = new OpenAIApi(configuration);
const locales = {};
fs.readdirSync(dir).forEach(file => {
const locale = file.split('.')[0];
locales[locale] = JSON.parse(fs.readFileSync(path.join(dir, file), { encoding: 'utf-8' }));
});
function asyncForEach(array, cb) {
return new Promise((resolve, reject) => {
(async function loop() {
for (let index = 0; index < array.length; index++) {
await cb(array[index], index, array);
}
resolve();
})();
});
}
function deepDiff(a, b) {
const diff = {};
Object.entries(a).forEach(([key, value]) => {
if (key.match(dropPluralRegex)) {
return;
}
if (typeof value === 'string') {
if (!b[key]) {
diff[key] = value;
}
} else {
diff[key] = deepDiff(value, b[key] || {});
}
});
return diff;
}
async function getTranslationOfStringFromBackend(string, locale, num_of_retry = 0) {
try {
const prompt = `Your task is to localize app. Your response must contain ONLY translated text without additional notes. Translate the following string to ${localeCodes.getByTag(locale).name}:\n\n${string}\n\n`;
const messages = [
{"role": "user", "content": prompt},
];
const completion = await openai.createChatCompletion({
model,
messages,
max_tokens: 500,
});
return completion.data.choices[0]?.message.content;
} catch (e) {
if (num_of_retry >= 5 || e.response?.status !== 429) {
throw e;
}
if (verbose) {
console.log(e);
}
console.log('Too many requests, waiting 1 minute...');
await sleep(60000);
return await getTranslationOfStringFromBackend(string, locale, num_of_retry + 1);
}
}
function deepCopy(obj) {
return JSON.parse(JSON.stringify(obj));
}
async function deepLocalization(locale, diff, locale_key) {
const loc = deepCopy(locale);
await asyncForEach(Object.entries(diff), async ([key, value]) => {
if (typeof value === 'string') {
loc[key] = await getTranslationOfStringFromBackend(value, locale_key);
} else {
loc[key] = await deepLocalization(loc[key] || {}, value, locale_key);
}
});
return loc;
}
function saveAsJson() {
Object.entries(locales).forEach(([locale, translations]) => {
fs.writeFileSync(path.join(dir, `${locale}.json`), JSON.stringify(translations, null, 2));
});
}
async function localizeRestLanguages() {
await asyncForEach(Object.entries(locales), async ([locale, translations]) => {
if (locale === mainLocale) {
return;
}
console.log(`Localizing ${locale}...`);
const diff = deepDiff(locales[mainLocale], translations);
console.log('detected new strings: ');
console.log(JSON.stringify(diff, null, 2));
locales[locale] = await deepLocalization(locales[locale], diff, locale);
});
saveAsJson();
}
localizeRestLanguages();