Merge pull request #2355 from bluewave-labs/hotfix/language-download

hotfix: normalize language codes
This commit is contained in:
Alexander Holliday authored and GitHub committed 2025-05-27 09:47:14 -07:00
commit 7d0668279e
1 file changed
+43 -22
+43 -22
View File
@@ -1,45 +1,60 @@
import axios from 'axios'; import axios from "axios";
import fs from 'fs-extra'; import fs from "fs-extra";
import path from 'path'; import path from "path";
import { URLSearchParams } from 'url'; import { URLSearchParams } from "url";
// POEditor API information // POEditor API information
const API_TOKEN = process.env.POEDITOR_API; const API_TOKEN = process.env.POEDITOR_API;
const PROJECT_ID = process.env.POEDITOR_PROJECT_ID; const PROJECT_ID = process.env.POEDITOR_PROJECT_ID;
const LANGUAGES = (process.env.LANGUAGES || 'tr,en').split(','); const LANGUAGES = (process.env.LANGUAGES || "tr,en").split(",");
const EXPORT_FORMAT = process.env.EXPORT_FORMAT || 'key_value_json'; const EXPORT_FORMAT = process.env.EXPORT_FORMAT || "key_value_json";
// POEditor API endpoint // POEditor API endpoint
const API_URL = 'https://api.poeditor.com/v2'; const API_URL = "https://api.poeditor.com/v2";
function normalizeLanguageCode(language) {
if (language.includes("-")) {
const [base, region] = language.split("-");
return `${base}-${region.toUpperCase()}`;
}
return language;
}
// Function to download translations // Function to download translations
async function downloadTranslations() { async function downloadTranslations() {
try { try {
console.log('Downloading translations from POEditor...'); console.log("Downloading translations from POEditor...");
console.log(`Using export format: ${EXPORT_FORMAT}`); console.log(`Using export format: ${EXPORT_FORMAT}`);
for (const language of LANGUAGES) { for (const language of LANGUAGES) {
console.log(`Downloading translations for ${language} language...`); console.log(`Downloading translations for ${language} language...`);
// Get export URL from POEditor // Get export URL from POEditor
const exportResponse = await axios.post(`${API_URL}/projects/export`, const exportResponse = await axios.post(
`${API_URL}/projects/export`,
new URLSearchParams({ new URLSearchParams({
api_token: API_TOKEN, api_token: API_TOKEN,
id: PROJECT_ID, id: PROJECT_ID,
language: language, language: language,
type: EXPORT_FORMAT type: EXPORT_FORMAT,
}) })
); );
if (exportResponse.data.response.status !== 'success') { if (exportResponse.data.response.status !== "success") {
throw new Error(`Failed to get export URL for ${language} language: ${JSON.stringify(exportResponse.data)}`); throw new Error(
`Failed to get export URL for ${language} language: ${JSON.stringify(
exportResponse.data
)}`
);
} }
const fileUrl = exportResponse.data.result.url; const fileUrl = exportResponse.data.result.url;
console.log(`Export URL obtained for ${language}`); console.log(`Export URL obtained for ${language}`);
// Download translation file // Download translation file
const downloadResponse = await axios.get(fileUrl, { responseType: 'json' }); const downloadResponse = await axios.get(fileUrl, {
responseType: "json",
});
const translations = downloadResponse.data; const translations = downloadResponse.data;
console.log(`Downloaded translations for ${language}`); console.log(`Downloaded translations for ${language}`);
@@ -48,9 +63,11 @@ async function downloadTranslations() {
// If data is in array format, convert it to key-value format // If data is in array format, convert it to key-value format
if (Array.isArray(translations)) { if (Array.isArray(translations)) {
console.log(`Converting array format to key-value format for ${language}`); console.log(
`Converting array format to key-value format for ${language}`
);
formattedTranslations = {}; formattedTranslations = {};
translations.forEach(item => { translations.forEach((item) => {
if (item.term && item.definition) { if (item.term && item.definition) {
formattedTranslations[item.term] = item.definition; formattedTranslations[item.term] = item.definition;
} }
@@ -58,16 +75,20 @@ async function downloadTranslations() {
} }
// Determine the output filename based on language // Determine the output filename based on language
const filename = language === 'en' ? 'gb.json' : `${language}.json`; const normalizedLanguage = normalizeLanguageCode(language);
const outputPath = path.join(process.cwd(), 'temp', filename); const filename =
normalizedLanguage === "en" ? "gb.json" : `${normalizedLanguage}.json`;
const outputPath = path.join(process.cwd(), "temp", filename);
await fs.writeJson(outputPath, formattedTranslations, { spaces: 2 }); await fs.writeJson(outputPath, formattedTranslations, { spaces: 2 });
console.log(`Translations for ${language} language successfully downloaded and saved as: ${filename}`); console.log(
`Translations for ${language} language successfully downloaded and saved as: ${filename}`
);
} }
console.log('All translations successfully downloaded!'); console.log("All translations successfully downloaded!");
} catch (error) { } catch (error) {
console.error('An error occurred while downloading translations:', error); console.error("An error occurred while downloading translations:", error);
process.exit(1); process.exit(1);
} }
} }
@@ -76,12 +97,12 @@ async function downloadTranslations() {
async function main() { async function main() {
try { try {
// Clean temp folder // Clean temp folder
await fs.emptyDir(path.join(process.cwd(), 'temp')); await fs.emptyDir(path.join(process.cwd(), "temp"));
// Download translations // Download translations
await downloadTranslations(); await downloadTranslations();
} catch (error) { } catch (error) {
console.error('An error occurred during the process:', error); console.error("An error occurred during the process:", error);
process.exit(1); process.exit(1);
} }
} }