Skip to content

Commit

Permalink
chore(i18n): cleanup terms in poeditor
Browse files Browse the repository at this point in the history
  • Loading branch information
nurikk committed Feb 11, 2024
1 parent d7f451b commit d2ef1e6
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 33 deletions.
27 changes: 27 additions & 0 deletions clean-terms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { getTerms, deleteTerms } = require("./poeditor");

async function main(project_id, api_token) {
const enTranslationFile = './src/i18n/locales/en.json';
const enTranslations = require(enTranslationFile);

const terms = await getTerms(project_id, api_token, 'en');
const cleanableContexts = [
'"featureDescriptions"',
'"featureNames"',
'"settingsSchemaDescriptions"',
'"settingsSchemaTitles"',
];
const cleanableTerms = terms
.filter((term) => cleanableContexts.includes(term.context));

const termsToClean = cleanableTerms
.filter((term) => !enTranslations[JSON.parse(term.context)][term.term]);

console.log(`cleanableTerms ${cleanableTerms.length}, termsToClean ${termsToClean.length}`);
const deletionResults = await deleteTerms(project_id, api_token, termsToClean);
console.log(deletionResults);

}

const { POEDITOR_PROJECT_ID, POEDITOR_API_TOKEN } = process.env;
main(POEDITOR_PROJECT_ID, POEDITOR_API_TOKEN);
32 changes: 1 addition & 31 deletions download_translations.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
const { getAvaliableLanguages, downloadLanguage } = require('./poeditor');
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: 'ua',
'pt-br': 'ptbr',
Expand Down
5 changes: 3 additions & 2 deletions i18n-z2m-exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ devices.forEach((device) => {
});
const enTranslationFile = './src/i18n/locales/en.json';
const enTranslations = require(enTranslationFile);
enTranslations.featureDescriptions = featureDescriptions;
enTranslations.featureNames = featureNames;


let settingsSchemaDescriptions = {};
let settingsSchemaTitles = {};
Expand All @@ -43,6 +42,8 @@ const exportSettingsSchemaDescriptions = (obj) => {

exportSettingsSchemaDescriptions(settingsSchema);

enTranslations.featureDescriptions = featureDescriptions;
enTranslations.featureNames = featureNames;
enTranslations.settingsSchemaDescriptions = settingsSchemaDescriptions;
enTranslations.settingsSchemaTitles = settingsSchemaTitles;

Expand Down
55 changes: 55 additions & 0 deletions poeditor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
async function callApi(url, parameters) {
const body = new FormData();
for (const key in parameters) {
body.set(key, parameters[key]);
}
const resp = await fetch(url, {
method: 'POST',
body,
});
return await resp.json();
}

async function downloadLanguage(project_id, api_token, language) {
console.log(`Downloading ${language.name} ${language.code}`);

const exportResult = await callApi('https://api.poeditor.com/v2/projects/export', {
api_token,
id: project_id,
language: language.code,
type: 'i18next',
});
const languageRes = await fetch(exportResult.result.url);
const languageData = await languageRes.json();
return languageData;
}

async function getAvaliableLanguages(project_id, api_token) {
const languages = await callApi('https://api.poeditor.com/v2/languages/list', {
api_token,
id: project_id
});
return languages.result.languages.map((lang) => ({ code: lang.code, name: lang.name }));
}

async function getTerms(project_id, api_token, language) {
const terms = await callApi('https://api.poeditor.com/v2/terms/list', {
api_token,
id: project_id,
language
});
return terms.result.terms.map((term) => ({ term: term.term, context: term.context }));
}


async function deleteTerms(project_id, api_token, terms) {
const res = await callApi('https://api.poeditor.com/v2/terms/delete', {
api_token,
id: project_id,
data: JSON.stringify(terms)
});
return res;
}


module.exports = { downloadLanguage, getAvaliableLanguages, getTerms, deleteTerms };

0 comments on commit d2ef1e6

Please sign in to comment.