It would be nice to have a Dropdown menu with several flags.Is there a way to show a image like a Flag in the navigation when I use twitter-bootstrap? If I select one flag, the language and the flag should change.
I can show the bootstrap supported Icons but they contain no flags.
I hope some can help me. below is my navigation.
$this->widget('bootstrap.widgets.TbNavbar', array(
'type' => 'null', // null or 'inverse'
'brand' => '<img src="http://localhost/images/logo.gif">',
'brandUrl' => 'localhost',
'collapse' => true, // requires bootstrap-responsive.css
'items' => array(
array(
'class' => 'bootstrap.widgets.TbMenu',
'type' => 'pills', // '', 'tabs', 'pills' (or 'list')
'items' => array(
array('label' => 'Home', 'url' => array('/site/index')),
array('label' => 'user create', 'url' => array('/account/user/create')),
array('label' => 'Contact', 'url' => array('/site/contact')),
array('label' => 'User', 'url' => array('/account/user/index')),
),
),
array(
'class' => 'bootstrap.widgets.TbMenu',
'type' => 'pills', // '', 'tabs', 'pills' (or 'list')
'htmlOptions' => array('class' => 'pull-right'),
'items' => array(
/**Added to show, that here should appear the flag*/
array('label' => '','htmlOptions'=> array('src'=>'http://localhost/images/german.png'), 'url' => ''),
array('label' => 'Sign in', 'url' => array('/account/signup/login'), 'visible' => Yii::app()->user->isGuest),
array('label' => 'Sign up', 'url' => array('/account/signup/registration'), 'visible' => Yii::app()->user->isGuest),
array('label' => Yii::app()->user->name , 'url' => '#', 'items' => array(
array('label' => 'Action', 'url' => '#'),
array('label' => 'Another action', 'url' => '#'),
array('label' => 'Something else here', 'url' => '#'),
'---',
array('label' => 'Logout', 'url' => array('/account/signup/logout')),
),'visible' => !Yii::app()->user->isGuest),
),
),
),
));
As you noticed, you'll need flag icons to start with. You could, for example, use these. You may or may not need to modify the returned CSS a little to suit your needs.
Next you need to create the switch-URLs. This essentially boils down to having the current URL AND the language selection as get parameter. Create a canonical link for the site you are currently on and attach ?lang=<target>
. Another approach would be to simply attach the switch part to the current URL and use that instead. I outlined something like that here.
Next you need to make sure your application can actually handle localization. I have no idea how far along that road you are yet, so you may want to read this. To actually have the languages switch on request you can do the following in protected/components/Controller.php
public function init() {
parent:init();
if (null === Yii::app()->user->getState('lang') || null !== Yii::app()->request->getQuery('lang', null)) {
Yii::app()->user->setState('lang', Yii::app()->request->getQuery('lang','defaultLanguage'));
}
Yii::app()->setLanguage(Yii::app()->user->getState('lang'));
}
This way, wherever you are, the language gets set based on the get parameter and persists as long as the session exists. You may even want to replace 'defaultLanguage' with a call to a function that determines a default value e.g. based on the user agent string of the browser or the visitor's IP.
To generate the language files you need/want take a look at this.
To create the necessary links inside the menu you can either create a function returning the appropriate array or switch the values such as label
and url
with a ternary operator, e.g. 'label' => ('en' == Yii::app()->language) ? 'Switch to Spanish' : 'Switch to English'
. However, this only properly works with binary decisions. If you want to offer more languages you should consider a different approach, such as the function I mentioned.