Skip to content

Commit 09e85d2

Browse files
committed
Add localization tooling and clean up existing locales
1 parent 326d8e9 commit 09e85d2

File tree

16 files changed

+910
-1761
lines changed

16 files changed

+910
-1761
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,5 @@ dist/
6565
certs/
6666
results/
6767
.ret.credentials
68+
src/assets/locales/en.json
69+
src/assets/locales/extracted-messages.json

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"spritesheet:system-notice": "spritesheet-js -f json -p src/assets/images/spritesheets/ --padding 8 --divisibleByTwo -n sprite-system-notice-spritesheet --powerOfTwo src/assets/images/sprites/notice/*",
3737
"storybook": "start-storybook -p 6006",
3838
"deploy-storybook": "storybook-to-ghpages",
39-
"extract": "formatjs extract src/**/*.js --throws --extract-source-location --extract-from-format-message-call"
39+
"intl-extract": "node ./src/assets/locales/intl-extract.js",
40+
"intl-extract-en-locale": "node ./src/assets/locales/intl-extract.js en"
4041
},
4142
"scripts-info": {
4243
"start": "Run the client against your Hubs Cloud instance, or Mozilla's developer infrastructure,\n using application configs pulled from the server",
@@ -176,6 +177,7 @@
176177
"esm": "^3.2.5",
177178
"fast-plural-rules": "0.0.3",
178179
"file-loader": "^1.1.10",
180+
"glob": "^7.1.6",
179181
"html-loader": "^0.5.5",
180182
"html-webpack-plugin": "^4.2.0",
181183
"htmlhint": "^0.11.0",

src/assets/locales/README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# Localization
22

3+
The Hubs redesign has a lot of new text and we need help from people like you to translate it. Follow the steps below to get started.
4+
5+
## CLI Tools
6+
7+
### `npm run intl-extract`
8+
9+
This script extracts all existing messages in the Hubs codebase to a temporary file, `extracted-messages.json`. This file includes the message id, default message, and where the translation is used in the codebase.
10+
11+
### `npm run intl-extract-en-locale`
12+
13+
This script extracts all existing messages in the Hubs codebase to a temporary file `en.json`. It does not contain any metadata, just the id and English translation. It can be used as a template for other locales.
14+
15+
### Coming Soon
16+
17+
Our goal is to integrate with [Mozilla Pontoon](https://pontoon.mozilla.org) in the near future.
18+
319
## Adding Locales
420

521
1. Add an entry to `AVAILABLE_LOCALES` in [locale_config.js](locale_config.js) with the appropriate locale code and in-language translation of the language.
@@ -8,9 +24,7 @@
824
3. Edit your new locale file with your translations.
925

1026
## Adding to existing Locales
11-
12-
1. Add your new message to the Hubs client. Be sure to follow the best practices guide below. The default message should be in English. You do not need to add an additional message to the en.json file.
13-
2. Update the other locale files with correct translations if available.
27+
Add your new message to the Hubs client in the appropriate json file in this directory. Be sure to follow the best practices guide below. The default message should be in English.
1428

1529
## Best Practices
1630
The best practices for localization are the following:

src/assets/locales/es.json

Lines changed: 163 additions & 355 deletions
Large diffs are not rendered by default.

src/assets/locales/intl-extract.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const path = require("path");
2+
const { extract } = require("@formatjs/cli");
3+
const glob = require("glob");
4+
const promisify = require("util").promisify;
5+
const asyncGlob = promisify(glob);
6+
const fs = require("fs");
7+
const asyncWriteFile = promisify(fs.writeFile);
8+
9+
async function main(argv) {
10+
const command = argv[2];
11+
const basePath = path.resolve(__dirname, "..", "..", "..");
12+
const files = await asyncGlob("../../../src/**/*.js", { cwd: __dirname, absolute: true });
13+
14+
if (command === "en") {
15+
const outPath = path.join(__dirname, "en.json");
16+
17+
const extractedMessages = await extract(files, {
18+
extractSourceLocation: false,
19+
extractFromFormatMessageCall: true,
20+
removeDefaultMessage: false,
21+
format: {
22+
format: msgs => {
23+
for (const id in msgs) {
24+
msgs[id] = msgs[id].defaultMessage;
25+
}
26+
return msgs;
27+
}
28+
}
29+
});
30+
31+
await asyncWriteFile(outPath, extractedMessages);
32+
33+
console.log(`Wrote file to ${outPath}`);
34+
} else {
35+
const outPath = path.join(__dirname, "extracted-messages.json");
36+
37+
const extractedMessages = await extract(files, {
38+
extractSourceLocation: true,
39+
extractFromFormatMessageCall: true,
40+
removeDefaultMessage: false,
41+
format: {
42+
format: msgs => {
43+
for (const id in msgs) {
44+
const msg = msgs[id];
45+
msg.file = path.relative(basePath, msg.file);
46+
}
47+
48+
return msgs;
49+
}
50+
}
51+
});
52+
53+
await asyncWriteFile(outPath, extractedMessages);
54+
55+
console.log(`Wrote file to ${outPath}`);
56+
}
57+
}
58+
59+
main(process.argv)
60+
.then(() => {
61+
process.exit(0);
62+
})
63+
.catch(error => {
64+
console.error(error);
65+
process.exit(1);
66+
});

0 commit comments

Comments
 (0)