forked from TurboWarp/scratch-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_tx_translations.js
executable file
·111 lines (93 loc) · 3.57 KB
/
sync_tx_translations.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
#!/usr/bin/env node
/**
* @fileoverview
* Script to pull translations for blocks from transifex and generate the scratch_msgs file.
* Expects that the project and resource have already been defined in Transifex, and that
* the person running the script has the the TX_TOKEN environment variable set to an api
* token that has developer access.
*/
const usage = `
Pull supported language translations from Transifex. Usage:
node sync_tx_translations.js
NOTE: TX_TOKEN environment variable needs to be set with a Transifex API token. See
the Localization page on the GUI wiki for information about setting up Transifex.
`;
// Fail immediately if the TX_TOKEN is not defined
if (!process.env.TX_TOKEN || process.argv.length !== 2) {
process.stdout.write(usage);
process.exit(1);
};
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const locales = require('scratch-l10n').default;
const {txPull} = require('scratch-l10n/lib/transifex.js');
// Globals
const PATH_OUTPUT = path.resolve(__dirname, '../msg');
const PROJECT = 'scratch-editor'
const RESOURCE = 'blocks';
const MODE = 'reviewed';
let en = fs.readFileSync(path.resolve(__dirname, '../msg/json/en.json'));
en = JSON.parse(en);
const enKeys = Object.keys(en).sort().toString();
// Check that translation is valid:
// entry: array [key, translation] corresponding to a single string from <locale>.json
// - messages with placeholders have the same number of placeholders
// - messages must not have newlines embedded
const validateEntry = function (entry) {
const re = /(%\d)/g;
const [key, translation] = entry;
const enMatch = en[key].match(re);
const tMatch = translation.match(re);
const enCount = enMatch ? enMatch.length : 0;
const tCount = tMatch ? tMatch.length : 0;
assert.strictEqual(tCount, enCount, `${key}:${en[key]} - "${translation}" placeholder mismatch`);
if (enCount > 0) {
assert.notStrictEqual(tMatch, null, `${key} is missing a placeholder: ${translation}`);
assert.strictEqual(
tMatch.sort().toString(),
enMatch.sort().toString(),
`${key} is missing or has duplicate placeholders: ${translation}`
);
}
assert.strictEqual(translation.match(/[\n]/), null, `${key} contains a newline character ${translation}`);
};
const validate = function (json, name) {
assert.strictEqual(Object.keys(json).sort().toString(), enKeys, `${name}: Locale json keys do not match en.json`);
Object.entries(json).forEach(validateEntry);
};
let file = `// This file was automatically generated. Do not modify.
'use strict';
goog.provide('Blockly.ScratchMsgs.allLocales');
goog.require('Blockly.ScratchMsgs');
`;
let localeMap = {
'aa-dj': 'aa_DJ',
'es-419': 'es_419',
'pt-br': 'pt_BR',
'zh-cn': 'zh_CN',
'zh-tw': 'zh_TW'
};
const getLocaleData = async function (locale) {
let txLocale = localeMap[locale] || locale;
const data = await txPull(PROJECT, RESOURCE, txLocale, MODE);
return {
locale: locale,
translations: data
};
};
Promise.all(Object.keys(locales).map(getLocaleData)).then(function (values) {
values.forEach(function (translation) {
validate(translation.translations, translation.locale);
file += '\n';
file += `Blockly.ScratchMsgs.locales["${translation.locale}"] =\n`;
file += JSON.stringify(translation.translations, null, 4);
file += ';\n';
});
file += '// End of combined translations\n';
// write combined file
fs.writeFileSync(`${PATH_OUTPUT}/scratch_msgs.js`, file);
}).catch((err) => {
console.error(err);
process.exit(1);
});