yii2-extensions/localeurls
最新稳定版本:0.1.2
Composer 安装命令:
composer require yii2-extensions/localeurls
包简介
Automatic locale/language management for URLs.
README 文档
README
Locale URLs
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.
Features
- ✅ 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/aboutor/es/acerca.
Quick start
Installation
composer require yii2-extensions/localeurls
How it works
- 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.
Basic Configuration
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>', ], ], ], ];
Basic Usage
Automatic URL generation
<?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
Language switching
<?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]), ); }
Current language access
<?php declare(strict_types=1); // Get current language $currentLang = Yii::$app->language; // Get default language $defaultLang = Yii::$app->urlManager->getDefaultLanguage();
Documentation
For detailed configuration options and advanced usage patterns.
Quality code
Our social networks
License
Fork
This package is a fork of https://github.com/codemix/yii2-localeurls with some corrections.
统计信息
- 总下载量: 6.57k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2023-10-31