Installation:
composer require weglot/translate-laravel
(Note: Despite being deprecated, the package remains functional for existing integrations.)
Publish Config (if needed):
php artisan vendor:publish --provider="Weglot\TranslateLaravel\WeglotServiceProvider"
This generates a config/weglot.php file with default settings.
Configure .env:
WEGLOT_API_KEY=your_api_key_here
WEGLOT_DOMAIN=yourdomain.com
(Replace placeholders with your Weglot credentials from dashboard.weglot.com.)
First Use Case:
Add Weglot’s JavaScript snippet to your layout (e.g., resources/views/layouts/app.blade.php):
@weglot
This renders the Weglot script with your configured API key and domain.
Dynamic Language Switching: Use Weglot’s built-in language selector in Blade templates:
@weglotLanguageSelector
Customize appearance via config/weglot.php (e.g., selector_position, selector_theme).
Excluding Pages/Elements: Skip translation for specific routes or elements:
// In routes/web.php
Route::middleware(['weglot:exclude'])->group(function () {
Route::get('/admin', 'AdminController@index');
});
Or exclude elements via HTML attributes:
<div data-weglot-ignore="true">...</div>
API Integration: Fetch translation stats or manage projects via Weglot’s API:
use Weglot\TranslateLaravel\Facades\Weglot;
$stats = Weglot::api()->stats();
Middleware for Conditional Loading: Load Weglot only for non-admin routes:
// app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
// ...
\Weglot\TranslateLaravel\Http\Middleware\LoadWeglot::class,
],
'admin' => [
// Exclude from admin
],
];
Custom Translations:
Override Weglot’s translations for specific terms via config/weglot.php:
'custom_translations' => [
'en' => ['home' => 'Home Page'],
'es' => ['home' => 'Página Principal'],
],
(Note: Requires Weglot’s custom dictionary feature.)
Event Listeners: Trigger actions on language change (e.g., update user locale):
// app/Providers/EventServiceProvider.php
public function boot()
{
Weglot::onLanguageChanged(function ($locale) {
app()->setLocale($locale);
});
}
Deprecation Warning:
Caching Issues:
?v=2 to bypass:
<script src="//cdn.weglot.com/weglot.js?v=2"></script>
Locale Mismatch:
app()->getLocale() matches Weglot’s detected language to avoid UI inconsistencies.dd(Weglot::currentLanguage());
API Rate Limits:
$limits = Weglot::api()->limits();
Verify API Key:
Check config/weglot.php and .env for typos. Test with:
Weglot::api()->validateKey();
Console Logs:
Enable debug mode in config/weglot.php:
'debug' => env('WEGLOT_DEBUG', false),
Logs appear in storage/logs/laravel.log.
Blade Directives:
If @weglot fails, ensure the service provider is registered (auto-discovery should handle this, but verify in config/app.php).
Custom JavaScript:
Override the default snippet via config/weglot.php:
'custom_script' => '<script src="custom-weglot.js"></script>',
Webhook Integration: Use Weglot’s webhooks to sync translations with your database:
// routes/web.php
Route::post('/weglot-webhook', 'WebhookController@handle');
Testing: Mock Weglot in tests:
$this->partialMock(Weglot::class, ['api']);
How can I help you explore Laravel packages today?