yiic tool + multilanguage

I've only just started using Yii, before that I was using Zend Framework, which I found too slow for the applications I'm developing.

I couldn't find the answer to my question anywhere:

  1. The yiic tool allows for model and crud creation, but I don't see how it's possible to create them inside a module? Do you have to move them manually after creation?

  2. (Offtopic) I've read the posts on the forum about making multilanguage site. One of the ways sugguested was catching onbeginRequest, but I couldn't find in the manual "the proper way of doing it". I've done it this way:

protected/components/LangEvent.php:



<?php





class LangEvent extends CComponent 


{


	public function setLanguage($event)


	{


		$languages = array('ua', 'ru', 'en', 'de');


		


		$suffix = $event->sender->getUrlManager()->urlSuffix;


		$path = substr($_SERVER['REQUEST_URI'], strlen(Yii::app()->request->baseUrl) + 1);


		


		if (stristr($path, $suffix))


		{


			$path = substr($path, 0, strlen($path) - strlen($suffix));


		}


		


		$path = explode('/', $path);


		


		if (in_array($path[0], $languages))


		{


			Yii::app()->setLanguage($path[0]);


			unset($path[0]);


			


			Yii::app()->session['language'] = Yii::app()->getLanguage();


			Yii::app()->request->redirect(Yii::app()->request->baseUrl . '/' . implode('/', $path) . $suffix);


		}


		elseif (Yii::app()->session['language'])


		{


			Yii::app()->setLanguage(Yii::app()->session['language']);


		}


		elseif (Yii::app()->user->hasProperty('language'))


		{


			Yii::app()->setLanguage(Yii::app()->user->language);


		}


		else


		{


			Yii::app()->setLanguage($languages[0]);


		}


		


		$event->handled = true;


	}


}


index.php



...


require_once($yii);


$app = Yii::createWebApplication($config);


$app->onbeginRequest = array('LangEvent', 'setLanguage');


$app->run();


a) Is this the "correct" way of doing this?

B) Is it possible to move $languages array in the configuration option, as a custom configuration option?

Good questions.

  1. Currently yiic tool only works for application, not module. I plan to enhance it in the next release.

  2. Yii doesn't enforce the way to set the current language. What you did is definitely a possible solution. I would prefer to write a base controller class and put this piece of code into the init() method of the base controller.

Do you mean adding a new property "languages" to store the list of valid languages? If yes, I suggest you declare this as an application parameter by storing it in the 'params' property in the app config.

Thanks for such a quick reply! Your answers were helpfull. I'm making changes in regard to them. Looking through my source I found another question: I have 3 modules, and all the controller I need are in them. Is it possible to set a default module and default controller inside that module, i.e: I have "torism", "transport", "admin". And when the user enters my site I would like them to be pointed to tourism/default/index. Currently I have SiteController.php and index action, which redirects them there. Is it possible to do through configuration without the need of this controller?

You can set 'defaultController' property in app config to be 'moduleID/controllerID'.

I know this topic is from a while back, but I still have problems with multi-language. After moving the code to BaseController and extending all the controllers from this BaseController the controller isn’t being created and init isn’t called, because when trying to access http://mysite.com/en/index/index it goes to “Page Not Found” page. Is there something I’m missing?

Did you override init() in IndexController? If you override it, you need to call the parent implementation.

Nope:



<?php





class IndexController extends BaseController


{


	public function actionIndex()


  {


    var_dump('index');


  }


}


I think it just never gets to that controller, since it tries to find controller uk, so the init() is never run.

What is your URL rules?

I don't have any.

Then you need some apache rewrite rules so that the language segment can be recognized. Otherwise, the 'en' segment will be treated as the controller ID.