3.9 KiB
Translation Library Overview (translate.ts)
Description
The translate.ts module serves as the core orchestration layer for the i18n package. It handles:
- Parsing and validating configuration options.
- Determining the translation mode (text vs. file).
- Resolving file paths and glob patterns.
- Dispatching files to appropriate translators based on file extension.
- Managing API interactions via the DeepL integration.
Key Exports
translate(opts: IOptions)
The main entry point for the translation process.
- Parameters:
opts- Configuration object containing source paths, destination languages, API keys, etc. - Logic Flow:
- Validates required options (
api_key,src/text,dstLang). - Resolves root paths and environmental variables.
- Iterates through target languages (
dstLang). - If
opts.textis present: CallstranslateDeepLdirectly for string translation. - If
opts.srcis present: Usesp-mapto process files concurrently.
- Validates required options (
translateFiles(file, targets, options)
Handles the translation of a specific file for a set of target languages.
- Parameters:
file: Source file path.targets: Array of target file paths/languages.options: Global configuration options.
- Logic Flow:
- Identifies the correct translator using
getTranslator(file). - Checks for dry-run mode (
options.dry). - Executes the translation for each target concurrently.
- Identifies the correct translator using
getTranslator(file: string)
Utility function that returns the appropriate translator function based on the file extension.
Supported File Types
The module uses a TRANSLATORS map to route files to specific handlers:
| Extension | Handler | Description |
|---|---|---|
.md, .html |
translateMarkup |
Handles Markdown and HTML content preservation. |
.json |
translateJSON |
Translates JSON values while preserving keys. |
.toml |
translateTOML |
Translates TOML configuration files. |
.yaml |
translateYAML |
Translates YAML configuration files. |
.xlsx, .xls |
translateXLS |
Handles Excel spreadsheet translation. |
.txt |
translateText |
Simple text file translation. |
Dependencies
p-map: Used to manage concurrency for file processing and language iteration (limited to concurrency: 1 by default intranslateFiles).path: For filesystem path resolution.@polymech/commons: specificallyglobBasefor handling glob patterns in source paths.
Glossary System
The package supports term consistency through a glossary system integrated with DeepL's glossary feature.
1. Storage Structure
Glossaries are stored locally in the configured storeRoot (default: ${OSR_ROOT}/i18n-store):
- CSV Data:
<storeRoot>/glossary/<SRC_LANG>/<DST_LANG>/glossary.csvcontains the term pairs. - Metadata:
<storeRoot>/glossary/<SRC_LANG>/<DST_LANG>/glossary.jsontracks theglossaryId, creation time, and content hash.
2. Synchronization Logic
To ensure the local glossary matches the one used by DeepL, the system checks for consistency before every translation request:
- Hash Verification: It calculates an MD5 hash of the local CSV file and compares it with the hash stored in the metadata file.
- Auto-Update:
- If the hashes differ (indicating local changes) or if the glossary is missing on the remote, it triggers an update.
- Re-creation: The old glossary is deleted from DeepL, and a new one is created using the current CSV content.
- Metadata Update: The new
glossaryIdand hash are saved locally.
3. Usage
When translateDeepL is called, it automatically:
- Attempts to resolve/update the glossary for the requested language pair.
- Injects the
glossary_idinto the DeepL API request options if a valid glossary exists.