Skip to content

Commit d8952da

Browse files
First translation sync (#576)
- Adds translation files for CreateAI. All languages are a preview for now and will not show on live. - Sync language from tool to embedded MakeCode. Currently, changing language inside MakeCode does not work. - Updates extension version to also support translations. (microbit-foundation/pxt-microbit-ml#31) - Adds a link to ask for more support in translation in Language dialog. (microbit-foundation/ml-trainer-microbit#16 (private)) - Script for adding machine-learning-strings.json to ui.en.json for MakeCode default code view. - Handle different translations and text length for default code view blocks.
1 parent 0bbceca commit d8952da

25 files changed

+32956
-70
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
- run: npm ci
3737
env:
3838
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39-
- run: npm install --no-save @microbit-foundation/ml-trainer-microbit@0.2.0-dev.58 @microbit-foundation/website-deploy-aws@0.6 @microbit-foundation/website-deploy-aws-config@0.9
39+
- run: npm install --no-save @microbit-foundation/ml-trainer-microbit@0.2.0-dev.60 @microbit-foundation/website-deploy-aws@0.6 @microbit-foundation/website-deploy-aws-config@0.9
4040
if: github.repository_owner == 'microbit-foundation'
4141
env:
4242
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

bin/update-translations.cjs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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);

lang/ui.en.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,10 @@
763763
"defaultMessage": "Help & support",
764764
"description": "Link or menu item link to support site"
765765
},
766+
"help-translate": {
767+
"defaultMessage": "Help translate",
768+
"description": "Help translate menu option text"
769+
},
766770
"home-action": {
767771
"defaultMessage": "Home",
768772
"description": "Home button text"
@@ -1055,22 +1059,14 @@
10551059
"defaultMessage": "Left arrow icon for MakeCode back",
10561060
"description": "Testing model page title"
10571061
},
1058-
"makecode-block-default-alt": {
1059-
"defaultMessage": "MakeCode block: on ML {actionName} start, show icon {iconName}",
1060-
"description": "Makecode default block alt text"
1061-
},
1062-
"makecode-block-on-ML": {
1063-
"defaultMessage": "on ML",
1064-
"description": "Makecode block text. The translation should match MakeCode translations."
1062+
"makecode-block-alt-prefix": {
1063+
"defaultMessage": "MakeCode block:",
1064+
"description": "Makecode block alt text prefix"
10651065
},
10661066
"makecode-block-show-icon": {
10671067
"defaultMessage": "show icon",
10681068
"description": "Makecode block text. The translation should match MakeCode translations."
10691069
},
1070-
"makecode-block-start": {
1071-
"defaultMessage": "start",
1072-
"description": "Makecode block text. The translation should match MakeCode translations."
1073-
},
10741070
"makecode-load-error-dialog-body": {
10751071
"defaultMessage": "{appNameFull} has failed to load MakeCode. Please check your internet connection, reload the page then try again.",
10761072
"description": "Content of MakeCode load error dialog"
@@ -1083,6 +1079,10 @@
10831079
"defaultMessage": "Your micro:bit is not connected",
10841080
"description": "Live graph disconnected micro:bit status message"
10851081
},
1082+
"ml.onStart|block": {
1083+
"defaultMessage": "on ML $event start",
1084+
"description": "This string should be a Crowdin duplicate of the MakeCode extension block with the same text and use the same translation."
1085+
},
10861086
"more-edit-in-makecode-options": {
10871087
"defaultMessage": "More edit in MakeCode options",
10881088
"description": "Aria label for the additional actions menu to the right of the Edit in MakeCode button"

0 commit comments

Comments
 (0)