A powerful URL manager extension that provides transparent language detection, persistence, and locale-aware URL generation for Yii applications.
Create SEO-friendly multilingual URLs with automatic language switching, GeoIP detection, and comprehensive fallback mechanisms.
- ✅ Automatic Language Detection - From URL, browser headers, session, or GeoIP.
- ✅ Flexible Configuration - Supports language aliases, wildcards, and custom mappings.
- ✅ Language Persistence - Remembers user's language choice.
- ✅ SEO-Friendly URLs - Clean URLs like
/en/about
or/es/acerca
.
composer require yii2-extensions/localeurls
- Detects language from URL path (
/es/about
→ Spanish). - Falls back to browser headers, session, or GeoIP.
- Adds language prefix to all generated URLs.
- Remember choice in session and cookie.
Replace your urlManager
component in config/web.php
.
<?php
declare(strict_types=1);
use yii2\extensions\localeurls\UrlLanguageManager;
return [
'components' => [
'urlManager' => [
'class' => UrlLanguageManager::class,
'languages' => ['en', 'es', 'fr', 'de'],
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'' => 'site/index',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
],
];
<?php
declare(strict_types=1);
use yii\helpers\Url;
// URL are automatically localized based on the current language
// /en/ (if current language is 'en')
Url::to(['site/index']);
// /es/site/about (if current language is 'es')
Url::to(['site/about']);
// Force specific language
Url::to(['site/contact', 'language' => 'fr']); // /fr/site/contact
<?php
declare(strict_types=1);
use yii\helpers\{Html, Url};
// Create language switcher links
foreach (Yii::$app->urlManager->languages as $language) {
echo Html::a(
strtoupper($language),
Url::current(['language' => $language]),
);
}
<?php
declare(strict_types=1);
// Get current language
$currentLang = Yii::$app->language;
// Get default language
$defaultLang = Yii::$app->urlManager->getDefaultLanguage();
For detailed configuration options and advanced usage patterns.
BSD-3-Clause license. Please see License File for more information.
This package is a fork of https://github.com/codemix/yii2-localeurls with some corrections.