92 lines
3.0 KiB
JavaScript
92 lines
3.0 KiB
JavaScript
import axios from 'axios';
|
||
import fs from 'fs-extra';
|
||
import path from 'path';
|
||
import { fileURLToPath } from 'url';
|
||
|
||
// Get current directory
|
||
const __filename = fileURLToPath(import.meta.url);
|
||
|
||
// POEditor API information
|
||
const API_TOKEN = process.env.POEDITOR_API_TOKEN;
|
||
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';
|
||
|
||
// POEditor API endpoint
|
||
const API_URL = 'https://api.poeditor.com/v2';
|
||
|
||
// Function to download translations
|
||
async function downloadTranslations() {
|
||
try {
|
||
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`,
|
||
new URLSearchParams({
|
||
api_token: API_TOKEN,
|
||
id: PROJECT_ID,
|
||
language: language,
|
||
type: EXPORT_FORMAT
|
||
})
|
||
);
|
||
|
||
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 translations = downloadResponse.data;
|
||
console.log(`Downloaded translations for ${language}`);
|
||
|
||
// POEditor'den dönen veri formatını kontrol et ve gerekirse dönüştür
|
||
let formattedTranslations = translations;
|
||
|
||
// Eğer dizi formatında geldiyse, key-value formatına dönüştür
|
||
if (Array.isArray(translations)) {
|
||
console.log(`Converting array format to key-value format for ${language}`);
|
||
formattedTranslations = {};
|
||
translations.forEach(item => {
|
||
if (item.term && item.definition) {
|
||
formattedTranslations[item.term] = item.definition;
|
||
}
|
||
});
|
||
}
|
||
|
||
// Save file
|
||
const outputPath = path.join(process.cwd(), 'temp', `${language}.json`);
|
||
await fs.writeJson(outputPath, formattedTranslations, { spaces: 2 });
|
||
|
||
console.log(`Translations for ${language} language successfully downloaded and saved: ${outputPath}`);
|
||
}
|
||
|
||
console.log('All translations successfully downloaded!');
|
||
} catch (error) {
|
||
console.error('An error occurred while downloading translations:', error);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// Main function
|
||
async function main() {
|
||
try {
|
||
// Clean temp folder
|
||
await fs.emptyDir(path.join(process.cwd(), 'temp'));
|
||
|
||
// Download translations
|
||
await downloadTranslations();
|
||
} catch (error) {
|
||
console.error('An error occurred during the process:', error);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// Run script
|
||
main();
|