UNPKG

zlocalz

Version:

ZLocalz - TUI Locale Guardian for Flutter ARB l10n/i18n validation and translation with AI-powered fixes

132 lines 5 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ARBParser = void 0; const fs = __importStar(require("fs/promises")); const path = __importStar(require("path")); const fast_glob_1 = __importDefault(require("fast-glob")); class ARBParser { static ARB_PATTERN = '**/*.arb'; static METADATA_PREFIX = '@'; static async discoverARBFiles(basePath) { const files = await (0, fast_glob_1.default)(this.ARB_PATTERN, { cwd: basePath, absolute: true, ignore: ['**/node_modules/**', '**/build/**', '**/.*/**'] }); return files.sort(); } static async parseARBFile(filePath) { const content = await fs.readFile(filePath, 'utf-8'); const raw = JSON.parse(content); const locale = this.extractLocale(filePath); const entries = {}; for (const [key, value] of Object.entries(raw)) { if (key.startsWith(this.METADATA_PREFIX)) continue; const entry = { key, value: String(value) }; const metadataKey = `${this.METADATA_PREFIX}${key}`; if (raw[metadataKey]) { const metadata = raw[metadataKey]; entry.metadata = metadata; entry.description = metadata.description; entry.placeholders = metadata.placeholders; } entries[key] = entry; } const stats = await fs.stat(filePath); return { locale, path: filePath, format: 'arb', entries, raw, lastModified: stats.mtime }; } static extractLocale(filePath) { const basename = path.basename(filePath, '.arb'); const parts = basename.split('_'); return parts[parts.length - 1] || 'unknown'; } static async writeARBFile(arbFile) { const output = {}; if (arbFile.raw['@@locale']) { output['@@locale'] = arbFile.raw['@@locale']; } const keys = Object.keys(arbFile.entries); for (const key of keys) { const entry = arbFile.entries[key]; output[key] = entry.value; if (entry.metadata || entry.description || entry.placeholders) { const metadata = {}; if (entry.description) metadata.description = entry.description; if (entry.placeholders) metadata.placeholders = entry.placeholders; if (entry.metadata) Object.assign(metadata, entry.metadata); if (Object.keys(metadata).length > 0) { output[`${this.METADATA_PREFIX}${key}`] = metadata; } } } const content = JSON.stringify(output, null, 2) + '\n'; await fs.writeFile(arbFile.path, content, 'utf-8'); } static extractPlaceholders(value) { const placeholderRegex = /\{([^}]+)\}/g; const placeholders = []; let match; while ((match = placeholderRegex.exec(value)) !== null) { placeholders.push(match[1]); } return placeholders; } static normalizeICUMessage(message) { return message .replace(/\s+/g, ' ') .replace(/\s*([{}])\s*/g, '$1') .trim(); } } exports.ARBParser = ARBParser; //# sourceMappingURL=arb-parser.js.map