@enterprise_search/translation
v0.8.66
Published
Moves between language file formats
Downloads
990
Readme
Translation Software
This package provides a framework for handling multi-language translations in projects. It supports loading and saving translation files in both JSON and CSV formats, validating configuration, and checking mismatches between translations.
Installation
npm install "@enterprise_search/translation"
Or if you want it from the command line
npm install -g "@enterprise_search/indexer"
From the command line
This requires a config file in the name
npm install -g "@enterprise_search/indexer"
index experiment translation-init # Createsan example translation.yaml file.
# Gives instructions on where files should be placed
index experiment translation # Turns csv files into json files
index experiment translation --invert # Turns json files into csv files
index experiment translation --help # For more information
Features
- Converts a csv file with langauges for columns into json suitable for i18n
- Can 'invert' the process to convert json files into csv files. This is useful if you have existing json files you want to consolidate and manage better
- Error Handling: Returns validation and processing errors, while 'doing as much as it can'
- Mismatch detection: checks for mismatchs across languages... places where something is defined in one language but not in another.
Important Files and Modules
config/config.ts
Holds the type definitions for the core entities:
- TranslationConfig: Describes the configuration for translations.
- CsvFilesDescription and JsonFilesDescription: Define the source and target file structures.
- Contains helper methods:
- validateTranslationConfig(config: TranslationConfig): Validates the configuration.
- invertTranslationConfig(config: TranslationConfig): Switches the source and target for reverse operations.
translate.ts
Core functions for performing translations:
- translateAllLanguageFiles: Translates all specified files in a configuration.
- _translateOneLanguageFile: Translates a single file, returning any errors or mismatches.
loaders and savers
- loadCsvLanguageFile: Loads translations from CSV files.
- saveCsvLanguageFile: Saves translations to CSV files.
- loadJsonLanguageFiles: Loads translations from JSON files.
- saveJsonLanguageFile: Saves translations to JSON files.
Example Usage
Sample Configuration: by default in a file called translation.yaml
source: # This is the source of truth for the language files
type: csv
directory: sample.translation/csv/{file}.csv # Note the {file} placeholder. Change this to your directory structure.
pathFieldName: path
target: # This is where we put the generated language files.
type: json
directory: sample.translation/json/{file}/{language}.json # Note the {file} and {language} placeholders. Change this to your directory structure.
languages: # This is the list of languages we want to generate
- de
- en
languageFiles: # And for each language, we want to generate these files
- about-us
- room-booking
- translation```
Translation Workflow
async function runTranslation(config: TranslationConfig) {
// Validate configuration
const validate = defaultTranslationOperations.validateTranslationConfig(config);
if (hasErrors(validate)) {
console.error('Configuration errors:', validate);
return;
}
// Perform translations
try {
const errors = await translateAllLanguageFiles(defaultTranslationOperations, config);
if (errors.length) {
console.error('Translation errors:', errors);
} else {
console.log('Translations completed successfully!');
}
} catch (error) {
console.error('Unexpected error:', error.message);
}
}
runTranslation(config);
License
MIT License. See the LICENSE.md file for more details.
ChatGPT session
This is the session where we wrote the software. https://chatgpt.com/share/674b0a46-f4e4-8013-9869-4ccd016a0209