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.

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料