elegantly / laravel-translator
All on one translations management for Laravel
Fund package maintenance!
ElegantEngineeringTech
Installs: 2 787
Dependents: 0
Suggesters: 0
Security: 0
Stars: 31
Watchers: 1
Forks: 2
Open Issues: 1
Requires
- php: ^8.2
- deeplcom/deepl-php: ^1.7
- illuminate/contracts: ^10.0||^11.0||^12.0
- nikic/php-parser: ^5.1
- openai-php/laravel: ^0.11
- spatie/laravel-package-tools: ^1.16
- spatie/simple-excel: ^3.7
- symfony/finder: ^6.0||^7.0
- symfony/intl: ^7.2
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^8.22.0||^9.0.0||^10.0.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
- dev-main
- v2.4.6
- v2.4.5
- v2.4.4
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.5
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.2
- v2.0.1
- v2.0.0
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.15
- v0.0.14
- v0.0.13
- v0.0.12
- v0.0.11
- v0.0.10
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- dev-dev
This package is auto-updated.
Last update: 2025-03-07 18:04:53 UTC
README
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.
- Import & Export translations in a CSV file.
Try Laratranslate – A Powerful UI for Managing Translations
Table of Contents
- How does it work?
- Installation
- Configuring the Driver
- Configuring the Locales
- Configuring the Code Scanner
- Sorting and Formatting
- Automatic Translation
- Proofreading Translations
- Identifying Untranslated Translations
- Detecting Missing Translations
- Detecting Dead Translations
- Export to a CSV
- Import from a CSV
How does it work?
This package will directly modify your translation files like /lang/en/messages.php
or /lang/fr.json
for example.
Both PHP
and JSON
files are supported.
Advanced features like dead translations detection will scan your entire codebase to find unused translation strings.
Installation
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"
Configuring the Driver
This package uses a driver-based architecture. By default, it supports two standard drivers: PHP and JSON.
- Use the
PHP
driver if you store your translation strings in.php
files, such as/lang/en/message.php
. - Use the
JSON
driver if you store your translation strings in.json
files, such as/lang/fr.json
.
You can also 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, // ... ];
Note
All features are supported in both the PHP and JSON drivers.
Configuring the Locales
Automatic Detection
By default, this package will attempt to determine the locales defined in your application by scanning your lang
directory.
You can customize this behavior in the configuration file.
use Elegantly\Translator\Support\LocaleValidator; return [ // ... 'locales' => LocaleValidator::class, // ... ];
Manual Setup
To set the locales manually, use the following configuration:
return [ // ... 'locales' => ['en', 'fr', 'es'], // ... ];
Configuring the Code Scanner
Service: searchcode
.
Features:
Both the detection of dead and missing translations rely on scanning your code.
- Missing translations are keys found in your codebase but missing in translation files.
- Dead translations are keys defined in your translation files but unused in your codebase.
Requirements
At the moment, this package can only scan the following files:
.php
.blade.php
Note
If you use a React or Vue frontend, it would not be able to scan those files, making this feature irrelevant.
The default detector uses nikic/php-parser
to scan all your .php
files, including the Blade ones.
In order to be able to detect your keys, you will have to use one of the following Laravel function:
__(...)
,trans(...)
trans_choice(...)
\Illuminate\Support\Facades\Lang::get(...)
\Illuminate\Support\Facades\Lang::has(...)
\Illuminate\Support\Facades\Lang::hasForLocale(...)
\Illuminate\Support\Facades\Lang::choice(...)
app('translator')->get(...)
app('translator')->has(...)
app('translator')->hasForLocale(...)
app('translator')->choice(...)
Or one of the following Laravel Blade directive:
@lang(...)
Here is some example of do's and don'ts:
__('messages.home.title'); // ✅ 'messages.home.title' is detected foreach(__('messages.welcome.lines') as $line){ // ✅ 'messages.welcome.lines' and all of its children are detected. } $key = 'messages.home.title'; __($key); // ❌ no key is detected
Included Paths
Specify paths to scan for translation keys. By default, both .php
and .blade.php
files are supported.
return [ 'searchcode' => [ 'paths' => [ app_path(), resource_path(), ], ], ];
Excluded Paths
Exclude irrelevant paths for optimized scanning, such as test files or unrelated directories.
return [ 'searchcode' => [ 'excluded_paths' => [ 'tests' ], ], ];
Ignored Translation Keys
Ignore specific translation keys:
return [ 'searchcode' => [ 'ignored_translations' => [ 'countries', // Ignore keys starting with 'countries'. ], ], ];
Sorting and Formatting
CLI Commands
Sort translations with the default driver:
php artisan translator:sort
Specify a driver for sorting:
php artisan translator:sort --driver=json
Using Code
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');
Automatic Translation
Service: translate
.
Before translating, configure a translation service. The package supports:
- OpenAI
- DeepL
Custom translation services can also be implemented.
Configuring OpenAI
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'), ], ], // ... ];
Configuring DeepL
Add your DeepL API key to the configuration file or environment variables:
return [ // ... 'services' => [ // ... 'deepl' => [ 'key' => env('DEEPL_KEY'), ], ], // ... ];
CLI Translation
Display all keys defined in the source locale (English) but not translated in the target (French):
php artisan translator:untranslated en fr
Translate untranslated English strings into French:
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 (French) with their translations from a source (English):
php artisan translator:add-locale fr en --translate
Programmatic Translation
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 Translations
Service: proofread
.
Proofreading corrects the grammar and syntax of your translation strings.
Currently, OpenAI is the only built-in service, but custom services can be implemented.
To configure OpenAI, see Configuring OpenAI.
CLI Proofreading
Proofread all strings in the target locale (English).
php artisan translator:proofread en
Programmatic Proofreading
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', ...] );
Identifying Untranslated Translations
Find keys defined in one locale but missing in another.
CLI Usage
Display all keys defined in the source locale (English) but not in the target locale (French).
php artisan translator:untranslated en fr
Programmatic Usage
Translator::getUntranslatedTranslations(source: 'en', target: 'fr');
Detecting Missing Translations
Service: searchcode
.
Configuration: Configuring the Code Scanner
Missing translations are keys found in your codebase but missing in translation files.
CLI Usage
Find keys defined in your codebase but missing in your locale (English) using 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
Programmatic Usage
Translator::getMissingTranslations(locale: 'en');
Detecting Dead Translations
Service: searchcode
.
Configuration: Configuring the Code Scanner
Dead translations are keys defined in your locale (English) but unused in your codebase.
CLI Usage
php artisan translator:dead en
Programmatic Usage
Translator::getDeadTranslations(locale: 'fr');
Export to a CSV
Service: exporter
Export all your translation strings to a CSV file in the following format:
Key | English | French |
---|---|---|
messages.auth.login | Login | Connexion |
CLI Usage
php artisan translator:export /path/to/my/file.csv
Programmatic Usage
$path = Translator::exportTranslations('/path/to/my/file.csv');
Import from a CSV
Service: exporter
Import translation strings from a CSV file. Ensure your CSV follows the format below:
Key | English | French |
---|---|---|
messages.auth.login | Login | Connexion |
CLI Usage
php artisan translator:import /path/to/my/file.csv
Programmatic Usage
$translations = Translator::importTranslations('/path/to/my/file.csv');
Testing
Run tests using:
composer test
Changelog
See the CHANGELOG for recent updates.
Contributing
Check the CONTRIBUTING guide for details.
Security Vulnerabilities
Report security vulnerabilities via GitHub or email.
Credits
License
This package is licensed under the MIT License. See the License File for more details.