So, I have to add language functionallity on "My Controller" to inherit all Controllers from this class. So if I have many "My Controllers" it could inherit the main "My Controller" (multi-level inheritance) right?
there is by default "Controller" class in protected/components of which all other generated controllers are descendants. You can just put your init() code there.
Yes I know that. But I have 2-3 different Controllers in components folder. Now I make it to inherit the base components/Controller. I run initialize it into __construct(). Thanks
A bit late but another solution that is independent from the controller is to set the language in a handler for the ‘onBeginReuest’ application event. Here is the code for a behavior that does exactly that:
class SetLanguageBehavior extends CBehavior
{
/**
* @inheritDoc
*/
public function events()
{
return array(
'onBeginRequest' => 'beginRequestHandler',
);
}
/**
* Sets the language based on application configuration and browser settings.
*/
public function beginRequestHandler($event)
{
$app = $this->getOwner();
$lang = $app->getRequest()->getPreferredLanguage();
// ADD YOUR OWN LOGIC HERE, E.G. SETTING THE LANGUAGE FROM THE DB
if ($lang == $app->sourceLanguage)
$lang = null;
$app->setLanguage($lang);
}
}
Then just add the behavior to the application in the main config: