|
| 1 | +/** |
| 2 | + * Update translations for CreateAI tool. To ensure translations for ML |
| 3 | + * MakeCode blocks are consistent, translated strings are taken from |
| 4 | + * pxt-microbit-ml machine-learning-strings.json. See CodeViewDefaultBlock.tsx |
| 5 | + * to see where they are used. |
| 6 | + * |
| 7 | + * To update all translations, pass 2 arguments: |
| 8 | + * 1. Path to CreateAI tool translation strings directory. |
| 9 | + * 2. Path to machine-learning-strings.json translation strings directory. |
| 10 | + * |
| 11 | + * To only update MakeCode block translations, pass path to |
| 12 | + * machine-learning-strings.json translation strings directory as an argument. |
| 13 | + * |
| 14 | + * Manually run `npm run i18n:compile` after. |
| 15 | + * |
| 16 | + * To add a language, add below and then update for all translations. |
| 17 | + */ |
| 18 | +const fs = require("fs"); |
| 19 | + |
| 20 | +const okExitStatus = 0; |
| 21 | +const errExitStatus = 2; |
| 22 | + |
| 23 | +const languages = ["en", "es-ES", "ja", "ko", "nl", "pl", "pt-br", "zh-tw"]; |
| 24 | +const enMessagesToAdd = { |
| 25 | + "ml.onStart|block": { |
| 26 | + defaultMessage: "on ML $event start", |
| 27 | + description: "This string should be a Crowdin duplicate of the MakeCode extension block with the same text and use the same translation.", |
| 28 | + }, |
| 29 | +}; |
| 30 | + |
| 31 | +const getMessagesToAdd = (mlStrings, langMessages) => { |
| 32 | + return Object.keys(enMessagesToAdd).reduce((acc, k) => { |
| 33 | + // Add or update with translated strings. |
| 34 | + if (mlStrings[k]) { |
| 35 | + return { |
| 36 | + ...acc, |
| 37 | + [k]: { ...enMessagesToAdd[k], defaultMessage: mlStrings[k] }, |
| 38 | + }; |
| 39 | + } |
| 40 | + // Fallback to en messages if no translation. |
| 41 | + if (!langMessages[k]) { |
| 42 | + return { ...acc, [k]: { ...enMessagesToAdd[k] } }; |
| 43 | + } |
| 44 | + return { ...acc, [k]: { ...enMessagesToAdd[k], defaultMessage: acc[k].defaultMessage } }; |
| 45 | + }, {}); |
| 46 | +}; |
| 47 | +const getFileJSONContent = (filepath) => JSON.parse(fs.readFileSync(filepath)); |
| 48 | + |
| 49 | +const args = process.argv.slice(2); |
| 50 | +if (args.length === 0 || args.length > 2) { |
| 51 | + console.log(`Error: 2 arguments needed. |
| 52 | + 1. Path to CreateAI tool translation strings directory. |
| 53 | + 2. Path to machine-learning-strings.json translation strings directory. `); |
| 54 | + process.exit(errExitStatus); |
| 55 | +} |
| 56 | + |
| 57 | +const [createAiTranslationsFilepath, mlTranslationsFilepath] = |
| 58 | + args.length === 1 ? [null, args[0]] : args; |
| 59 | + |
| 60 | +languages.forEach((language) => { |
| 61 | + const lowerLang = language.toLowerCase(); |
| 62 | + const outputFilepath = `lang/ui.${lowerLang}.json`; |
| 63 | + |
| 64 | + if (language === "en") { |
| 65 | + // Assumes that lang/ui.en.json exists and directly adds enMessagesToAdd. |
| 66 | + const langMessages = getFileJSONContent(outputFilepath); |
| 67 | + fs.writeFileSync( |
| 68 | + outputFilepath, |
| 69 | + JSON.stringify({ ...langMessages, ...enMessagesToAdd }) |
| 70 | + ); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const srcLangFilepath = !createAiTranslationsFilepath |
| 75 | + ? `lang/ui.${lowerLang}.json` |
| 76 | + : `${createAiTranslationsFilepath}/${language}/ui.en.json`; |
| 77 | + const langMessages = getFileJSONContent(srcLangFilepath); |
| 78 | + |
| 79 | + const mlFilepath = `${mlTranslationsFilepath}/${language}/machine-learning-strings.json`; |
| 80 | + const mlStrings = getFileJSONContent(mlFilepath); |
| 81 | + |
| 82 | + const messagesToAdd = getMessagesToAdd(mlStrings, langMessages); |
| 83 | + fs.writeFileSync( |
| 84 | + outputFilepath, |
| 85 | + JSON.stringify({ ...langMessages, ...messagesToAdd }) |
| 86 | + ); |
| 87 | +}); |
| 88 | + |
| 89 | +process.exit(okExitStatus); |
0 commit comments