elegantly / laravel-translator
All on one translations management for Laravel
Fund package maintenance!
ElegantEngineering
Requires
- php: ^8.2
- deeplcom/deepl-php: ^1.7
- illuminate/contracts: ^10.0||^11.0
- openai-php/laravel: ^0.10.1
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
README
Laravel Translator - All in one translations file manager
Manage all your laravel translations easily:
- Sort tranlations array in natural order
- Find missing translations strings
- Translate automatically string to other languages (DeepL, OpenAI or any custom service)
- Fix Grammar and syntax in your translations (OpenAI, or any custome service)
- Format your translations files
Installation
You can install the package via composer:
composer require-dev elegantly/laravel-translator
You can publish the config file with:
php artisan vendor:publish --tag="translator-config"
This is the contents of the published config file:
return [ 'lang_path' => lang_path(), 'translate' => [ 'service' => 'deepl', 'services' => [ 'deepl' => [ 'key' => env('DEEPL_KEY'), ], 'openai' => [ 'model' => 'gpt-4o', 'prompt' => "Translate the following json to the locale '{targetLocale}' while preserving the keys.", ], ], ], 'grammar' => [ 'service' => 'openai', 'services' => [ 'openai' => [ 'model' => 'gpt-4o', 'prompt' => ' Fix the grammar and the syntax the following json string while preserving the keys. Do not change the meaning or the tone of the sentences and never change the keys. ', ], ], ], ];
Usage
This package can be used:
- Like a CLI tool, using commands.
- In a programmatic way using
\Elegantly\Translator\Facades\Translator::class
facade class.
Sort all translations in natural order
You can format and sort all your php translations files using:
php artisan translator:sort
use Elegantly\Translator\Facades\Translator; Translator::sortAllTranslations();
Find the missing translations
You can display all the missing translations present in a given locale but not in the other ones using:
php artisan translator:missing fr
use Elegantly\Translator\Facades\Translator; Translator::getAllMissingTranslations('fr');
Auto translate strings
This package can automatically translate your files for you. It includes 2 services right now:
- DeepL
- OpenAI
You can also define your own service.
Auto-translate using DeepL
First, you need to edit the config file to add your DeepL api key and select deepl as your service:
return [ 'translate' => [ 'service' => 'deepl', // select the default service here 'services' => [ 'deepl' => [ 'key' => env('DEEPL_KEY'), // add you api key here ], ], ], ]
To translate all the missing translations use:
php artisan translator:translate --from=fr --to=en
To translate all translations use:
php artisan translator:translate --from=fr --to=en --all
Ommitting the --to
option will translate to every available languages in your project.
use Elegantly\Translator\Facades\Translator; Translator::translateTranslations( referenceLocale: 'fr', targetLocale: 'en', namespace: 'namespace-file', keys: ['title', ...] );
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.