Skip to content

Commit 2964bbd

Browse files
committed
(still not done) translation scripts
1 parent c5a6b3b commit 2964bbd

12 files changed

+24060
-11
lines changed

i18n/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Translation Scripts
2+
**NOTE: PenguinMod uses different scripts as we have a different method for translation.**
3+
4+
These are written for converting the Blockly format to Google Sheets & back.
5+
The Scratch scripts are still used at build time, so we don't remove them.
6+
7+
Run these files with a file path as the argument if needed.
8+
9+
Example: `C:/scratch-blocks/i18n> node create_base_xlsx.js ./my_js_file.js`
10+
11+
## Files
12+
- `messages_fillout.js`
13+
Creates a JS file that merges English text from `msg/messages.js`
14+
into every language in `msg/scratch_msgs.js`.
15+
16+
**NOTE:** This output shouldn't replace the existing text in `msg/scratch_msgs.js`.
17+
This output is used as input in `create_base_xlsx.js` to create sheets.
18+
19+
- `create_base_xlsx.js`
20+
Creates an XLSX sheet based on the JS input taken from `msg/scratch_msgs.js` or `messages_fillout.js`.
21+
22+
- `import_xlsx.js`
23+
Creates a file in the format of `msg/scratch_msgs.js` based on the input XLSX sheet.

i18n/create_base_xlsx.js

Whitespace-only changes.

i18n/create_en_msgs.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const fs = require('fs');
2+
3+
const linePrefix = 'Blockly.Msg';
4+
console.log('Parsing files');
5+
6+
const EnMessages = fs.readFileSync('../msg/messages.js', 'utf8');
7+
const EnObject = (() => {
8+
const EnParsable = EnMessages.split('\n')
9+
.filter(line => line.startsWith(linePrefix))
10+
.join('\n');
11+
const Blockly = {
12+
Msg: {}
13+
};
14+
eval(EnParsable);
15+
return Blockly.Msg;
16+
})();
17+
18+
// write en.json because its just json.stringify
19+
console.log('writing json file');
20+
fs.writeFileSync('../msg/output/en.json', JSON.stringify(EnObject, null, 4), 'utf8');
21+
22+
// write en.js
23+
console.log('writing en.js');
24+
let fileText = `// This file was automatically generated. Do not modify.
25+
26+
'use strict';
27+
28+
goog.provide('Blockly.Msg.en');
29+
goog.require('Blockly.Msg');
30+
31+
`;
32+
for (const key in EnObject) {
33+
const value = EnObject[key];
34+
fileText += `Blockly.Msg[${JSON.stringify(key)}] = ${JSON.stringify(value)};\n`
35+
}
36+
fs.writeFileSync('../msg/output/en.js', fileText, 'utf8');

i18n/messages_fillout.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const fs = require('fs');
2+
3+
const linePrefix = 'Blockly.Msg';
4+
console.log('Parsing files');
5+
6+
const EnMessages = fs.readFileSync('../msg/messages.js', 'utf8');
7+
const CombinedMessages = fs.readFileSync('../msg/scratch_msgs.js', 'utf8');
8+
9+
const CombinedObject = (() => {
10+
const goog = {
11+
provide: () => {},
12+
require: () => {},
13+
};
14+
const Blockly = {
15+
ScratchMsgs: {
16+
locales: {}
17+
}
18+
};
19+
eval(CombinedMessages.replace("'use strict';", ''));
20+
return Blockly.ScratchMsgs.locales;
21+
})();
22+
const EnObject = (() => {
23+
const EnParsable = EnMessages.split('\n')
24+
.filter(line => line.startsWith(linePrefix))
25+
.join('\n');
26+
const Blockly = {
27+
Msg: {}
28+
};
29+
eval(EnParsable);
30+
return Blockly.Msg;
31+
})();
32+
33+
console.log('filling');
34+
35+
// merge english if the keys dont exist in the lang
36+
for (const langCode in CombinedObject) {
37+
const language = CombinedObject[langCode];
38+
for (const key in EnObject) {
39+
if (!(key in language)) {
40+
language[key] = EnObject[key];
41+
console.log('filled', langCode, 'missing', key);
42+
}
43+
}
44+
}
45+
46+
console.log('Saving to file...');
47+
let fileText = `// This file was automatically generated. Do not modify.
48+
49+
'use strict';
50+
51+
goog.provide('Blockly.ScratchMsgs.allLocales');
52+
53+
goog.require('Blockly.ScratchMsgs');
54+
55+
`;
56+
for (const langCode in CombinedObject) {
57+
const language = CombinedObject[langCode];
58+
fileText += `\nBlockly.ScratchMsgs.locales[${JSON.stringify(langCode)}] =\n`
59+
fileText += `${JSON.stringify(language, null, 4)};\n`;
60+
}
61+
fileText += `// End of combined translations\n`;
62+
fs.writeFileSync('../msg/output/filled_scratch_msgs.js', fileText, 'utf8');

msg/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# / (root)
2+
Important files for PenguinMod block text
3+
4+
- `messages.js`: Editable file, used for creating translations. Add default english text here.
5+
- `scratch_msgs.js`: Script-generated file, used by scratch-blocks. Contains all languages.
6+
7+
# /js
8+
Default language block files (JS)
9+
10+
- `en.js`: Script-generated file, used by scratch-blocks. Contains default english text in JS.
11+
12+
# /json
13+
Default language block files (JSON)
14+
15+
- `en.json`: Script-generated file, used by scratch-blocks. Contains default english text in JSON.
16+
17+
# /output
18+
Script-generated files by the custom PenguinMod Scripts (NOT the Scratch scripts)

msg/messages.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,26 @@
2020

2121
/**
2222
* @fileoverview English strings.
23-
* @author [email protected] (Andrew Sliwinski)
23+
* @author [email protected] (Andrew Sliwinski) PenguinMod for PM Scripts
2424
*
25-
* After modifying this file, run `npm run translate` from the root directory
26-
* to regenerate `./msg/json/en.json`.
27-
* IMPORTANT:
28-
* All message strings must use single quotes for the scripts to work properly
29-
*/
25+
* PENGUINMOD SCRIPT INSTRUCTIONS
26+
* All message strings must use single quotes for the build scripts to work properly.
27+
* Lines starting with "Blockly.Msg" will be evaluated as JS. This likely doesn't apply in the build scripts.
28+
* Make sure to import the translations from the google sheet before running any scripts!
29+
* After modifying this file:
30+
* 1. Run the i18n/create_en_msgs script and replace msg/js/en.js and msg/json/en.json with the output
31+
* 2. Run the i18n/messages_fillout script for sheet creation (make sure you imported the translations from the google sheet first)
32+
* 2.5. If you modified the text of a translation key, run i18n/messages_override with the generated filled_scratch_msgs.js from step 2 as an argument to the script & then specify the changed keys as arguments after.
33+
* 3. Generate an XLSX file with i18n/create_base_xlsx for translation
34+
* 4. Please update the Google Sheets if you have access (otherwise, someone with access needs to update it later)
35+
*/
36+
37+
// SCRATCH SCRIPT INSTRUCTIONS (NOT REQUIRED IN PM)
38+
// After modifying this file, run `npm run translate` from the root directory
39+
// to regenerate `./msg/json/en.json`.
40+
// IMPORTANT:
41+
// All message strings must use single quotes for the scripts to work properly
42+
3043
'use strict';
3144

3245
goog.provide('Blockly.Msg.en');

msg/messages_fillout.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)