3

有一个带有多个标志的下拉菜单会很好。当我使用 twitter-bootstrap 时,有没有办法在导航中显示像标志这样的图像?如果我选择一个标志,语言和标志应该改变。

我可以显示引导程序支持的图标,但它们不包含标志。

我希望有人能帮助我。下面是我的导航。

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

1 回答 1

2

正如您所注意到的,您首先需要标志图标。例如,您可以使用这些. 您可能需要也可能不需要稍微修改返回的 CSS 以满足您的需要。

接下来,您需要创建切换 URL。这基本上归结为将当前 URL 和语言选择作为 get 参数。为您当前所在的站点创建一个规范链接并附加?lang=<target>。另一种方法是简单地将开关部分附加到当前 URL 并使用它。我在这里概述了类似的内容。

接下来,您需要确保您的应用程序实际上可以处理本地化。我不知道你在那条路上走了多远,所以你可能想读一下。要实际根据请求切换语言,您可以执行以下操作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'));

}

这样,无论您身在何处,语言都会根据 get 参数设置,并且只要会话存在就一直存在。您甚至可能希望将“defaultLanguage”替换为调用确定默认值的函数,例如根据浏览器的用户代理字符串或访问者的 IP。

要生成您需要/想要的语言文件,请查看.

要在菜单中创建必要的链接,您可以创建一个返回适当数组的函数,也可以使用三元运算符切换值,例如label和。但是,这仅适用于二元决策。如果你想提供更多的语言,你应该考虑一种不同的方法,比如我提到的函数。url'label' => ('en' == Yii::app()->language) ? 'Switch to Spanish' : 'Switch to English'

于 2013-11-13T10:42:23.463 回答