UNPKG

auto-translatr

Version:
90 lines (89 loc) 3.67 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import * as fs from 'fs'; import * as path from 'path'; import { left, right, isLeft } from './utils'; const getTranslationFilePath = (language, config) => { return path.join(process.cwd(), config.output, `${language}.json`); }; const readTranslationFile = (filePath) => { try { if (!fs.existsSync(filePath)) { return right({}); } const fileContent = fs.readFileSync(filePath, 'utf-8'); const parsed = JSON.parse(fileContent); return right(parsed); } catch (error) { return left(error); } }; export const readAllTranslations = (supportedLanguages, config) => { try { const translations = {}; for (const lang of supportedLanguages) { const filePath = getTranslationFilePath(lang, config); const result = readTranslationFile(filePath); if (isLeft(result)) { // If we can't read a translation file, log a warning but continue with empty translations console.warn(`Warning: Could not read translation file for language '${lang}': ${result.value.message}`); translations[lang] = {}; } else { translations[lang] = result.value; } } return right(translations); } catch (error) { return left(error); } }; export const writeLanguageFile = (language, translations, config, options = { append: false }) => { try { const filePath = getTranslationFilePath(language, config); const outputDir = path.dirname(filePath); // Ensure directory exists if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir, { recursive: true }); } let existingTranslations = {}; // If appending, try to read existing translations if (options.append && fs.existsSync(filePath)) { const readResult = readTranslationFile(filePath); if (isLeft(readResult)) { return left(new Error(`Failed to read existing translation file for '${language}': ${readResult.value.message}`)); } existingTranslations = readResult.value; } const newTranslations = Object.assign(Object.assign({}, existingTranslations), translations); const fileContent = JSON.stringify(newTranslations, null, 2); fs.writeFileSync(filePath, fileContent, 'utf-8'); return right(undefined); } catch (error) { return left(error); } }; export const writeLanguageFiles = (translations, config, options) => __awaiter(void 0, void 0, void 0, function* () { try { for (const lang of Object.keys(translations)) { const result = writeLanguageFile(lang, translations[lang], config, options); if (isLeft(result)) { return result; } } return right(undefined); } catch (error) { return left(error); } });