Beautifying Urls with Lang Parameter

I need a bit of help on this:

scenario:

I have developed a multilingual site for a real estate application and now that I need to display the flags I am wondering what would be the best approach to include the parameter.

I know that at first seems quite an easy task but it is not as I am using $_GET and $_POST and I do not know when that is happening as that depends if the user is searching for properties or not. When a user makes a search I collect posted data and created a custom CListView widget that renders the pagination according to user submission. This pagination needs to maintain its dataSource even though I click on a flag.

So, how do I create that link that sets the language? (i.e. http://localhost/bla/bla/lang or http://localhost/bla/bla/?l=lang). Maybe is my head that is already about to explote but I do not know how to create a proper URL…

I would like it to be http://lang.domain.com/allthequerystring (but remember I need to maintain old URL strings there, when a user submits a search, the $_POST parameters need to be attached!, and when the pagination is clicked, then the $_GET parameters)

How can I do it?

Any help will be highly appreciated…

I found everything I needed for that feature by stealing looking at the code for Phundament II.’’

http://code.google.com/p/phundament/source/browse/trunk/modules/p2/extensions/langhandler/ELangCUrlManager.php




        	'rules'=>array(

            	'<lang:[a-z]{2}_[a-z]{2,}>/cms/<pageId:\d+>/<pageName:[a-zA-Z0-9.\-_]+>'=>'p2/p2Page/view',

            	'<lang:[a-z]{2}_[a-z]{2,}>/cms/<pageId:\d+>'=>'p2/p2Page/view',

            	'<lang:[a-z]{2}_[a-z]{2,}>/cms/<pageName:[a-zA-Z0-9.\-_]+>'=>'p2/p2Page/view',

            	'<lang:[a-z]{2}_[a-z]{2,}>/wiki/<page:[a-zA-Z0-9.\-_]+>'=>'site/wiki',

            	'<lang:[a-z]{2}_[a-z]{2,}>/wiki'=>'site/wiki',

            	'<lang:[a-z]{2}_[a-z]{2,}>/blog/<id:\d+>/<name:[a-zA-Z0-9.\-_]+>' => 'site/blog',

            	'<lang:[a-z]{2}_[a-z]{2,}>/blog/<id:\d+>' => 'site/blog',

            	'<lang:[a-z]{2}_[a-z]{2,}>/blog' => 'site/blog',

            	'<lang:[a-z]{2}_[a-z]{2,}>' => '/',

            	'<lang:[a-z]{2}_[a-z]{2,}>/<_c>' => '<_c>',

            	'<lang:[a-z]{2}_[a-z]{2,}>/<_c>/<_a>' => '<_c>/<_a>',

            	'<lang:[a-z]{2}_[a-z]{2,}>/<_m>/<_c>/<_a>' => '<_m>/<_c>/<_a>',

        	),



Well, it could be a start ? :)

not bad no… but, that means I need to rebuild all my createUrl links throughout my application?

Thanks jacmoe! Your forwarded me to the right direction. After reading the classes of phundament, I have found http://www.yiiframework.com/extension/langhandler/ which holds some rules within that I think are what I am looking for…

I keep you udpated

This is what the custom UrlManager does in Phundament2:


class ELangCUrlManager extends CUrlManager {

	

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


    	if (isset($params['lang']) && $params['lang'] == "__EMPTY__") {

        	unset($params['lang']);

    	} elseif (!isset($params['lang'])) {

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

    	}

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

	}

	

}



I believe you can just plug that in and customize it to fit your needs.

It will basically make sure that the ‘lang’ parameter is set when a Url is created.

I’d love to offer more advice, but my Yii-foo is less than a month old. :lol:

<edit>

I saw your edit - and it looks like you’re on your way to do the same as the Phundament dev has done. :)

</edit>

My doubt is how to not do the double task of rewriting all my createUrl statements. I can already do it but I want to re-create the links without the need of touching the rest of my application code.

Wouldn’t the power of inheritance make that work without touching a single createUrl call?

So, instead of the ‘standard’ UrlManager, you just use a CustomUrlManager in you app config? :)

It will make sure that the lang parameter is set, before handing it over to the standard Yii UrlManager.

I think that’s pretty neat. ;)

I am not sure if this works:


    	'urlManager' => array(

        	'class' => 'ext.CustomUrlManager',



But if it did, it would be awesome.

Hi Jacmoe,

Thank you very much for pointing me to the right direction. This is what I have done:

  • modified versions of urlManager to manage the creation of urls (you were right on your last post)

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

if (isset($params[‘lang’]) && $params[‘lang’] == “EMPTY”) {

unset(&#036;params['lang']);

} elseif (!isset($params[‘lang’])) {

&#036;params['lang']=Language::model()-&gt;getLocalizedLanguage();

}

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

}

  • then I created my own component to handle the language parameters (just like the ELangHandler extension does)

  • next step was to handle urls in the main.php config file

i.e. (this works for all, I do not need to put as many as phundament does thanks to ELanghandler extension again; great job qian with this, easy, clean, very good job)

‘<lang:(es|en|de|it|fr|ca)>/<_c>/<_a>/*’ => ‘<_c>/<_a>’

  • and finally, for the flags display, I use preg_replace to change the url parameter using (/es/|/en/|/de/|/it/|/fr/|/ca/).

You deserve my positive vote… Thanks jacmoe