Locale URLs
Nội dung này hiện chưa có sẵn bằng ngôn ngữ của bạn.
Language detection, persistence, and locale-aware URLs for Yii2 applications
Features
Section titled “Features”Quick start
Section titled “Quick start”Installation
Section titled “Installation”Install the package with Composer:
composer require yii2-extensions/localeurls:^0.2The package has no assets and no bootstrap class. The Installation Guide lists the requirements.
A storefront in four languages: English is the default language and has no code in the URL, Spanish accepts regional
variants, and German is published under the deutsch alias. The JSON API under /api has no language at all. Customers
sign in with an ActiveRecord identity that has a language column.
<?php
declare(strict_types=1);
// config/web.phpuse yii2\extensions\localeurls\{LanguageChangedEvent, UrlLanguageManager};
return [ 'language' => 'en', 'components' => [ // Replaces the URL manager of the application; nothing else has to be registered. 'urlManager' => [ 'class' => UrlLanguageManager::class, // Every request starts from English, also in a long-running worker that keeps the application alive. 'defaultLanguage' => 'en', // `es-*` accepts /es and regional URLs such as /es-mx; German is reachable as /deutsch only. 'languages' => [ 'en', 'es-*', 'fr', 'deutsch' => 'de', ], // Without a stored choice or a matching browser language, the `X-Geo-Country` header decides. 'geoIpLanguageCountries' => [ 'de' => ['DEU', 'AUT'], 'fr' => ['FRA'], ], // Route pattern => URL pattern: API URLs get no code, and API requests resolve no language. 'ignoreLanguageUrlPatterns' => ['#^api/#' => '#^api/#'], 'showScriptName' => false, 'rules' => [ '' => 'site/index', 'product/<id:\d+>' => 'product/view', 'api/orders' => 'api/order/index', ], // Optional. Raised when a language from the URL replaces the stored one: keep it on the customer record // too. `updateAttributes()` comes from ActiveRecord, so drop the handler for any other identity class. 'on languageChanged' => static function (LanguageChangedEvent $event): void { Yii::$app->user->identity?->updateAttributes(['language' => $event->language]); }, ], ],];The manager resolves the language before the request is routed, so the URL rules stay free of language codes:
| Request | Result |
|---|---|
/es/product/42 | Runs in es and stores the choice in the session and a cookie. |
/product/42 with es stored | Redirects to /es/product/42. |
/ES-mx/product/42 | Redirects to /es-mx/product/42, which runs in es-MX. |
/deutsch/product/42 | Runs in de. |
/en/product/42 | Redirects to /product/42 and stores English again. |
/product/42 with Accept-Language: fr-CA | Redirects to /fr/product/42 on a first visit. |
/product/42 with X-Geo-Country: AUT | Redirects to /deutsch/product/42 when no browser language matches. |
/api/orders | Runs in English: no redirect, no stored language. |
Created URLs follow the language of the request:
use yii\helpers\Url;
// On /es/product/42?size=m the application language is `es`, and so is every URL.Url::to(['/product/view', 'id' => 7]); // /es/product/7Url::to(['/site/index']); // /es
// Another language: the alias replaces `de`, and a regional code is written in lowercase.Url::to(['/product/view', 'id' => 7, 'language' => 'de']); // /deutsch/product/7Url::to(['/product/view', 'id' => 7, 'language' => 'es-MX']); // /es-mx/product/7
// An ignored route gets no code.Url::to(['/api/order/index']); // /api/orders
// Language switcher: the same page and query string in another language.Url::current(['language' => 'fr']); // /fr/product/42?size=m
// The default language given explicitly links the URL that redirects to /product/42?size=m// and resets the stored choice.Url::current(['language' => 'en']); // /en/product/42?size=mThe Usage Examples build a complete language
switcher and the hreflang links for search engines. The
Configuration Guide details
persistence, detection, redirects and the
worker-mode requirements
for sessions, cookies and event handlers.
Documentation
Section titled “Documentation”Practical guides and examples for language-aware URLs and worker isolation.
- 📚 Installation Guide
- ⚙️ Configuration Guide, including worker mode
- 💡 Usage Examples
- 🔄 Upgrade Guide
- 🧪 Testing Guide
The guides and the API reference generated
from the PHPDoc in src/ are published at https://yii2-framework.github.io/docs/extensions/localeurls/.
AI assistance
Section titled “AI assistance”AI agents: read the documentation index and fetch the Markdown version
of a page by appending index.md to its URL. The
AI agents guide covers skill installation, client setup and
verification.
- 🤖
yii2-localeurls-configurationskill: language URL aliases, default language, detection and persistence, canonical redirects, thelanguageChangedevent, and request isolation in long-running workers. - 🧭
yii2-frameworkskill: Yii 22.0 application conventions.
The hosted documentation covers Yii 22.0. If it does not match the installed revision or is unavailable, use the repository guides, the skill source, the source PHPDoc and the changelog.
Our social networks
Section titled “Our social networks”Follow on X Follow on Facebook Join our Subreddit Join on Telegram
Credits
Section titled “Credits”Original project: codemix/yii2-localeurls.
License
Section titled “License”Package
Section titled “Package”- Source code and issues: yii2-extensions/localeurls. Star the repository if the package is useful to you.
- Composer package: yii2-extensions/localeurls.
- Changelog: CHANGELOG.md.