oss-tools / laravel-auto-translate
Automatically translate your language files using Google Translate
Requires
- php: ^8.0.2
- google/cloud: ^v0.196.0
- illuminate/support: ^9.0 || ^10.0
- oss-tools/laravel-langman: ^2.0
- oss-tools/laravel-libretranslate: ^1.0.0
- scn/deepl-api-connector: ^2
- stichoza/google-translate-php: ^5.0.1
Requires (Dev)
- mockery/mockery: ^1.4.0
- orchestra/testbench: ^7.0 || ^8.0
- phpunit/phpunit: ^9.5.10 || ^10.1
README
With this package you can translate your language files using a translator service. The package ships with a free Google Translate version, Google Translate API and Deepl.
Specify a source language and a target language and it will automatically translate your files. This is useful if you want to prototype something quickly or just a first idea of the translation for later editing. The package ships with two artisan commands. One for translating all the missing translations that are set in the source language but not in the target language. The other one for translating all source language files and overwriting everything in the target language.
Note
This package is a detached fork of ben182/laravel-auto-translate.
Installation
This package can be used in Laravel 5.6 or higher and needs PHP 7.2 or higher.
You can install the package via composer:
composer require oss-tools/laravel-auto-translate
Config
After installation publish the config file:
php artisan vendor:publish --provider="OSSTools\AutoTranslate\AutoTranslateServiceProvider"
You can specify your source language, the target language(s), the translator and the path to your language files in there.
Translators
If you have lots of translations to make I recommend Google Cloud Translate or Deepl. They are fast, reliable and you will not encounter any rate limiting.
Usage
Missing translations
Simply call the artisan missing command for translating all the translations that are set in your source language, but not in your target language:
php artisan autotrans:missing
E.g. you have English set as your source language. The source language has translations in auth.php:
<?php return [ 'failed' => 'These credentials do not match our records.', 'throttle' => 'Too many login attempts. Please try again in :seconds seconds.', ];
Your target language is German. The auth.php file has the following translations:
<?php return [ 'failed' => 'Diese Kombination aus Zugangsdaten wurde nicht in unserer Datenbank gefunden.', ];
The artisan missing command will then translate the missing auth.throttle
key.
All translations
To overwrite all your existing target language keys with the translation of the source language simply call:
php artisan autotrans:all
This will overwrite every single key with a translation of the equivalent source language key.
Parameters
Sometimes you have translations like these:
'welcome' => 'Welcome, :name',
They can be replaced with variables. When we pass these placeholders to a translator service, weird things can happen. Sometimes the placeholder comes back in upper-case letters or it has been translated. Thankfully the package will respect your variable placeholders, so they will be the same after the translation.
Extending
You can create your own translator by creating a class that implements \OSSTools\AutoTranslate\Translators\TranslatorInterface
. Simply reference it in your config file.
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.