Skip to content

Commit 36c015d

Browse files
author
Brian Vaughn
committed
Added config validation and fixed language <> locale mapping
1 parent 5f2ef48 commit 36c015d

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

crowdin/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
__translations/
2+
translations/

crowdin/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
22
key: process.env.CROWDIN_API_KEY,
33
url: 'https://api.crowdin.com/api/project/react',
4-
translation_threshold: 50,
4+
threshold: 50,
55
};

crowdin/download.js

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,33 @@ const DOWNLOADED_TRANSLATIONS_PATH = path.resolve(__dirname, '__translations');
88
const DOWNLOADED_TRANSLATIONS_DOCS_PATH = path.resolve(
99
__dirname,
1010
'__translations',
11-
'docs',
11+
'test-17', // TODO (crowdin) This is probably not part of the final export structure
12+
'docs'
1213
);
1314

15+
const validateConfig = ({ key, threshold, url }) => {
16+
const errors = [];
17+
if (!key) {
18+
errors.push('key: No process.env.CROWDIN_API_KEY value defined.');
19+
}
20+
if (!Number.isInteger(threshold)) {
21+
errors.push(`threshold: Invalid translation threshold defined.`);
22+
}
23+
if (!url) {
24+
errors.push('url: No Crowdin project URL defined.');
25+
}
26+
if (errors.length > 0) {
27+
console.error('Invalid Crowdin config values for:\n• ' + errors.join('\n• '));
28+
29+
throw Error('Invalid Crowdin config');
30+
}
31+
};
32+
1433
function main() {
34+
validateConfig(config);
35+
1536
const crowdin = new Crowdin({apiKey: config.key, endpointUrl: config.url});
37+
1638
process.chdir(SYMLINKED_TRANSLATIONS_PATH);
1739

1840
crowdin
@@ -23,14 +45,13 @@ function main() {
2345
.then(locales => {
2446
const usableLocales = locales
2547
.filter(
26-
locale => locale.translated_progress > config.translation_threshold,
48+
locale => locale.translated_progress > config.threshold,
2749
)
2850
.map(local => local.code);
2951

3052
const localeDirectories = getDirectories(
3153
DOWNLOADED_TRANSLATIONS_DOCS_PATH,
3254
);
33-
3455
const localeToFolderMap = createLocaleToFolderMap(localeDirectories);
3556

3657
usableLocales.forEach(locale => {
@@ -43,14 +64,12 @@ function main() {
4364
// Note that the current working directory of this node process should be where the symlink is created
4465
// or else the relative paths would be incorrect
4566
function createSymLink(folder) {
46-
symlink(`../__translations/docs/${folder}`, folder, err => {
67+
symlink(path.resolve(DOWNLOADED_TRANSLATIONS_DOCS_PATH, folder), folder, err => {
4768
if (!err) {
48-
console.log(`Created symlink for ${folder}.`);
4969
return;
5070
}
5171

5272
if (err.code === 'EEXIST') {
53-
console.log(
5473
`Skipped creating symlink for ${folder}. A symlink already exists.`,
5574
);
5675
} else {
@@ -60,19 +79,21 @@ function createSymLink(folder) {
6079
});
6180
}
6281

63-
// When we run getTranslationStatus(), it gives us 2-ALPHA locale codes unless necessary
64-
// However, the folder structure of downloaded translations always has 4-ALPHA locale codes
65-
// This function creates a map from a locale code to its corresponding folder name
82+
// When we run getTranslationStatus(), it typically gives us ISO 639-1 (e.g. "fr" for French) or 639-3 (e.g. "fil" for Filipino) language codes,
83+
// But the folder structure of downloaded translations uses locale codes (e.g. "fr-FR" for French, "fil-PH" for the Philippines).
84+
// This function creates a map between language and locale code.
6685
function createLocaleToFolderMap(directories) {
67-
const twoAlphaLocale = locale => locale.substring(0, 2);
86+
const localeToLanguageCode = locale => locale.includes('-') ? locale.substr(0, locale.indexOf('-')) : locale;
6887
const localeToFolders = new Map();
6988
const localeToFolder = new Map();
7089

7190
for (let locale of directories) {
91+
const languageCode = localeToLanguageCode(locale);
92+
7293
localeToFolders.set(
73-
twoAlphaLocale(locale),
74-
localeToFolders.has(twoAlphaLocale(locale))
75-
? localeToFolders.get(twoAlphaLocale(locale)).concat(locale)
94+
languageCode,
95+
localeToFolders.has(languageCode)
96+
? localeToFolders.get(languageCode).concat(locale)
7697
: [locale],
7798
);
7899
}
@@ -87,6 +108,7 @@ function createLocaleToFolderMap(directories) {
87108
}
88109
});
89110

111+
console.log(localeToFolder);
90112
return localeToFolder;
91113
}
92114

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,20 @@
7979
"build": "gatsby build",
8080
"check-all": "npm-run-all prettier --parallel lint flow",
8181
"ci-check": "npm-run-all prettier:diff --parallel lint flow",
82+
"crowdin:download": "node ./crowdin/download",
8283
"dev": "gatsby develop -H 0.0.0.0",
8384
"flow": "flow",
8485
"format:source": "prettier --config .prettierrc --write \"{gatsby-*.js,{flow-typed,plugins,src}/**/*.js}\"",
8586
"format:examples": "prettier --config .prettierrc.examples --write \"examples/**/*.js\"",
8687
"lint": "eslint .",
87-
"netlify": "yarn install && yarn translations && yarn build",
88+
"netlify": "yarn install && yarn crowdin:download && yarn build",
8889
"nit:source": "prettier --config .prettierrc --list-different \"{gatsby-*.js,{flow-typed,plugins,src}/**/*.js}\"",
8990
"nit:examples": "prettier --config .prettierrc.examples --list-different \"examples/**/*.js\"",
9091
"prettier": "yarn format:source && yarn format:examples",
9192
"prettier:diff": "yarn nit:source && yarn nit:examples",
9293
"reset": "yarn reset:cache && yarn reset:translations",
9394
"reset:cache": "rimraf ./.cache",
94-
"reset:translations": "rimraf ./crowdin/__translations && find crowdin/translations -type l -not -name '*en-US' -delete",
95-
"translations": "node ./crowdin/download"
95+
"reset:translations": "rimraf ./crowdin/__translations && find crowdin/translations -type l -not -name '*en-US' -delete"
9696
},
9797
"devDependencies": {
9898
"eslint-config-prettier": "^2.6.0",

0 commit comments

Comments
 (0)