mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-04-27 20:19:39 -05:00
Merge pull request #2355 from bluewave-labs/hotfix/language-download
hotfix: normalize language codes
This commit is contained in:
@@ -1,45 +1,60 @@
|
||||
import axios from 'axios';
|
||||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import { URLSearchParams } from 'url';
|
||||
import axios from "axios";
|
||||
import fs from "fs-extra";
|
||||
import path from "path";
|
||||
import { URLSearchParams } from "url";
|
||||
|
||||
// POEditor API information
|
||||
const API_TOKEN = process.env.POEDITOR_API;
|
||||
const PROJECT_ID = process.env.POEDITOR_PROJECT_ID;
|
||||
const LANGUAGES = (process.env.LANGUAGES || 'tr,en').split(',');
|
||||
const EXPORT_FORMAT = process.env.EXPORT_FORMAT || 'key_value_json';
|
||||
const LANGUAGES = (process.env.LANGUAGES || "tr,en").split(",");
|
||||
const EXPORT_FORMAT = process.env.EXPORT_FORMAT || "key_value_json";
|
||||
|
||||
// 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
|
||||
async function downloadTranslations() {
|
||||
try {
|
||||
console.log('Downloading translations from POEditor...');
|
||||
console.log("Downloading translations from POEditor...");
|
||||
console.log(`Using export format: ${EXPORT_FORMAT}`);
|
||||
|
||||
for (const language of LANGUAGES) {
|
||||
console.log(`Downloading translations for ${language} language...`);
|
||||
|
||||
// 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({
|
||||
api_token: API_TOKEN,
|
||||
id: PROJECT_ID,
|
||||
language: language,
|
||||
type: EXPORT_FORMAT
|
||||
type: EXPORT_FORMAT,
|
||||
})
|
||||
);
|
||||
|
||||
if (exportResponse.data.response.status !== 'success') {
|
||||
throw new Error(`Failed to get export URL for ${language} language: ${JSON.stringify(exportResponse.data)}`);
|
||||
if (exportResponse.data.response.status !== "success") {
|
||||
throw new Error(
|
||||
`Failed to get export URL for ${language} language: ${JSON.stringify(
|
||||
exportResponse.data
|
||||
)}`
|
||||
);
|
||||
}
|
||||
|
||||
const fileUrl = exportResponse.data.result.url;
|
||||
console.log(`Export URL obtained for ${language}`);
|
||||
|
||||
// Download translation file
|
||||
const downloadResponse = await axios.get(fileUrl, { responseType: 'json' });
|
||||
const downloadResponse = await axios.get(fileUrl, {
|
||||
responseType: "json",
|
||||
});
|
||||
const translations = downloadResponse.data;
|
||||
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 (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 = {};
|
||||
translations.forEach(item => {
|
||||
translations.forEach((item) => {
|
||||
if (item.term && item.definition) {
|
||||
formattedTranslations[item.term] = item.definition;
|
||||
}
|
||||
@@ -58,16 +75,20 @@ async function downloadTranslations() {
|
||||
}
|
||||
|
||||
// Determine the output filename based on language
|
||||
const filename = language === 'en' ? 'gb.json' : `${language}.json`;
|
||||
const outputPath = path.join(process.cwd(), 'temp', filename);
|
||||
const normalizedLanguage = normalizeLanguageCode(language);
|
||||
const filename =
|
||||
normalizedLanguage === "en" ? "gb.json" : `${normalizedLanguage}.json`;
|
||||
const outputPath = path.join(process.cwd(), "temp", filename);
|
||||
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) {
|
||||
console.error('An error occurred while downloading translations:', error);
|
||||
console.error("An error occurred while downloading translations:", error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -76,15 +97,15 @@ async function downloadTranslations() {
|
||||
async function main() {
|
||||
try {
|
||||
// Clean temp folder
|
||||
await fs.emptyDir(path.join(process.cwd(), 'temp'));
|
||||
await fs.emptyDir(path.join(process.cwd(), "temp"));
|
||||
|
||||
// Download translations
|
||||
await downloadTranslations();
|
||||
} catch (error) {
|
||||
console.error('An error occurred during the process:', error);
|
||||
console.error("An error occurred during the process:", error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Run script
|
||||
main();
|
||||
main();
|
||||
|
||||
Reference in New Issue
Block a user