PHPackages                             unique/yii2-translatable-url-rule - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. unique/yii2-translatable-url-rule

ActiveYii2-extension[Localization &amp; i18n](/categories/localization)

unique/yii2-translatable-url-rule
=================================

A custom URL rule class for Yii 2 which allows to create translated URL rules

v1.2.1(7y ago)011MITPHP

Since Apr 17Pushed 4y agoCompare

[ Source](https://github.com/uniquexor/yii2-translatable-url-rule)[ Packagist](https://packagist.org/packages/unique/yii2-translatable-url-rule)[ RSS](/packages/unique-yii2-translatable-url-rule/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (6)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/dc6992f54896c23fe7357d108ae2911595a9e11f4f16017f4dae159e227fbac7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6765657274772f796969322d7472616e736c617461626c652d75726c2d72756c652e737667)](https://packagist.org/packages/geertw/yii2-translatable-url-rule)[![Total Downloads](https://camo.githubusercontent.com/455fa765872e1be3a26ace187c37b79aebee4ead6e50a7ea59b68e5d9805dc47/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6765657274772f796969322d7472616e736c617461626c652d75726c2d72756c652e737667)](https://packagist.org/packages/geertw/yii2-translatable-url-rule)[![License](https://camo.githubusercontent.com/08760ad01b1173e97940c7a870717caeada75ebb18a4c28c9e4fd3c292f35ae2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6765657274772f796969322d7472616e736c617461626c652d75726c2d72756c652e737667)](https://packagist.org/packages/geertw/yii2-translatable-url-rule)

Yii2 TranslatableUrlRule
========================

[](#yii2-translatableurlrule)

A custom [URL rule class](http://www.yiiframework.com/doc-2.0/yii-web-urlruleinterface.html) for [Yii 2](http://www.yiiframework.com/) which allows for translated URL rules.

This extension allows you to write URL rules per language. For example, you can have `signup` for `en`, `aanmelden` for `nl` and `registrieren` for `de`. Because this extension uses [normal Yii2 UrlRule](http://www.yiiframework.com/doc-2.0/yii-web-urlrule.html) objects to build language-specific URL rules, you still have all power that comes with normal Yii URL rules, including URL parameters and regular expressions.

The current language is determined by the `Yii::$app->language` parameter. The value of this parameter is used to create and to parse URLs.

This extension does **not** set the current language parameter. Use [codemix/yii2-localeurls](https://github.com/codemix/yii2-localeurls) for that.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
composer require geertw/yii2-translatable-url-rule

```

or add

```
"geertw/yii2-translatable-url-rule": "*"

```

to the require section of your `composer.json` file.

Usage
-----

[](#usage)

Update your URL rules to use this class and set the patterns per language.

```

```

The `route` parameter remains the same for all rules. `patterns` is an array of all patterns, the key must equal the Yii language identifier. If there is no pattern for a language, the first configured pattern will be used.

Set `forceRuleSearch` to `true` to force searching in all rule patterns.

You may omit the `class` configuration in your URL rules when you configure a [ruleConfig](http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html#$ruleConfig-detail) in UrlManager:

```

```

Language switcher example
-------------------------

[](#language-switcher-example)

Due to the way this library works, you need to specify **two** language parameters when creating URLs for routes in another language.

The following two code snippets allow you to create a simple dropdown which allows users to select alternative languages for the current route:

Create a widget like this:

```
