How to setup multi language site?

I'm newbie, I have been trying and trying but i got no idea how it works.

How could i proceed with the multi-language support?

eg. I want to eat apple. --> I want to 吃 apple.

Please advise.

Sincerely,

Kenny

Hello,

Hope it helps :

http://www.yiiframew…0.html#msg10330

Let me know if not :)

Hi ouhman,

Thank you for the url…

I tested but still not working…

What should i do?

Kenny.

Can you explain a bit more what doesn't work?

i get it working… the only thing is i do it in a dropdownlist… submit on selected… it change the setting for the whole pages links as expected but the url that display on the browser still the same…

eg.

Browser URL: http://localhost/blo…p/zh/site/index

Links of the page:

http://localhost/blo…p/en/site/index

http://localhost/blo…en/site/contact

http://localhost/blo…p/en/site/login

When i browse http://localhost/blo…p/en/site/index

Once loaded all in en, when i selected zh in dropdownlist, all the links in the page changed to zh but the Browser URL still en.

may i know how can i make the Browser's URL display identical with the links in the page?

Hmm not sure I understand what you want to do.

I am doing that way, pass the language variable in GET:

with the following :

$this->createUrl('controller/action',array('lang'=>'your_language_variable));

So your url will be :

http://localhost/blo…_language'

And after that, in the init method of your controler check the Get variable, and set the language application.

The setLanguage part is working good…

The strange part is even i key in url manually eg. http://localhost/blo…/zh/site/index, the result that display still in english.

When i select language, the url for all links changed but the Browser link does not change…

Any idea how can i solve the issue?

Where are you testing the language? I mean in which method and controller?

Can you post the code with your dropdownlist?

Ok here is the complete code of what i did:

/protected/components/LangBox.php



<?php


class LangBox extends CWidget


{


	public function run()


	{


		$currentLang = Yii::app()->language;


		$this->render('langBox', array('currentLang' => $currentLang));


	}


}


?>


/protected/components/views/langBox.php



<?php echo CHtml::form(); ?>


	<div id="langdrop">


	<?php 


		echo CHtml::dropDownList(


			'_lang', 


			$currentLang, 


			array('en'=>'English', 


				'zh'=>'Chinese', 


				'my'=>'Malay'), 


			array('submit' => '')


		);


	?>


	</div>


</form>


/protected/config/main.php



'urlManager'=>array(


	'class'=>'application.components.MyCUrlManager',


	'urlFormat'=>'path',


	'rules'=>array(


		'<language:(en|zh|my)>/<route:[w\/]+>' => '<route>'


	),


),


/protected/components/MyCUrlManager.php



<?php


class MyCUrlManager extends CUrlManager


{


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


	{


		if (!isset($params['language']))


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


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


	}


}


?>


/protected/controllers/SiteController.php



<?php


class SiteController extends CController


{


	private $_mylanguage;


	


	public function init()


	{


		if(isset($_POST['_lang']) && $_POST['_lang']!='')


		{


			$this->_mylanguage = $_POST['_lang'];


		}


		else


		{


			$this->_mylanguage = 'en';


		}


		


		# Do we have a cookie for the language?


		if(Yii::app()->request->cookies['lang']->value!='')


		{


			$this->_mylanguage = Yii::app()->request->cookies['lang']->value;


		}


		


		# Changed the language by get


		if(isset($_POST['_lang']) && $_POST['_lang'] != '')


		{


			$this->_mylanguage = trim($_POST['_lang']);


			


			# Save in cookie


			$cookie = new CHttpCookie('lang', $_POST['_lang']);


			$cookie->expire = time()+60*60*24*180;


			Yii::app()->request->cookies['lang'] = $cookie;


		}





		# Set the language


		Yii::app()->setLanguage($this->_mylanguage);


	}


}


?>


/protected/components/MyController.php



<?php


class MyController extends CController


{


	function init()


	{


		parent::init();


		$app = Yii::app();


		if (isset($_POST['_lang']))


		{


			$app->language = $_POST['_lang'];


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


		}


		else if (isset($app->session['_lang']))


		{


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


		}


	}


}


?>


/protected/views/site/index.php



<h1>


<?php echo Yii::t('index','home'); ?>!


</h1>


<?php $this->widget('application.components.Langbox'); ?> 


Here is all i have… I guess i missing on something… If i'm not wrong is the preload extension… I do not know how to extend it.

Please advise.

Kenny.

by rules you are using get parameters, but in this case  $app->language = $_POST['_lang'], why?

I'm using dropdownlist submit, that's why i get the selected language from $_POST['_lang'].

Are there any other way to do it?

Hope to hear from u…

jQuery ;)

but i think that's not way out

Thank you for your comment… If jQuery, how should the code be?

Anymore ideas?

Quote

Thank you for your comment.. If jQuery, how should the code be?

Anymore ideas?

smthng like this

jQuery(document).ready(function() {


jQuery('#lang').change(function(){


window.location = 'http://localhost/blog/index.php/'+$(this).val()+'/site/login';


);return false;});


});

I tested in jQuery, it never redirect… Anymore ideas?

Your effect will be greatly appreciated…

Kenny.

Quote

I tested in jQuery, it never redirect.. Anymore ideas?

Your effect will be greatly appreciated…

Kenny.

you don't need to copypaste all, it can be with syntax errors

like in this case

try

jQuery(document).ready(function() {

jQuery('#lang').change(function(){

window.location = 'http://localhost/blog/index.php/'+$(this).val()+'/site/login';

});return false;});