The user wants a design for a Command Line Interface (CLI) tool for localization (l10n).
The key features requested are:
ICU Message Extraction: Scanning codebases for ICU (International Components for Unicode) formatted strings.
Validation: Checking gender and plural rules against the CLDR (Common Locale Data Repository) or specific language rules.
Translation Memory (TM) Population: Storing extracted strings and potential translations into a TM format.
1. High-Level Architecture & Technologies:
Language: Python or Node.js are the usual suspects for CLIs. Node.js is often preferred for web-heavy projects (React/Vue/Angular), while Python is great for data processing. Let's stick to a generic design but maybe lean towards Node.js/TypeScript because ICU is heavily used in the JS ecosystem (FormatJS/intl-messageformat), or Python if we want to emphasize the parsing logic. Actually, let's keep the implementation language agnostic but focus on the components.
Parser: Need an AST (Abstract Syntax Tree) parser for the specific source code language (e.g., JS/TS, Python) or a regex-based extractor if the syntax is simple. Since it's a design, I should propose an architecture that supports plugins for different frameworks.
ICU Library: The core logic needs to understand the ICU syntax.
TM Storage: JSON files, SQLite, or PO files (gettext style) are common.
2. Command Structure:
A standard CLI structure usually involves:
init: Setup config.
extract: The core feature.
validate: The checking feature.
sync / populate: Handling TMs and Translation Management Systems (TMS).
3. Feature Breakdown:
Extraction:
Needs a configuration file (e.g., .l10nrc).
Needs to define "markers" (e.g., t('key', 'message') or <FormattedMessage>).
Output format: A catalog (JSON usually) mapping IDs to default messages.
Validation (Gender/Plurals):
This is the trickiest part.
Syntax Validation: Is the ICU string parseable? (e.g., matching braces).
Semantic Validation:
Plurals: Does # items match zero, one, other? (Note: English usually just has one and other, but CLDR has specific rules).
Genders: Does the switch cover male, female, other?
Language Context: If I'm translating into Arabic, I need duals. If Russian, specific plurals. The CLI needs to know the target locales.
Translation Memory (TM) Population:
Input: Source strings.
Process: Look for fuzzy matches or exact matches in a database/file.
Output: Pre-filled files ready for translators, or an upload package for a SaaS tool (like Crowdin/Phrase).
Critique: Just extracting isn't enough. The prompt asks for validating rules. I need to emphasize how we check if a plural string is valid for specific target languages.
Solution: The validator needs a CLDR dataset dependency. When running validate --locale ar, it checks if the ICU string contains the specific plural forms required by Arabic (zero, one, two, few, many, other).
Critique: "Populating Translation Memories". This usually means taking the extracted English, looking at the existing translations, and merging them.
Solution: Introduce a "Sync" workflow. 1. Extract new keys. 2. Compare with TM. 3. Update TM with new entries, mark obsolete ones.
7. Final Polish:
Use a technology-agnostic approach for the logic, but suggest Node.js/TypeScript for implementation due to the maturity of ICU tools in that ecosystem (intl-messageformat).
Define the data structures (JSON schema for the extracted catalog).
Let's write out the design based on these points. I will organize it into Architecture, CLI Commands, Validation Logic, and Data Formats.
Self-Correction during drafting: Make sure to explain what ICU is briefly for context, though the user likely knows. Focus on the "Design" aspect—how the pieces fit together.
Design Pattern: Use a Pipeline pattern.
Input -> Extract -> Transform -> Validate -> Load (TM).
Specific Validation Detail:
The plural rules logic.
Example: English (1 cat, 2 cats). one and other.
Russian (1 кошка, 2 кошки, 5 кошек). one, few, many.
The CLI must warn: "String 'num_cats' is missing plural category 'few' required for target locale 'ru'".
Let's generate the output.
Design Proposal: icu-localizer
This document outlines the architecture for a polyglot CLI tool designed to extract ICU-formatted messages, validate localization rules (plurals/genders), and manage Translation Memories (TM).
1. Core Architecture
The CLI will be built using Node.js/TypeScript (for native ICU support compatibility via intl-messageformat and ecosystem tools) or Go/Rust (for performance in large repos). For this design, we assume a Node.js architecture using an ESM module structure.
Error: If target is ru and is missing the few or many categories, raise PLURAL_RULE_INCOMPLETE.
C. Translation Memory (TM) & Population
The TM is a database of previously translated units used to leverage past work.
TM Storage Format:
We will use a flattened JSON structure optimized for lookups, or standard XLIFF 1.2.
Population Strategy (icu-localizer populate):
Deduplication: Check the Source Catalog against the TM.
Exact Match: If source_string exists in TM, copy translation to Locale File.
Fuzzy Match (Levenshtein Distance):
Calculate similarity ratio.
If > 0.8, mark translation as needs_review and copy.
Pseudo-Localization (Dev Mode):
If no translation exists and --pseudo is enabled:
Transform: Hello {name}→[Ĥêļļõ {name}] (expands string by 30% to test UI layout).
4. Data Schemas
Source Message Catalog (en-US.json)
json
{
"header.user_greeting": {
"description": "Greeting on the dashboard",
"message": "Hello {name}, you have {count, plural, one {# notification} other {# notifications}}.",
"filePath": "src/components/Header.tsx",
"line": 42
}
}
Locale File (de-DE.json)
json
{
"header.user_greeting": {
"message": "Hallo {name}, du hast {count, plural, one {# Benachrichtigung} other {# Benachrichtigungen}}.",
"status": "translated",
"updatedAt": "2023-10-27T10:00:00Z"
}
}
5. Implementation Roadmap
Phase 1: The Core (Extraction)
Implement extract command.
Support for JSON output.
Integration with @formatjs/ts-transformer for TypeScript extraction.
Phase 2: Validation Engine
Integrate intl-messageformat parser.
Implement CLDR plural rule checking logic.
Add validate CLI command with exit codes (0 for success, 1 for errors).
Phase 3: Ecosystem & TM
Implement populate command with JSON Memory support.
Add support for XLIFF export/import (for sending files to agencies).
Add CI/CD integration (GitHub Action).
6. Usage Example
bash
# 1. Extract all strings from src/ to lang/en.json
icu-localizer extract --src ./src --out ./lang/en.json
# 2. Validate German translations adhere to plural rules
icu-localizer validate ./lang/de.json --locale de
# 3. Fill German file using old translations from TM
icu-localizer populate ./lang/de.json --tm ./archive/translations.json