File size: 2,263 Bytes
fbe84a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const fs = require('fs');
const xlsx = require('xlsx');

console.log('Parsing files');

const filePath = process.argv[2];
if (!filePath) {
    throw new Error("No file path specified");
}
const workbook = xlsx.readFile(filePath);

const getLanguageCode = (inputString) => {
    const parts = inputString.split('|');
    if (parts.length === 2) {
        return parts[1].trim();
    }
}

const writeEnFiles = (json) => {
    console.log('writing en.json');
    fs.writeFileSync('../msg/json/en.json', JSON.stringify(json, null, 4), 'utf8');

    console.log('writing en.js');
    let fileText = `// This file was automatically generated.  Do not modify.



'use strict';



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

goog.require('Blockly.Msg');



`;
    for (const key in json) {
        const value = json[key];
        fileText += `Blockly.Msg[${JSON.stringify(key)}] = ${JSON.stringify(value)};\n`
    }
    fs.writeFileSync('../msg/js/en.js', fileText, 'utf8');
};

const CombinedObject = {};
for (const sheetName of workbook.SheetNames) {
    const languageCode = getLanguageCode(sheetName);
    console.log('parsing', languageCode);

    const worksheet = workbook.Sheets[sheetName];
    const jsonData = xlsx.utils.sheet_to_json(worksheet);

    const keyValuePairs = {};
    for (const pair of jsonData) {
        const key = pair['Translation Keys'];
        const value = pair['Translated Text'];
        if (key.startsWith('--')) continue;
        keyValuePairs[key] = value;
    }
    CombinedObject[languageCode] = keyValuePairs;

    if (languageCode === 'en') {
        writeEnFiles(keyValuePairs);
    }
}

console.log('writing scratch_msgs.js');
let msgsfileText = `// This file was automatically generated.  Do not modify.



'use strict';



goog.provide('Blockly.ScratchMsgs.allLocales');



goog.require('Blockly.ScratchMsgs');



`;
for (const langCode in CombinedObject) {
    const language = CombinedObject[langCode];
    msgsfileText += `\nBlockly.ScratchMsgs.locales[${JSON.stringify(langCode)}] =\n`
    msgsfileText += `${JSON.stringify(language, null, 4)};\n`;
}
msgsfileText += `// End of combined translations\n`;
fs.writeFileSync('../msg/scratch_msgs.js', msgsfileText, 'utf8');