Ir al contenido
PHP Ecosystem
Explore

Locale URLs

Esta página aún no está disponible en tu idioma.

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

Feature Overview

Install the package with Composer:

Terminal window
composer require yii2-extensions/localeurls:^0.2

The 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.php
use 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:

RequestResult
/es/product/42Runs in es and stores the choice in the session and a cookie.
/product/42 with es storedRedirects to /es/product/42.
/ES-mx/product/42Redirects to /es-mx/product/42, which runs in es-MX.
/deutsch/product/42Runs in de.
/en/product/42Redirects to /product/42 and stores English again.
/product/42 with Accept-Language: fr-CARedirects to /fr/product/42 on a first visit.
/product/42 with X-Geo-Country: AUTRedirects to /deutsch/product/42 when no browser language matches.
/api/ordersRuns 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/7
Url::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/7
Url::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=m

The 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.

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

The guides and the API reference generated from the PHPDoc in src/ are published at https://yii2-framework.github.io/docs/extensions/localeurls/.

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.

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.

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

Original project: codemix/yii2-localeurls.

License