tomatophp/filament-translations-google
Installation
composer require tomatophp/filament-translations-google
Publish the config file:
php artisan vendor:publish --provider="Tomato\FilamentTranslationsGoogle\FilamentTranslationsGoogleServiceProvider" --tag="config"
Register the Package
Add the service provider to config/app.php under providers:
Tomato\FilamentTranslationsGoogle\FilamentTranslationsGoogleServiceProvider::class,
Configure Google API Key
Add your Google Cloud Translation API key to .env:
GOOGLE_TRANSLATE_API_KEY=your_api_key_here
Configure the supported languages in config/filament-translations-google.php:
'supported_languages' => [
'en' => 'English',
'fr' => 'French',
'es' => 'Spanish',
// Add more as needed
],
First Use Case: Auto-Translate Existing Strings
Navigate to Filament Admin Panel > Translations Manager > Select a language (e.g., fr) > Click "Auto-Translate" to fetch and translate all __()/trans() strings from your source language (default: en).
Add Language Support
Extend config/filament-translations-google.php with new languages:
'supported_languages' => [
'en' => 'English',
'de' => 'German', // New addition
],
Trigger Auto-Translation
use Tomato\FilamentTranslationsGoogle\Facades\FilamentTranslationsGoogle;
FilamentTranslationsGoogle::translate('de'); // Translate to German
Manual Overrides After auto-translation, manually refine translations in the Filament Translations Manager for accuracy.
Dynamic Language Switching Use middleware to set the locale dynamically (e.g., based on user preference):
public function handle($request, Closure $next)
{
$locale = $request->header('Accept-Language') ?? config('app.locale');
app()->setLocale($locale);
return $next($request);
}
Fallback Logic
Configure fallback locales in config/app.php:
'fallback_locale' => 'en',
Blade Directives
Use @translate in Blade templates for consistency:
<h1>@translate('welcome.title')</h1>
Custom Translation Sources
Override the default __()/trans() parsing logic by extending the TranslationSource class:
namespace App\Providers;
use Tomato\FilamentTranslationsGoogle\TranslationSource;
use Illuminate\Support\Facades\File;
class CustomTranslationSource extends TranslationSource
{
public function getStrings(): array
{
$customStrings = File::get('custom-translations.json');
return json_decode($customStrings, true);
}
}
Bind it in a service provider:
$this->app->bind(
Tomato\FilamentTranslationsGoogle\TranslationSource::class,
App\Providers\CustomTranslationSource::class
);
Batch Processing Schedule translations for multiple languages using Laravel Queues:
use Tomato\FilamentTranslationsGoogle\Facades\FilamentTranslationsGoogle;
use Illuminate\Support\Facades\Queue;
Queue::push(function () {
FilamentTranslationsGoogle::translate(['fr', 'es', 'de']);
});
API Key Restrictions
'cache_translations' => true, // Enable in config
Character Limits
use Tomato\FilamentTranslationsGoogle\Helpers\StringSplitter;
$chunks = StringSplitter::split($longString, 5000);
Rate Limiting
429 Too Many Requests.try {
FilamentTranslationsGoogle::translate($lang);
} catch (\GuzzleHttp\Exception\RequestException $e) {
if ($e->getCode() === 429) {
sleep(30); // Wait 30 seconds
retry();
}
}
Log Translation Failures
Enable debug logging in config/filament-translations-google.php:
'debug' => env('APP_DEBUG', false),
Check logs at storage/logs/laravel.log for API errors.
Verify API Key Test your API key manually:
curl -X POST -H "Authorization: Bearer $GOOGLE_TRANSLATE_API_KEY" \
-H "Content-Type: application/json" \
-d '{"q":"Hello","target":"fr"}' \
"https://translation.googleapis.com/language/translate/v2"
Clear Cache After Updates If translations appear stale:
php artisan cache:clear
php artisan view:clear
Prioritize High-Impact Strings Use the "Translate Priority" feature in Filament to mark critical strings (e.g., error messages) for manual review after auto-translation.
Localization Testing
Simulate locale-specific content with Laravel’s app()->setLocale():
app()->setLocale('fr');
dd(__('welcome.title')); // Test French translation
Cost Optimization
Fallback to Human Translation For low-quality auto-translations, integrate a manual review step:
FilamentTranslationsGoogle::translate('de', [
'require_review' => true, // Flag for manual check
]);
Environment-Specific Config
Override settings per environment (e.g., disable auto-translation in staging):
'auto_translate_on_save' => env('APP_ENV') !== 'staging',
How can I help you explore Laravel packages today?