php-translation/extractor
Extracts translation messages from PHP projects for the php-translation ecosystem. Scans source code and templates to collect translatable strings and generate catalogs, helping keep i18n files in sync across frameworks and custom apps.
Installation
composer require php-translation/extractor:^2.3.2
Add to composer.json under require-dev if only needed for development:
"require-dev": {
"php-translation/extractor": "^2.3"
}
Basic CLI Usage Extract translations from a Laravel project:
vendor/bin/extractor extract ./app --output=resources/lang/en/extracted.json
First Use Case
Run extraction on your app/ directory and verify the output JSON matches expected strings (e.g., __('key') calls). Compare with existing translation files to identify missing keys.
CI/CD Pipeline
Add to phpunit.xml or GitHub Actions:
<php>
<env key="TRANSLATION_EXTRACTOR" value="1"/>
</php>
Trigger extraction before merging to main:
- name: Extract translations
run: vendor/bin/extractor extract ./app --output=temp/extracted.json
Laravel-Specific Patterns
--blade flag to extract __() calls in views:
vendor/bin/extractor extract ./resources/views --blade --output=lang/en/views.json
config('app.name')) with --config:
vendor/bin/extractor extract ./app/Providers --config --output=lang/en/config.json
Incremental Updates
Use --update to append new strings to existing files:
vendor/bin/extractor extract ./app --output=lang/en/app.json --update
Custom Extractors
Extend functionality via ExtractorInterface:
use PhpTranslation\Extractor\ExtractorInterface;
class CustomExtractor implements ExtractorInterface {
public function extract(string $file): array {
// Custom logic (e.g., parse Markdown files)
return ['custom.key' => 'Custom Value'];
}
}
Register in composer.json:
"extra": {
"extractor": ["CustomExtractor"]
}
False Positives
User::class or Route::get().--ignore to exclude directories/files:
vendor/bin/extractor extract ./app --ignore=./app/Models --output=lang/en/extracted.json
Blade Cache
__() calls in cached Blade files.php artisan view:clear
vendor/bin/extractor extract ./resources/views --blade --output=lang/en/views.json
Dynamic Keys
__("Hello {$name}")) as separate keys.--dynamic to group dynamic keys:
vendor/bin/extractor extract ./app --dynamic --output=lang/en/dynamic.json
Namespace Collisions
auth.login may conflict with Laravel’s default translations.vendor/bin/extractor extract ./app --namespace=app. --output=lang/en/app.json
vendor/bin/extractor extract ./app --dry-run
vendor/bin/extractor extract ./app --verbose
vendor/bin/extractor extract ./app --output=lang/en/extracted.json 2> extraction.log
Custom Formats Output translations in YAML/JSON5:
vendor/bin/extractor extract ./app --output=lang/en/extracted.yaml --format=yaml
Pre-Processors
Use --pre-process to transform files before extraction (e.g., minify JS):
vendor/bin/extractor extract ./resources/js --pre-process="node --compile" --output=lang/en/js.json
Post-Processors
Chain with jq to reformat output:
vendor/bin/extractor extract ./app --output=- | jq '.' > lang/en/extracted.json
# Ensure your local PHP version matches your server's (e.g., 8.2)
php -v
vendor/bin/extractor extract ./app --output=lang/en/extracted.json
How can I help you explore Laravel packages today?