sysoce / laravel-translation
Provides a translation dictionary layer between a translation service and your Laravel application.
Installs: 15
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/sysoce/laravel-translation
Requires
- php: >=7.0
Requires (Dev)
- fzaninotto/faker: ^1.8
- orchestra/testbench: ~3.5
- phpunit/phpunit: ^7.2
This package is auto-updated.
Last update: 2025-10-12 21:16:11 UTC
README
This package provides a translation dictionary layer between a translation service and your Laravel application. The dictionary layer can be used to avoid cost and speed up reoccurring translations. When you translate text through a translation service the translation is saved to your database and will be used instead of the translation service next time you translate the same text.
Installation
Laravel
This package can be used in Laravel 5.4 or higher.
In Laravel 5.5 the service provider will automatically get registered. In older versions of the framework just add the service provider in config/app.php file:
'providers' => [ // ... Sysoce\Translation\TranslationServiceProvider::class, ];
You can publish the migration with:
php artisan vendor:publish --provider="Sysoce\Translation\TranslationServiceProvider" --tag="migrations"
After the migration has been published you can create the translations table by running the migration:
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --provider="Sysoce\Translation\TranslationServiceProvider" --tag="config"
Usage
The Client
To use this package you need to connect to a translation service. This package provides a wrapper for Google Cloud Translate out of the box. To use this translation client you need to install the Google Cloud Translate package:
composer require google/cloud-translate
Enter the path to your JSON key file obtained from the Google Cloud Console in the published config/translation.php config file or simply specify the path in your .env file:
/* * If the client requires a key file, enter its path below. */ 'keyFilePath' => env('GOOGLE_APPLICATION_CREDENTIALS', ''),
You can create a wrapper for other Translation services by implementing the Sysoce\Translation\Contracts\Client contract.
Using the translation dictionary layer
To use the translation dictionary layer
// Set source language for translation app(Translation::class)->setSource('en'); // Set target language for translation app(Translation::class)->setTarget('ja'); // Translate text $translation = app(Translation::class)->translate('hello'); // Print translation (outputs 'こんにちは') echo $translation->text
Extending
If you need to EXTEND the existing Translation model note that:
- Your
Translationmodel needs to extend theSysoce\Translation\Models\Translationmodel
If you need to REPLACE the existing Translation model you need to keep the
following things in mind:
- Your
Translationmodel needs to implement theSysoce\Translation\Traits\HasHashIdTraitandSysoce\Translation\Traits\TranslationTraittraits
Whether extending or replacing, you will need to specify your new model in the configuration. To do this you must update the models.translation value in the configuration file after publishing the configuration.
Testing
./vendor/bin/phpunit
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.