CLI & Workflow
This chapter explains how to run translations from the command line, what happens step-by-step, and how caches make subsequent runs incremental.
Prerequisites
-
Composer installed and vendor deps present.
-
.env has Azure Translator credentials:
AZURE_KEY=... AZURE_REGION=... AZURE_ENDPOINT=https://api.cognitive.microsofttranslator.com -
Configuration file (e.g.,
translate.config.php) is filled out (see the Configuration chapter).
Entry points
Docara CLI
Run the bundled command directly:
php vendor/bin/docara translate
Autoloading stays in the CLI; you don't need to require
vendor/autoload.phpyourself.
Optional Composer script
If you want a shorter alias, add this to composer.json:
"scripts": {
"translate": "php vendor/bin/docara translate"
}
Then run composer translate.
What happens when you run it
- Boot: load
.env, readtranslate.config.phpand projectconfig.php. - Build CommonMark env: install Custom Tags extension to parse Markdown safely.
- Scan source: locate the base tree
source/_docs-<target_lang>. - Plan targets: for each
langinlanguages, ensure/preparesource/_docs-<lang>. - Collect strings:
- Markdown: traverse AST, collect Text nodes (code blocks/inline code/tags remain intact).
- Front matter: collect only keys listed in
frontMatter. - Language packs: mirror
.lang.php/.settings.php.
- Cache check: normalize → hash → lookup in
<cache_dir>/translations/translate_<lang>.json. - Batch & throttle: group untranslated strings (~9k chars/batch) and send to Azure; respect chars-per-minute with jitter.
- Apply translations: replace bottom-up by line ranges to avoid shifting positions.
- Write output: update/create files under
source/_docs-<lang>. - Persist caches: update
translate_<lang>.json,hash.json, and.config.json(locales map).
Incremental behavior
- Cache ensures idempotency: unchanged strings are skipped.
- Re-running after small edits only translates new/changed text.
- To force retranslate a specific string, remove its entry from
<cache_dir>/translations/translate_<lang>.json(or delete the file for a clean slate).
Output & cache layout
Relative to project root (main):
!folders
- source
- {$DOCS_DIR}
- {$lang}
- {$DOCS_DIR}
- {$CACHE_DIR}
- translations -- translate_{$lang}.json (key→translated string) -- hash.json (bookkeeping) -- .config.json (locales map, read by Docara beforeBuild)
!endfolders
Docara build integration
During beforeBuild, if <cache_dir>/translations/.config.json exists, it merges into config('locales') so new languages appear in the same build run.
Logs & exit codes
- CLI emits summary per language (discovered strings, translated count, reused from cache).
- Non-zero exit means unrecoverable error (invalid config, provider error, I/O issues).
Provider errors (Azure)
- Check HTTP status/body; retry 429/5xx.
- If a batch fails, report which file/chunk caused it and continue when safe.
Common workflows
First full pass
php vendor/bin/docara translate
Generates _docs-ru, fills caches, writes .config.json.
After content edits
php vendor/bin/docara translate
Only new/changed strings are sent; everything else comes from cache.
Add a new language
- Add it to
languagesintranslate.config.php. - Run
php vendor/bin/docara translate. - Docara picks it up via
.config.jsonduring build.
Tips
- Keep
cache_diraligned with the path used in corebootstrap.phpfor.config.json. - Use absolute paths in config to avoid CWD issues in CI.
- Watch chars/minute limits if you bulk-edit many files.
Troubleshooting
- No new locale in the site: ensure
.config.jsonis under<cache_dir>/translations/and the bootstrap merges it. - Markup broken: verify only text nodes are replaced; check for manual post-processing that might alter HTML.
- Attributes translated: confirm you aren’t translating inside tag attributes; only text nodes should be collected.
- Autoload errors: run from the project root so
php vendor/bin/docara translatefindsvendor/autoload.php.