Yii:使用图像在Navbar中进行本地化

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),
                        ),
                ),
            ),
        ));

1个回答

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.

doufuxi7093
doufuxi7093 哦谢谢。 我喜欢这种精神!
6 年多之前 回复
dongtuo4132
dongtuo4132 如我所链接的网站所述,精灵“可以出于任何目的免费使用,无需归属”。 右侧边栏中提供了完整的许可证信息。
6 年多之前 回复
dragon87836215
dragon87836215 这太棒了! 非常感谢。 我刚刚将我的建议添加到我的代码中,它确实很好用。 但CSS Spirits是否可以免费用于商业用途?
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问