douyun3631 2013-11-13 09:31
浏览 56
已采纳

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条回答

  • douren5898 2013-11-13 10:42
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能