language in the path! ex: .com/fr/

Hello

Would you please help me to insert the language in the URL of my application?

I’m developing a multilingual application, the main language is English with extensibility to other languages.

So the website will work like this:

http://www.example.com/ <- English version

http://www.example.com/fr/ will show the same previous version but in Frensh

If you can help me to setup the router (urlManager) it would be awesome!

I only can suggest you to do following:

1.Add rule in UrlManager like:

‘fr/<controller:\w+>/<id:\d+>’=>’<controller>/view/language/fr’,

‘fr/<controller:\w+>/<action:\w+>/<id:\d+>’=>’<controller>/<action>/language/fr’,

‘fr/<controller:\w+>/<action:\w+>’=>’<controller>/<action>/language/fr’,

  1. Alter your components/Controller.php

public function __construct(){

if (isset($_GET['language']))

//set Yii::app() language here

}



But I’m sorry if this solution can be wrong.

Thank you diggy, but this is not what I’m looking for, it should be more general, because it’s going to be applied to the whole website, with many possible languages.

Regards

First at all, try looking here at the forum. Similar questions were raised.

Or try something like this:

In config/parameters.php define allowed languages:




// List of all available languages

// NOTE! must be identical with below langsNames array and urlManager rule in main.php

'allLangs' => array('uk'=>'Українська', 'en'=>'English', 'ru'=>'Русский'),

// List of all available languages exclude default (in our case - 'uk')

'langs' => array('en', 'ru'),

// All alloved languages (NOTE! the variables must be identical to $allLangs array)

'langsNames' => array(

  'en'=>'en',

  'en_us'=>'en',

  'uk'=>'uk',

  'uk_ua'=>'uk',

  'ru'=>'ru',

  'ru_ru'=>'ru',

),

'defaultLang' => 'uk',



Than define urlManager rule in main.php:




'urlManager'=>array(

  'class'=>'AUrlManager',

  'urlFormat'=>'path',

  'caseSensitive'=>false,

  'showScriptName'=>false,

  'rules'=>array(

// only for 3 allowed languages

    '<language:(en|uk|ru)>' => '/',

    '<language:(en|uk|ru)>/<route:[\w\\/]+>' => '<route>',

// or for any languages

    '<language:[a-zA-Z_]{2,5}>/<route:[\w\\/]+>' => '<route>',

  ...



Create ../components/AUrlManager class:




class AUrlManager extends CUrlManager {

	public function createUrl($route,$params=array(),$ampersand='&'){

		$params['language'] = Yii::app()->language;

		return parent::createUrl($route,$params,$ampersand);

	}

}



Than add setMyAppLanguage() method in ../components/Controller and call it:




private function setMyAppLanguage() {

  $app = Yii::app();

  $lang = $app->parameters['defaultLang'];


  if(!empty($_GET['language']) && array_key_exists($_GET['language'], $app->parameters['langsNames'])){

    $lang = $app->parameters['langsNames'][$_GET['language']];

  } elseif(isset($app->session['language'])) {

    $lang = $app->session['language'];

  } elseif(isset($app->request)){

    $preferLang = $app->request->getPreferredLanguage();

    if(!empty($preferLang) && array_key_exists($prefLang, $app->parameters['langsNames'])){

      $lang = $app->parameters['langsNames'][$preferLang];

    }

  } else {

    $lang = $app->parameters['defaultLang'];

  }


  $app->session['language'] = $lang;

  $app->setLanguage($lang);

}



At final you always will have your current language in session.

I hope this help you for start…

Thank you,

I reached a similar approach, but the problem is that using the following role, cancels the benefit of including other get params in the URL.


'<language:[a-zA-Z_]{2,5}>/<route:[\w\\/]+>' => '<route>',

Looking for a workaround.

Regards