codebuglab/laravel-go-translate
Installation:
composer require codebuglab/laravel-go-translate
Publish the config file (if needed):
php artisan vendor:publish --provider="Codebuglab\LaravelGoTranslate\GoTranslateServiceProvider"
First Use Case:
Translate a Laravel resource (e.g., resources/views):
php artisan translate:resource en es resources/views
Replace en (source language) and es (target language) with your desired language codes.
Where to Look First:
php artisan for available commands (translate:resource, translate:vendor, translate:folder, translate:file).config/laravel-go-translate.php (if published). Adjust settings like google_api_key (if needed) or default languages.Codebuglab\LaravelGoTranslate\GoTranslateServiceProvider registers commands and binds the translator service.Translating Laravel Resources:
Use the translate:resource command to translate views, languages, or other resource files. Example:
php artisan translate:resource en fr resources/lang
resources/lang/fr/*.php).Translating Vendor Files:
Translate third-party packages (e.g., Blade templates in vendor):
php artisan translate:vendor en de vendor/package-name
Translating Folders/Files:
php artisan translate:folder en ja /path/to/folder
php artisan translate:file en zh resources/views/welcome.blade.php
config/).Integration with Workflows:
post-install or post-deploy script to auto-translate resources during deployment (e.g., for multilingual sites).en).translate:resource en es resources/lang to generate Spanish translations.resources/lang/es/.Customizing Translations:
--extension option to specify file extensions (e.g., --extension=php,blade):
php artisan translate:folder en fr resources/views --extension=blade
resources/lang/es/messages.php).Environment-Specific Config:
if (app()->environment('local'))).composer.json:
"scripts": {
"post-install-cmd": [
"@php artisan translate:resource en es resources/lang"
]
}
Add this only to dev dependencies or use a script like npm run dev && php artisan translate:resource ....Handling Dynamic Content:
__('key')).Combining with Other Packages:
spatie/laravel-translatable for database-localized models:
// After translating resources/lang, ensure your models use the same keys.
$model->title = __('translated.key');
Testing:
$this->app->instance('google-translate', Mockery::mock());
Google API Limitations:
Google_Service_Exception. Solution: Add a google_api_key to config/laravel-go-translate.php or wait.File Overwrites:
--dry-run first:
php artisan translate:file en fr file.blade.php --dry-run
git diff resources/lang/es/
Unsupported File Types:
.php, .blade.php, and text-based files. Binary files (e.g., .png) or non-text files (e.g., .json with non-string values) may break.--extension or pre-process files.Language Code Mismatches:
es for Spanish) match Google’s supported languages. Invalid codes (e.g., spa) will fail silently or return poor results.Vendor File Risks:
composer update. Best Practice: Fork the package and translate the forked version.Command Output:
-v (verbose) flag to debug:
php artisan translate:resource en fr resources/lang -v
Failed to translate file X or Unsupported extension.Logging:
// config/logging.php
'default' => 'single',
'channels' => [
'single' => [
'driver' => 'single',
'path' => storage_path('logs/translate.log'),
'level' => 'debug',
],
],
API Key Issues:
google_api_key, ensure it has the Cloud Translation API enabled and billing set up (even for free tier).Exclude Files:
Use .gitignore-style patterns to skip files (e.g., node_modules, vendor):
php artisan translate:folder en fr resources/views --exclude="node_modules,*.min.js"
Batch Processing: For large projects, translate incrementally:
# Step 1: Translate views
php artisan translate:folder en fr resources/views
# Step 2: Translate language files
php artisan translate:resource en fr resources/lang
Custom Translator: Extend the package by binding a custom translator in the service provider:
$this->app->bind('google-translate', function () {
return new CustomGoogleTranslator();
});
Performance:
// config/laravel-go-translate.php
'cache_translations' => true,
'cache_ttl' => 3600, // 1 hour
Fallback Languages:
Configure fallback languages in config/laravel-go-translate.php:
'fallback_languages' => [
'es' => 'en', // Fallback to English for Spanish
],
Post-Translation Hooks:
Use Laravel’s translated event to process files after translation:
// app/Providers/EventServiceProvider.php
protected $listen = [
'translated' => [
'App\Listeners\PostTranslateListener',
],
];
How can I help you explore Laravel packages today?