コンテンツにスキップ
PHP Ecosystem
Explore

Locale URLs

このコンテンツはまだ日本語訳がありません。

Language detection, persistence, and locale-aware URLs for Yii2 applications

Feature Overview

Worker-mode requirements (sessions, cookies, event handlers) are listed in the configuration guide.

Terminal window
composer require yii2-extensions/localeurls:^0.2
  1. Detects the language from the URL path (/es/about sets Spanish).
  2. Falls back to the persisted language, then to browser headers or GeoIP, then to the default language.
  3. Adds the language code to generated URLs, except for the default language (see enableDefaultLanguageUrlCode).
  4. Remembers the choice in session and cookie.

Replace the urlManager component in config/web.php. The application language is the default language, which has no code in the URL; the defaultLanguage property of the manager sets it explicitly.

<?php
declare(strict_types=1);
use yii2\extensions\localeurls\UrlLanguageManager;
return [
'language' => 'en',
'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;
// Current language `en`, the default language.
Url::to(['site/about']); // /site/about
// Current language `es`, set by a request such as /es/site/index.
Url::to(['site/about']); // /es/site/about
// Another language.
Url::to(['site/contact', 'language' => 'fr']); // /fr/site/contact

Practical guides and examples for language-aware URLs and worker isolation.

AI agents: use the repository guides and source for the installed revision. For a documentation deployment with llms.txt, read that index first. When Markdown export is enabled, append index.md to a page URL. For Yii conventions, use the yii2-framework skill from a local framework documentation checkout.

If the hosted sources are unavailable, use the repository guides and locally available skills.

Follow on X Follow on Facebook Join our Subreddit Join on Telegram

This package started as a fork of codemix/yii2-localeurls.

License