Skip to content

Commit

Permalink
feat(i18): add locale downloader script
Browse files Browse the repository at this point in the history
  • Loading branch information
nurikk committed Feb 4, 2024
1 parent 4526aa6 commit c7061cf
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions download_translations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const fs = require('fs').promises;

async function getAvaliableLanguages(project_id, api_token) {
const body = new FormData();
body.set("api_token", api_token);
body.set("id", project_id);

const resp = await fetch('https://api.poeditor.com/v2/languages/list', {
method: 'POST',
body
})
const languages = await resp.json();
return languages.result.languages.map(lang => ({ code: lang.code, name: lang.name }));
}

async function downloadLanguage(project_id, api_token, language) {
console.log(`Downloading ${language.name} ${language.code}`);
const body = new FormData();
body.set("api_token", api_token);
body.set("id", project_id);
body.set("language", language.code);
body.set("type", "i18next");

const resp = await fetch('https://api.poeditor.com/v2/projects/export', {
method: 'POST',
body
})
const exportResult = await resp.json();
const languageRes = await fetch(exportResult.result.url);
const languageData = await languageRes.json();
return languageData;
}

const locale2fileMap = {
'uk': 'en',
'pt-br': 'ptbr',
'zh-Hans': 'chs',
'zh-TW': 'zh'

}
async function main(project_id, api_token) {
const locales = await getAvaliableLanguages(project_id, api_token);

for (const locale of locales) {
const exported = await downloadLanguage(project_id, api_token, locale);
const fileName = locale2fileMap[locale.code] || locale.code;
await fs.writeFile(`./src/i18n/locales/${fileName}.json`, JSON.stringify(exported, null, 2));
}
}

const { POEDITOR_PROJECT_ID, POEDITOR_API_TOKEN } = process.env;
main(POEDITOR_PROJECT_ID, POEDITOR_API_TOKEN);

0 comments on commit c7061cf

Please sign in to comment.