/
socolovivan
/
sample_android_app_localization
Обзор
Документация
Войти
/
socolovivan
/
sample_android_app_localization
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
localization
import-file.js
137 строк
4 KB
qqshka
update import-file.js
06 июн 2025, 17:00
06 июн 2025, 17:00
be9e95a
Код
Авторство
О чём код?
const axios = require('axios'); const fs = require('fs'); const path = require('path'); // Constants const API_KEY = "M2VkNDY1NzYtNmM0Ni00NzdkLWEzNmEtMTFhYTMxOGQ2MDY3OjFfSDAzMWZUZ3NSdzBsWUx3NG5MbWEwVDhnTg=="; const PROJECT_ID = "a0924501-f718-4d30-882e-4252985230c9"; const API_URL = "https://app.plaia.ru/api/integration/v1"; // Headers for authentication const headers = { 'Authorization': `Basic ${API_KEY}`, 'Content-Type': 'application/json' }; // Mapping of language codes to Android language codes const LANGUAGE_CODE_MAP = { 25: "ru", // Russian 1: "en", // English 11: "ja", // Japanese 12: "ko", // Korean // Add more mappings as needed }; async function getProjects() { try { const response = await axios.get(`${API_URL}/project/list`, { headers }); return response.data; } catch (error) { throw error; } } function getCompletedProjects(projects) { return projects.filter(project => project.status === 'completed' && project.id === PROJECT_ID); } async function downloadProjectFiles(documentId) { try { // Initiate export and get task ID const exportResponse = await axios.post(`${API_URL}/document/export`, null, { headers, params: { documentIds: documentId, mode: 'Complete', type: 'Target', exportingDocumentFormat: 'android-xml' } }); const taskId = exportResponse.data.id; console.log(`Export initiated with taskId: ${taskId}`); let taskStatus; let content; // Check the status of the export task while (true) { const statusResponse = await axios.get(`${API_URL}/document/export/${taskId}`, { headers }); taskStatus = statusResponse.status; // Get HTTP status code if (taskStatus === 200) { // If status is 200, file is ready in the response body console.log(`Export completed for taskId: ${taskId}`); content = statusResponse.data; // File content is in the response body break; } else if (taskStatus === 204) { // If status is 204, task is not ready yet console.log(`Task HTTP status: ${taskStatus}`); console.log("Task is not ready yet. Waiting..."); await new Promise(resolve => setTimeout(resolve, 5000)); // Wait for 5 seconds } else if ([422, 404].includes(taskStatus)) { // If status is 422 or 404, stop processing throw new Error(`Export failed with HTTP status: ${taskStatus}`); } else { // Unexpected status throw new Error(`Unexpected HTTP status: ${taskStatus}`); } } return { taskId, content }; } catch (error) { throw error; } } function saveFiles(documentId, content) { // Extract language code from taskId const languageCodeNum = documentId.split('_').pop(); // Get the part after the last "_" console.log(languageCodeNum); const languageCode = LANGUAGE_CODE_MAP[languageCodeNum]; if (!languageCode) { console.error(`Unknown language code: ${languageCodeNum}. Skipping file.`); return; } // Define the target directory based on the language code const targetDirectory = path.join(__dirname, 'app', 'src', 'main', 'res', `values-${languageCode}`); // Create the directory if it doesn't exist if (!fs.existsSync(targetDirectory)) { fs.mkdirSync(targetDirectory, { recursive: true }); } // Define the file path for the strings.xml file const filePath = path.join(targetDirectory, "strings.xml"); // Save the file fs.writeFileSync(filePath, content); console.log(`File saved to ${filePath}`); } async function main() { try { const projects = await getProjects(); const completedProjects = getCompletedProjects(projects); if (completedProjects.length === 0) { console.log("No completed projects found."); return; } for (const document of completedProjects[0].documents) { const documentId = document.id; console.log(`Processing document with ID: ${documentId}`); const { taskId, content } = await downloadProjectFiles(documentId); saveFiles(documentId, content); } } catch (error) { console.error("Error in main function:", error.message); } } main();