CLI tool for synchronizing localization resources with Crowdin service.
yarn add --dev trezor-translations-manager
This package does not deal with the process of extracting messages for translation from your source code.
For React apps it is recommended to use babel-plugin-react-intl. Then with ttm merge-msgs
you can merge extracted messages to a single JSON file.
Example configuration in the babel.config.js
[
'react-intl',
{
messagesDir: './translations/extractedMessages/',
extractSourceLocation: true,
},
],
You can also build your own custom extraction process. Just make sure that the JSON file containing the messages matches following format:
[
{
"source": "Source string that is to be translated to target language",
"meta": {
"comment": "Additional information for translators",
"occurrences": [
"Path to file that containts the message"
]
},
...
]
This package also serves as CLI tool. Configuration is stored in a JSON file. By default it will try to parse l10n.config.json
in the root of your project. You can also specify the path to your configuration file via command line option --config path
.
$ ttm <command>
Options
--config, -c Config file
--version, -v Show ttm version
Commands
merge-msgs
- merges messages exported by babel-plugin-react-intl to single JSON file
build-csv
- converts JSON file to CSV
upload
- uploads CSV file to Crowdin
build-translations
- builds translations in Crowdin
export-translations
- downloads translations and generates locales
You can automatize running this tool by creating a new script entry in your project's package.json
:
{
"scripts": {
"translations-sync": "yarn ttm merge-msgs && yarn ttm build-csv && yarn ttm upload && yarn ttm build-translations && yarn ttm export-translations",
}
}
Now you are able to run the whole flow with yarn run translations-sync
.
extractedMessagesFilePattern
: Glob pattern used to look for React Intl messages that were extracted via babel-plugin-react-intl.outputDir
- Directory which contains JSON file with messages to translate.localesOutputDir
: Output directory where lang-specific json files will be generated.project.identifier
: Crowdin identifier of projectproject.filename
: Name of the file inoutputDir
with extracted messages (without an extension)project.apiKeyEnv
: Name of an environment variable which contains API keylanguages
: Crowdin language codes. Codes are also used as filenames for generated lang-specific JSON files with translations. Be aware that they do not have to match codes returned bynavigator.language
.langToFileNameMap
: Object of language:filename pairs. Allows to overrides default filenames of generated locales JSON files
l10n.config.json
example
{
"extractedMessagesFilePattern": "translations/extractedMessages/**/*.json",
"localesOutputDir": "translations/locales/",
"outputDir": "translations/",
"project": {
"identifier": "trezor-wallet-test",
"filename": "master",
"apiKeyEnv": "CROWDIN_API_KEY"
},
"languages": ["af", "ar", "bn", "zh-CN", "zh-TW", "cs", "nl", "en", "et", "fr", "de", "el", "he", "hi", "hu", "id", "it", "ja", "ko", "no", "fa", "pl", "pt-PT", "ru", "es-ES", "sv-SE", "tr", "uk", "vi"],
"langToFileNameMap": { "en": "en", "bn": "bn", "cs": "cs", "de": "de", "el": "el", "es-ES": "es", "fr": "fr", "id": "id", "it": "it", "ja": "ja", "nl": "nl", "pl": "pl", "pt-PT": "pt", "ru": "ru", "uk": "uk", "zh-CN": "zh", "zh-TW": "zh_TW"}
}
If the CLI tool does not suit your needs you can build your own script with these exported functions.
import { mergeMessages, buildCSV, buildLocales } from 'trezor-translations-manager';
import Crowdin from 'trezor-translations-manager/lib/services/crowdin';
// projectId - Identifier of a Crowdin project
// apiKey - API key for the Crowdin project
const crowdin = new Crowdin(projectId, apiKey);
// available methods are addFile, updateFile, downloadTranslationsZip, buildTranslations, exportTranslations,
Aggregates the default messages that were extracted from the app's React components via the React Intl Babel plugin. The result is a flat collection of translation_key: Object
pairs for the app's default locale.
filePattern
: Pattern used to look for React Intl messagesoutputFilePath
: Path for outputed JSON fileallowDuplicates
: Iffalse
an error will be thrown if there are messages in different components that use the sameid
Builds CSV file from JSON with merged messages. Format of an input file is same as format of the file created by mergeMessages():
inputFilePath
: JSON file containing merged messagesoutputFilePath
: Path for outputed CSV filelanguages
: Array of Crowdin language codes that are appended to CSV header
Build language json files from a translated CSV file (downloaded from Crowdin)
inputFilePath
: Path to the CSV file downloaded from Crowdin.outputDir
: Directory where locales should be generated.languages
: Array of Crowdin language codes. Used for naming generated JSON locales files.langToFileNameMap
: Object of language:filename pairs. Allows to overrides default filenames of generated locales JSON filesdeleteCSV
: Whether should delete the csv file after. Defaulttrue
.
Creates a new branch in Crowdin service
branch
: Crowdin branch name
Upload a new csv file to Crowdin service
filePath
: Path to a filescheme
: Crowdin-specific CSV scheme (See https://support.crowdin.com/api/add-file/)branch
: Crowdin branch name
Update an existing csv file in Crowdin service
filePath
: Path to a filebranch
: Crowdin branch name
Builds translations in Crowdin service. It does not actually download anything. This method can be invoked only once per 30 minutes (otherwise the build process is skipped).
branch
: Crowdin branch name
Downloads a ZIP file with translations.
branch
: Crowdin branch name
Downloads a ZIP file with translations and extracts files to exportDir
exportDir
: Directory where files from the archive should be extracted.branch
: Crowdin branch name
To build a lib run yarn build
yarn run lint