2017 © Pedro Peláez
 

yii2-extension yii2-translatable-url-rule

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

image

geertw/yii2-translatable-url-rule

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

  • Wednesday, October 18, 2017
  • by geertw
  • Repository
  • 2 Watchers
  • 7 Stars
  • 2,044 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 5 Versions
  • 5 % Grown

The README.md

Latest Stable Version Total Downloads License, (*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)

The Versions

18/10 2017

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

by Geert Wirken

extension yii2 translation i18n urlmanager

18/10 2017

v1.2.0

1.2.0.0

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

  Sources   Download

MIT

The Requires

 

by Geert Wirken

extension yii2 translation i18n urlmanager

21/05 2017

v1.1.1

1.1.1.0

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

  Sources   Download

MIT

The Requires

 

by Geert Wirken

extension yii2 translation i18n urlmanager

17/04 2017

v1.1.0

1.1.0.0

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

  Sources   Download

MIT

The Requires

 

by Geert Wirken

extension yii2 translation i18n urlmanager

17/04 2017

v1.0.0

1.0.0.0

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

  Sources   Download

MIT

The Requires

 

by Geert Wirken

extension yii2 translation i18n urlmanager