Installation
composer require cavernbay/translation-bundle
Add to config/bundles.php:
return [
// ...
CavernBay\TranslationBundle\CavernBayTranslationBundle::class => ['all' => true],
];
First Export Generate a CSV for all translations (English as reference):
php bin/console cavernbay:translation:export --reference-locale=en --output=translations.csv
Key flags:
--reference-locale: Base locale for comparison (e.g., en).--filter-bundle: Limit to specific bundles (e.g., AppBundle).--filter-domain: Limit to domains (e.g., messages).First Import
After translators update translations.csv, import back:
php bin/console cavernbay:translation:import translations.csv
Verify with:
php bin/console debug:translation
Translation Lifecycle
translations/ directory.Domain-Specific Exports
Export only frontend messages (domain: messages):
php bin/console cavernbay:translation:export \
--reference-locale=en \
--filter-domain=messages \
--output=frontend_translations.csv
Vendor Bundle Support
Include translations from installed bundles (e.g., FOSUserBundle):
php bin/console cavernbay:translation:export --include-vendors
Automated CI Pipeline
Add to composer.json scripts:
"scripts": {
"post-update-cmd": [
"php bin/console cavernbay:translation:export --reference-locale=en --output=translations.csv"
]
}
Trigger on composer update to keep CSV in sync.
Fallback Locale Handling
Configure fallback in config/packages/translation.yaml:
framework:
default_locale: en
translator:
fallbacks:
- en
CBTB respects this during exports/imports.
CSV Format Sensitivity
\t (not commas). Use --delimiter if needed:
php bin/console cavernbay:translation:export --delimiter=,
Locale Mismatches
--reference-locale doesn’t exist, the command fails. Pre-create locales in translations/:
mkdir -p translations/fr
touch translations/fr/messages.yaml
Vendor Overwrites Importing vendor translations replaces existing files. Backup first:
cp -r translations/ translations_backup/
YAML vs. CSV Conflicts
php bin/console cavernbay:translation:validate translations.csv
Symfony 6+ Quirks
CavernBayTranslationBundle is loaded before FrameworkBundle.dev if translations are missing. Use --no-debug:
php bin/console cavernbay:translation:import translations.csv --no-debug
Dry Run Imports Preview changes without writing files:
php bin/console cavernbay:translation:import translations.csv --dry-run
Log Missing Keys Check for untranslated entries:
php bin/console cavernbay:translation:check --locale=fr
Custom Exporters Extend the exporter to add metadata (e.g., context):
// src/Command/CustomTranslationExporter.php
use CavernBay\TranslationBundle\Command\ExportTranslationCommand;
use Symfony\Component\Console\Input\InputInterface;
class CustomTranslationExporter extends ExportTranslationCommand {
protected function configure() {
$this->setName('app:custom-export');
}
protected function execute(InputInterface $input, OutputInterface $output) {
$this->addMetadataToCsv(); // Custom logic
parent::execute($input, $output);
}
}
Git Ignore
Add to .gitignore to avoid committing generated CSVs:
/translations.csv
/*.csv
Performance For large projects, export in chunks by bundle:
find src -name "*.php" | xargs grep -l "__" | cut -d'/' -f2 | sort | uniq | while read bundle; do
php bin/console cavernbay:translation:export --filter-bundle=$bundle --output=$bundle.csv
done
How can I help you explore Laravel packages today?