Skip to content

yii2-extensions/localeurls

Yii Framework

Locale URLs


PHP-Version Yii-22.0.52 Yii2-22 PHPUnit Mutation-Testing Static-Analysis Codecov

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/about or /es/acerca.

Quick start

Installation

composer require yii2-extensions/localeurls

How it works

  1. Detects language from URL path (/es/about → Spanish).
  2. Falls back to browser headers, session, or GeoIP.
  3. Adds language prefix to all generated URLs.
  4. 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

phpstan-level StyleCI

Our social networks

Twitter

License

BSD-3-Clause license. Please see License File for more information.

Fork

This package is a fork of https://github.com/codemix/yii2-localeurls with some corrections.

Sponsor this project

 

Packages

No packages published

Contributors 2

  •  
  •  

Languages