Search by

yii2-extensions / localeurls

terabytesoftw

Automatic locale/language management for URLs.

Package info

github.com/yii2-extensions/localeurls

Homepage

Documentation

pkg:composer/yii2-extensions/localeurls

Statistics

Installs: 6 747

Dependents: 0

Suggesters: 0

Stars: 3

Open Issues: 0

0.2.2 2026-12-02 12:40 UTC

This package is auto-updated.

Last update: 2026-12-02 12:47:08 UTC


README

Yii Framework

Locale URLs


PHPUnit Coverage Mutation Testing PHPStan

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

Features

Feature Overview

Quick start

Installation

Install the package with Composer:

composer require yii2-extensions/localeurls:^0.2

The package has no assets and no bootstrap class. The Installation Guide lists the requirements.

Usage

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:

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

Documentation

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 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-configuration skill: language URL aliases, default language, detection and persistence, canonical redirects, the languageChanged event, and request isolation in long-running workers.
  • ๐Ÿงญ yii2-framework skill: 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.

Package information

PHP Yii 2.0.55+ Yii 22.0.x Latest Stable Version Total Downloads

Project status

PHPStan Level Max Quality StyleCI

Security

OpenSSF Scorecard Zizmor

Built with AI

Built with Claude Code Built with Codex Reviewed by CodeRabbit

Our social networks

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

Credits

Original project: codemix/yii2-localeurls.

License

License