Installation:
composer require weglot/translate-laravel
The package uses Laravel's auto-discovery, so no manual service provider registration is needed.
Publish Config (if needed):
php artisan vendor:publish --provider="Weglot\TranslateLaravel\WeglotServiceProvider"
This generates a config/weglot.php file with default settings.
Configure Weglot:
Add your Weglot API key and project slug to .env:
WEGLOT_API_KEY=your_api_key_here
WEGLOT_PROJECT_SLUG=your_project_slug
First Use Case:
Add Weglot's JavaScript snippet to your resources/views/layouts/app.blade.php (or your main layout file):
@weglot
This outputs the Weglot script with your configured API key and project slug.
Dynamic Language Switching: Use Weglot's language detection middleware to auto-switch based on user preferences or browser settings:
// In routes/web.php
Route::middleware(['weglot'])->group(function () {
// Your routes here
});
Blade Directives:
@weglot: Outputs the Weglot script tag.@weglotLang: Displays the current language (e.g., @weglotLang → "en").@weglotLangSwitcher: Renders a language switcher dropdown.API Integration: Fetch Weglot's translation status or user language via the facade:
use Weglot\TranslateLaravel\Facades\Weglot;
$currentLang = Weglot::getCurrentLanguage(); // e.g., "fr"
$isTranslated = Weglot::isTranslated(); // boolean
Asset Optimization: Load Weglot asynchronously or defer its script to improve page load performance:
@weglot(['async' => true])
Conditional Loading: Only load Weglot for specific locales or routes:
if (app()->getLocale() !== 'en') {
@weglot
}
@weglot in cached views.$this->partialMock(Weglot::class, ['getCurrentLanguage']);
Deprecation Warning: The package is deprecated. Migrate to Weglot’s subdomain integration for long-term support.
Configuration Overrides:
weglot.php config file may not exist after installation. Publish it explicitly if customization is needed.default_language) are hardcoded in the package. Override them in the config file.JavaScript Conflicts:
@weglot directive is placed before </body> to avoid race conditions.Locale Mismatch:
@weglotLang) may differ from Laravel’s app()->getLocale(). Sync them via middleware:
public function handle($request, Closure $next) {
$weglotLang = Weglot::getCurrentLanguage();
if ($weglotLang && !in_array($weglotLang, config('app.locales'))) {
app()->setLocale(config('app.fallback_locale'));
}
return $next($request);
}
API Key Exposure:
.env file is committed by default in some setups. Add weglot.php to .gitignore if it contains sensitive keys.Check Console Errors: Weglot’s JS may log errors if the API key/project slug is invalid.
Disable Weglot Temporarily:
// In config/weglot.php
'enabled' => env('WEGLOT_ENABLED', false),
Set WEGLOT_ENABLED=false in .env to debug without Weglot.
Inspect Network Requests: Verify Weglot’s API calls in Chrome DevTools (Network tab) under weglot.com.
Custom Language Detection:
Override Weglot’s language logic by extending the Weglot facade:
Weglot::extend(function ($app) {
$app->bind('weglot.detector', function () {
return new CustomLanguageDetector();
});
});
Post-Translation Hooks: Trigger actions after language changes (e.g., redirect or reload):
// In a custom JS file
window.Weglot.on('languageChanged', function() {
window.location.reload();
});
Server-Side Fallbacks: Use Weglot’s API to fetch translations for untranslated content:
public function getTranslation($text, $targetLang) {
return Weglot::translate($text, $targetLang); // Hypothetical method
}
Note: The package lacks a direct API client; use Weglot’s official API instead.
How can I help you explore Laravel packages today?