Yii Framework Forum: language in the path! ex: .com/fr/ - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

language in the path! ex: .com/fr/ Rate Topic: -----

#1 User is offline   Y!! Newbie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 02-November 10

Posted 02 November 2010 - 11:13 AM

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!
0

#2 User is offline   diggy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 112
  • Joined: 18-November 09
  • Location:Ukraine, Khmelnitsky

Posted 02 November 2010 - 05:34 PM

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',
2. 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.
0

#3 User is offline   Y!! Newbie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 02-November 10

Posted 02 November 2010 - 05:38 PM

View Postdiggy, on 02 November 2010 - 05:34 PM, said:

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',
2. 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
0

#4 User is offline   sprint 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 15-December 08
  • Location:Ukraine, Dubno

Posted 06 November 2010 - 10:01 AM

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...
0

#5 User is offline   Y!! Newbie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 02-November 10

Posted 07 November 2010 - 10:25 AM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users