worksome / translator
A driver-based translation package for Laravel
Fund package maintenance!
v0.4.0
2026-11-30 10:32 UTC
Requires
- php: ^8.4
- google/cloud-translate: ^2.4
- illuminate/support: ^13.0
- spatie/laravel-package-tools: ^1.93
Requires (Dev)
- canvural/larastan-strict-rules: ^3.0
- guzzlehttp/guzzle: ^7.8
- larastan/larastan: ^3.10
- nunomaduro/collision: ^8.1.1
- orchestra/testbench: ^11.3
- pestphp/pest: ^5.3
- pestphp/pest-plugin-laravel: ^5.0
- worksome/coding-style: ^7.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A driver-based translation package for Laravel.
Installation
You can install the package via composer:
composer require worksome/translator
You can publish the config file with:
php artisan vendor:publish --tag="translator-config"
Configuration
The Google Cloud Translate driver uses the Cloud Translation Advanced (v3) API.
# Falls back to GOOGLE_CLOUD_PROJECT when not set. GOOGLE_CLOUD_TRANSLATE_PROJECT_ID=your-project-id # Optional: path to a service account JSON key file (recommended). GOOGLE_CLOUD_TRANSLATE_CREDENTIALS=/path/to/service-account.json # Optional: an API key, used only when no credentials file is set. GOOGLE_CLOUD_TRANSLATE_API_KEY=your-api-key
When neither is set, Application Default Credentials are used. These are found, in order, from:
- The
GOOGLE_APPLICATION_CREDENTIALSenvironment variable, which points to a JSON credentials file (a service account key, or a workload or workforce identity federation configuration). - Your local credentials from
gcloud auth application-default login(intended for development only). - The service account attached to the environment, when running on Google Cloud.
See Authentication in the Google Cloud PHP library for more details.
Usage
$translator = new \Worksome\Translator\TranslationManager(); $translator->driver('google_cloud_translate')->translate('Text to translate', 'en', 'da'); // TranslationDTO $translator->driver('google_cloud_translate')->detectLanguage('Text to detect'); // DetectedLanguageDTO
Via the Facade
// Use the default driver \Worksome\Translator\Facades\Translator::translate(''); // Use a custom driver \Worksome\Translator\Facades\Translator::driver('google_cloud_translate')->translate('', 'en', 'da');
Test suites can utilise the null driver, this will always return the same string value as provided for translations.
Testing
composer test
Changelog
Please see GitHub Releases for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.