Easily manage all your Laravel translation strings with powerful features:
- Translate strings into other languages using DeepL, OpenAI, or custom services.
- Proofread translations to fix grammar and syntax automatically (via OpenAI or custom services).
- Find missing translation strings across locales.
- Detect unused translation keys in your codebase.
- Sort translations in natural order.
- Installation
- Configuring the Driver
- Sorting and Formatting
- Automatic Translation
- Proofreading Translations
- Identifying Untranslated Translations
- Detecting Missing Translations
- Detecting Dead Translations
- Code Scanner Configuration
Install the package via Composer:
composer require elegantly/laravel-translator --dev
Add the following line to your .gitignore
file:
storage/.translator.cache
Publish the configuration file:
php artisan vendor:publish --tag="translator-config"
This package uses a driver-based architecture. By default, it supports two standard drivers: PHP and JSON. You can create custom drivers for alternative storage methods, such as a database.
Set the default driver in the configuration file:
use Elegantly\Translator\Drivers\PhpDriver;
return [
/**
* Possible values: 'php', 'json', or a class-string implementing Driver.
*/
'driver' => PhpDriver::class,
// ...
];
Sort translations with the default driver:
php artisan translator:sort
Specify a driver for sorting:
php artisan translator:sort --driver=json
Sort translations programmatically with the default driver:
use Elegantly\Translator\Facades\Translator;
Translator::sortTranslations(locale: 'fr');
Specify a driver:
Translator::driver('json')->sortTranslations(locale: 'fr');
Before translating, configure a translation service. The package supports:
- OpenAI
- DeepL
Custom translation services can also be implemented.
Define your OpenAI credentials in the configuration file or via environment variables:
return [
// ...
'services' => [
'openai' => [
'key' => env('OPENAI_API_KEY'),
'organization' => env('OPENAI_ORGANIZATION'),
'request_timeout' => env('OPENAI_REQUEST_TIMEOUT'),
],
],
// ...
];
Add your DeepL API key to the configuration file or environment variables:
return [
// ...
'services' => [
// ...
'deepl' => [
'key' => env('DEEPL_KEY'),
],
],
// ...
];
Translate untranslated French translations:
php artisan translator:untranslated en fr --translate
Translate using a specific driver:
php artisan translator:untranslated en fr --translate --driver=json
Add a new locale with translations:
php artisan translator:add-locale fr en --translate
Translate translations programmatically with the default driver:
Translator::translateTranslations(
source: 'en',
target: 'fr',
keys: ['validation.title', ...]
);
Specify a driver for translation:
Translator::driver('json')->translateTranslations(
source: 'en',
target: 'fr',
keys: ['My Title', ...]
);
Proofreading corrects grammar and syntax.
Currently, OpenAI is the only built-in service, but custom services can be implemented.
php artisan translator:proofread en
Proofread translations with the default driver:
Translator::proofreadTranslations(
locale: 'fr',
keys: ['auth.email', ...]
);
Specify a driver:
Translator::driver('json')->proofreadTranslations(
locale: 'fr',
keys: ['My Title', ...]
);
Find keys defined in one locale but missing in another.
php artisan translator:untranslated en fr
Translator::getUntranslatedTranslations(source: 'en', target: 'fr');
Missing translations are keys found in your codebase but missing in translation files.
Find the missing keys in your default driver:
php artisan translator:missing en
Specify a driver:
php artisan translator:missing en --driver=json
Add the missing keys to your driver:
php artisan translator:missing en --sync
Translator::getMissingTranslations(locale: 'en');
Dead translations are keys defined in your files but unused in your codebase.
php artisan translator:dead fr
Translator::getDeadTranslations(locale: 'fr');
Specify paths to scan for translation keys. By default, both .php
and .blade.php
files are supported.
return [
'searchcode' => [
'paths' => [
app_path(),
resource_path(),
],
],
];
Exclude irrelevant paths for optimized scanning, such as test files or unrelated directories.
Ignore specific translation keys:
return [
'searchcode' => [
'ignored_translations' => [
'countries', // Ignore keys starting with 'countries'.
],
],
];
Run tests using:
composer test
See the CHANGELOG for recent updates.
Check the CONTRIBUTING guide for details.
Report security vulnerabilities via GitHub or email.
This package is licensed under the MIT License. See the License File for more details.