This package add artisan commands to scan/export/import translations.
webo3/laravel-translator is a Laravel package for this package add artisan commands to scan/export/import translations..
It currently has 1 GitHub stars and 2.060 downloads on Packagist (latest version v2.0.4).
Install it with composer require webo3/laravel-translator.
Discover more Laravel packages by webo3
or browse all Laravel packages to compare alternatives.
Last updated
A Laravel package that provides artisan commands to scan, export, import and auto-translate translation strings. It extracts translation keys from your source code, manages JSON language files, supports CSV and XLIFF workflows for working with translators, and can automatically translate using the Google Translate API.
| Function | File types | Example |
| --- | --- | --- |
| __() | PHP, JS, TS, Vue | __('Hello world') |
| Lang::get() | PHP | Lang::get('messages.welcome') |
| @lang() | PHP (Blade) | @lang('Please login') |
| $t() | JS, TS, Vue | $t('Vue key') |
| .t() | JS, TS, Vue | i18n.t('My key') |
__('key')__("key")__('It\'s a test') correctly extracts It's a test__(`multi-line key`)composer require webo3/laravel-translator
Package auto-discovery is enabled for Laravel >= 5.5. For older versions, register the provider manually in config/app.php:
'providers' => [
webO3\Translator\Providers\TranslatorServiceProvider::class,
],
Publish the configuration file to specify which languages to manage:
php artisan vendor:publish --provider="webO3\Translator\Providers\TranslatorServiceProvider" --tag=config
This creates config/webo3-translator.php with the following options:
return [
// Languages to manage
'languages' => ['en', 'fr'],
// Where JSON language files are stored
'lang_path' => resource_path('lang'),
// Where export files are written (null = same as lang_path)
'export_path' => null,
// Directories to scan for translation keys
'scan_paths' => [app_path(), resource_path()],
// Export format: 'csv' or 'xliff'
'format' => 'csv',
// Translation driver for auto-translate: 'google_free'
'translation_driver' => 'google_free',
// Driver-specific options
'translation_options' => [],
];
Scans *.php, *.js, *.ts and *.vue files in the configured scan_paths for translation function calls. Extracts unique keys and creates/updates a JSON file for each configured language.
php artisan translations:scan
// and /* */) are stripped before scanning, but // inside strings (e.g. URLs) is preservedAdd the --clean option to detect and remove translation keys that are no longer used in your source files:
php artisan translations:scan --clean
This will scan your code, then compare existing keys in each language JSON file against the keys found in source. Unused keys are listed in a table and you are prompted for confirmation before they are removed.
Reads all language JSON files and exports them in the configured format.
php artisan translations:export
Exports a single translations.csv file with a UTF-8 BOM for Excel compatibility:
| key | en | fr | | --- | --- | --- | | Hello world | Hello world | Bonjour le monde | | Goodbye | Goodbye | Au revoir |
Untranslated values (where the value equals the key) appear as empty cells.
Set 'format' => 'xliff' in your config. Exports one XLIFF 1.2 file per target language (e.g. translations-fr.xlf). The first language in your languages array is used as the source language.
XLIFF is the industry standard format supported by professional translation tools (SDL Trados, memoQ, Phrase, Crowdin, etc.).
Reads the exported file(s) and merges the translated values back into the JSON language files.
php artisan translations:import
format config option (same as export)Automatically translates untranslated keys using a translation API and saves the results directly to your JSON language files.
First, install the Google Translate package:
composer require stichoza/google-translate-php
Then run:
php artisan translations:translate
This translates all untranslated keys (where value equals the key) from the source language to every other configured language.
| Option | Description |
| --- | --- |
| --lang=fr,es | Only translate specific target languages |
| --force | Re-translate keys that already have translations |
| --driver=google_free | Override the translation driver from config |
| --batch-size=50 | Number of strings per API call (default: 50) |
The command automatically preserves Laravel and Vue placeholders during translation:
:name, :count, :attribute{name}, {count}, {item}These are masked before sending to the API and restored in the translated result.
Keys that differ only in letter case (e.g. "Hello", "hello", "HELLO") are deduplicated into a single API call. The translated result is then adjusted to match each variant's casing (uppercase, lowercase, title case).
Two config keys control auto-translation:
// Translation driver: 'google_free'
'translation_driver' => 'google_free',
// Driver-specific options (reserved for future drivers)
'translation_options' => [
// 'api_key' => env('TRANSLATION_API_KEY'),
],
You can register your own translation driver by binding it in the service container:
$this->app->singleton('translation.driver.my_driver', function () {
return new MyCustomDriver(); // must implement TranslatorDriverInterface
});
Then use it with --driver=my_driver or set 'translation_driver' => 'my_driver' in config.
__('key'), $t('key'), etc.php artisan translations:scan to extract all keysphp artisan translations:translate to translate via APIphp artisan translations:exportphp artisan translations:importInstall the vue-i18n package:
npm install vue-i18n --save-dev
Set up vue-i18n in your app.js:
import { createI18n } from 'vue-i18n';
import en from '../lang/en.json';
import fr from '../lang/fr.json';
const i18n = createI18n({
locale: 'en',
messages: { en, fr }
});
app.use(i18n);
Then use $t() or i18n.t() in your templates:
<a :title="$t('Hello world')">{{ $t('Hello world') }}</a>
composer test # Run tests without coverage
composer test-coverage # Run tests with coverage report
MIT