, (*1)
Yii2 TranslatableUrlRule
A custom URL rule class for Yii 2 which allows for translated URL rules., (*2)
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 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., (*3)
The current language is determined by the Yii::$app->language
parameter. The value of this parameter is used to create and to parse URLs., (*4)
This extension does not set the current language parameter. Use codemix/yii2-localeurls for that., (*5)
Installation
The preferred way to install this extension is through composer., (*6)
Either run, (*7)
composer require geertw/yii2-translatable-url-rule
or add, (*8)
"geertw/yii2-translatable-url-rule": "*"
to the require section of your composer.json
file., (*9)
Usage
Update your URL rules to use this class and set the patterns per language., (*10)
<?
$rules = [
[
'class' => 'geertw\Yii2\TranslatableUrlRule\TranslatableUrlRule',
'patterns' => [
'en' => '/signup',
'nl' => '/aanmelden',
'de' => '/registrieren',
],
'route' => 'user/signup',
],
[
'class' => 'geertw\Yii2\TranslatableUrlRule\TranslatableUrlRule',
'patterns' => [
'en' => '/user/<username>',
'nl' => '/gebruiker/<username>',
'de' => '/benutzer/<username>',
],
'route' => 'user/view',
],
];
?>
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., (*11)
Set forceRuleSearch
to true
to force searching in all rule patterns., (*12)
You may omit the class
configuration in your URL rules when you configure a ruleConfig in UrlManager:, (*13)
<?
return [
'components' => [
'urlManager' => [
'ruleConfig' => [
'class' => 'geertw\Yii2\TranslatableUrlRule\TranslatableUrlRule'
],
'rules' => $rules,
// Additional UrlManager configuration
],
];
?>
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., (*14)
The following two code snippets allow you to create a simple dropdown which allows users to select alternative languages for the current route:, (*15)
Create a widget like this:, (*16)
controller->route;
$params = $_GET;
$this->isError = $route === Yii::$app->errorHandler->errorAction;
array_unshift($params, '/' . $route);
foreach (Yii::$app->urlManager->languages as $language) {
$isWildcard = substr($language, -2) === '-*';
if ($isWildcard) {
$language = substr($language, 0, 2);
}
$params['language'] = $language;
$params['url-language'] = $language;
$this->items[] = [
'label' => $this->label($language),
'url' => $params,
];
}
parent::init();
}
public function run() {
// Only show this widget if we're not on the error page
if ($this->isError) {
return '';
} else {
return parent::run();
}
}
public function label($code) {
return isset($this->langLabels[$code]) ? $this->langLabels[$code] : null;
}
}
```
Insert the following code in your view code:
```php
= LanguageSwitcher::widget([
'options' => ['class' => 'pull-right'],
'langLabels' => [
'de' => 'German',
'en' => 'English',
'nl' => 'Nederlands',
],
]) ?>
You might recognize this example, as it is largely based on the example for yii2-localeurls., (*17)