PHP code example of geertw / yii2-translatable-url-rule
1. Go to this page and download the library: Download geertw/yii2-translatable-url-rule library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
geertw / yii2-translatable-url-rule example snippets
<?
$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',
],
];
<?
return [
'components' => [
'urlManager' => [
'ruleConfig' => [
'class' => 'geertw\Yii2\TranslatableUrlRule\TranslatableUrlRule'
],
'rules' => $rules,
// Additional UrlManager configuration
],
];
namespace frontend\components;
use Yii;
use yii\bootstrap\Dropdown;
class LanguageSwitcher extends Dropdown {
public $langLabels;
private $isError;
public function init() {
$route = Yii::$app->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;
}
}
<?= LanguageSwitcher::widget([
'options' => ['class' => 'pull-right'],
'langLabels' => [
'de' => 'German',
'en' => 'English',
'nl' => 'Nederlands',
],
])