Zend_Translate - Zend_Navigation 和路由组合问题!

发布于 2024-08-23 07:47:36 字数 1822 浏览 12 评论 0原文

我在结合 Zend_Navigation、Zend_Translate 和所需的路由时遇到一些困难。

我的站点导航是通过基于 XML 文件的 Zend_Navigation 完成的。 我现在已经添加了基于 Zend_Translate 的翻译,并添加了以下引导程序的路由更改:

protected function _initRoutes()

    $config = new Zend_Config($this->getOptions());
    $languages = array_keys($config->languages->toArray());
    $zl = new Zend_Locale();
    $lang = in_array($zl->getLanguage(), $languages)
                  ? $zl->getLanguage() : 'en';

    $zfc = Zend_Controller_Front::getInstance();


    // add language to default route
    $route = new Zend_Controller_Router_Route(
            ':lang/:module/:controller/:action/*',
      array('controller'=>'index',
                'action' => 'index',
                'module'=>'default',
                'lang'=>$lang));

    $router = $zfc->getRouter();
    $router->addRoute('default', $route);
    $zfc->setRouter($router);

然后,我使用 preDispatc 方法创建了一个 View_Helper:

    $language = $request->getParam('lang','');

 if ($language !== 'en' && $language !== 'da')
     $request->setParam('lang','en');

 $language = $request->getParam('lang');
 if ($language == 'en')
     $locale = 'en_EN';
 else
     $locale = 'da_DK';

 $zl = new Zend_Locale();
 $zl->setLocale($locale);
 Zend_Registry::set('Zend_Locale', $zl);

 $translate = new Zend_Translate('csv', APPLICATION_PATH . '/configs/language/'. $language . '.csv' , $language);
 Zend_Registry::set('Zend_Translate', $translate);

现在,当我转到:“site/en/模块/控制器/动作”它工作正常。

当我转到:“site/da/module/controller/action”时,翻译工作正常,但我来自 Zend_Navigation 的链接指向默认的“en”语言链接“site/en” /module2/controller2/"

我不知道你的 at /da/ 语言。 任何帮助将不胜感激。

亲切的问候,

莫滕

I'm having some difficulties with the combination of Zend_Navigation, Zend_Translate and the routing needed.

My site navigation is done through Zend_Navigation based on and XML file.
I've now added translation to the site based on Zend_Translate and added to following change of routing to the bootstrap:

protected function _initRoutes()

    $config = new Zend_Config($this->getOptions());
    $languages = array_keys($config->languages->toArray());
    $zl = new Zend_Locale();
    $lang = in_array($zl->getLanguage(), $languages)
                  ? $zl->getLanguage() : 'en';

    $zfc = Zend_Controller_Front::getInstance();


    // add language to default route
    $route = new Zend_Controller_Router_Route(
            ':lang/:module/:controller/:action/*',
      array('controller'=>'index',
                'action' => 'index',
                'module'=>'default',
                'lang'=>$lang));

    $router = $zfc->getRouter();
    $router->addRoute('default', $route);
    $zfc->setRouter($router);

I've then created a View_Helper with a preDispatc method:

    $language = $request->getParam('lang','');

 if ($language !== 'en' && $language !== 'da')
     $request->setParam('lang','en');

 $language = $request->getParam('lang');
 if ($language == 'en')
     $locale = 'en_EN';
 else
     $locale = 'da_DK';

 $zl = new Zend_Locale();
 $zl->setLocale($locale);
 Zend_Registry::set('Zend_Locale', $zl);

 $translate = new Zend_Translate('csv', APPLICATION_PATH . '/configs/language/'. $language . '.csv' , $language);
 Zend_Registry::set('Zend_Translate', $translate);

Now when I go to: "site/en/module/controller/action" it works fine.

When I go to: "site/da/module/controller/action" the translation works fine, but my links from Zend_Navigation is pointing to the default 'en' language link "site/en/module2/controller2/"

I cant figure out to know that your at /da/ language.
Any help would be appriciated.

Kind regards,

Morten

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骑趴 2024-08-30 07:47:36

这并不能直接回答你的问题。但是 Zend_View_Helper_Navigation(导航视图助手)有一个 setTranslator() 方法,它将提供您添加到 Zend_Navigation 容器中的导航“页面”的隐式转换。

class MyModule_Bootstrap extends Zend_Application_Module_Bootstrap
{
   protected function _initMyModuleNavigation()
   {
    $langSess = new Zend_Session_Namespace('language');
    $translate = $langSess->translate;

    $this->getApplication()->bootstrap('navigation');

    $view = $this->getApplication()->getResource('view');

    $navigationHelper = $view->getHelper('navigation');

    $navigation = $navigationHelper->getContainer();

    $navigationHelper->setTranslator($translate);

    //...
}

这使您不必为每个项目手动调用翻译,

$navigation->addPages(array(
        new Zend_Navigation_Page_Mvc(array(
            'label' => $translate->_('Wiki'), //<-- This can be eliminated
            'action' => 'index',
            'controller' => 'article',
            'module' => 'wiki',
            'pages' => //...

因为它会自动完成,因为 setTranslator() 已完成。

This doesn't directly answer your question. But Zend_View_Helper_Navigation, the view helper for navigation, has a setTranslator() method that will provide implicit translation of the navigation 'pages' that you have added to the Zend_Navigation container.

class MyModule_Bootstrap extends Zend_Application_Module_Bootstrap
{
   protected function _initMyModuleNavigation()
   {
    $langSess = new Zend_Session_Namespace('language');
    $translate = $langSess->translate;

    $this->getApplication()->bootstrap('navigation');

    $view = $this->getApplication()->getResource('view');

    $navigationHelper = $view->getHelper('navigation');

    $navigation = $navigationHelper->getContainer();

    $navigationHelper->setTranslator($translate);

    //...
}

This saves you having to manually call translate for each item

$navigation->addPages(array(
        new Zend_Navigation_Page_Mvc(array(
            'label' => $translate->_('Wiki'), //<-- This can be eliminated
            'action' => 'index',
            'controller' => 'article',
            'module' => 'wiki',
            'pages' => //...

as it will be done automatically because setTranslator() was done.

音盲 2024-08-30 07:47:36

因为 Zend_Navigation_Page_Mvc 只检查模块、控制器和操作,所以看起来它永远不会转到其他路由元素。

一种解决方案可能是使用一个新类,例如: Zend_Navigation_Page_Mvcl 将处理语言。

但它似乎有点复杂,所以我找到了一个解决方案,使用更大的导航,其中包括所有语言的所有页面。

这是我的 xml 配置,用于用法语和英语导航 2 个页面“home”、“test”:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <nav>
        <fr>
            <label>menu.home</label>
            <params>
                <lang>fr</lang>
            </params>
            <action>index</action>
            <controller>homepage</controller>
            <route>langcontrolleraction</route>
            <pages>
                <home>
                    <label>menu.home</label>
                    <params>
                        <lang>fr</lang>
                    </params>
                    <action>index</action>
                    <controller>homepage</controller>
                    <route>langcontrolleraction</route>
                </home>
                <test>
                    <label>menu.wallet</label>
                    <params>
                        <lang>fr</lang>
                    </params>
                    <action>index</action>
                    <controller>wallet</controller>
                    <route>langcontrolleraction</route>
                </test>
            </pages>
        </fr>
        <en>
            <label>menu.home</label>
            <params>
                <lang>en</lang>
            </params>
            <action>index</action>
            <controller>homepage</controller>
            <route>langcontrolleraction</route>
            <pages>
                <home>
                    <label>menu.home</label>
                    <params>
                        <lang>en</lang>
                    </params>
                    <action>index</action>
                    <controller>homepage</controller>
                    <route>langcontrolleraction</route>
                </home>
                <test>
                    <label>menu.wallet</label>
                    <params>
                        <lang>en</lang>
                    </params>
                    <action>index</action>
                    <controller>wallet</controller>
                    <route>langcontrolleraction</route>
                </test>
            </pages>
        </en>
    </nav>
</config>

如您所见,仅 2 个页面就相当长了。
实际上,您将在导航中拥有(页面+ 1)x种语言的项目,

我确实使用了一个“技巧”:主页出现了两次。

  • 一次作为主菜单项
  • 一次作为第一个子菜单项

这个想法是根据所选的语言定义要显示的子菜单,这是通过路由器自动完成的,所以您所要做的就是告诉您的应用程序绘制子菜单而不是菜单。因此,这是我在布局文件中使用的行:

<?php 
echo $this->navigation()->menu()->renderSubMenu() 
?>

我没有看到您如何添加Zend_Navigation,所以这是我的Bootstrap.php< /strong>:

<?php
protected function _initNavigation() {
    $config = new Zend_Config_Xml(Zend_Registry::get ( 'config' )
            ->resources->navigation->file, 'nav');
    $navigationContainer = new Zend_Navigation($config);
    // Store the container in the proxy helper:
    $view = $this->getResource ( 'view' );
    $view->getHelper('navigation')->setContainer($navigationContainer);
}
?>

顺便说一句,在同一个 Bootstrap.php 中,我可以将默认语言保留为“en”,这是我的 _initRoutes()

<?php
protected function _initRoutes() {
    $router = Zend_Controller_Front::getInstance ()->getRouter ();
    $router->removeDefaultRoutes ();
    $myRoute = new Zend_Controller_Router_Route (
        ':lang/:controller/:action',
        array (
            'lang' => 'en',
            'controller' => 'index',
            'action' => 'index',
        )
    );
    $router->addRoute ( 'langcontrolleraction', $myRoute );
}
?>

对于翻译,它是如果您注册了 Zend_Translate,则会自动完成。在我的示例中:menu.home 将给出:

  • 英语 Home
  • 法语 Accueil

我希望这会有所帮助。

我更喜欢另一个更微妙的解决方案,但我还没有找到。

Because Zend_Navigation_Page_Mvc only checks for Module, Controller and Action it looks like it will never goes to additional routing element.

One solution could be to use a new class such as: Zend_Navigation_Page_Mvcl which will handle language.

But it seems a bit complex so I found one solution by using a larger navigation which includes all the pages in all the languages.

This is my xml config for navigation for 2 pages "home","test" in french and english:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <nav>
        <fr>
            <label>menu.home</label>
            <params>
                <lang>fr</lang>
            </params>
            <action>index</action>
            <controller>homepage</controller>
            <route>langcontrolleraction</route>
            <pages>
                <home>
                    <label>menu.home</label>
                    <params>
                        <lang>fr</lang>
                    </params>
                    <action>index</action>
                    <controller>homepage</controller>
                    <route>langcontrolleraction</route>
                </home>
                <test>
                    <label>menu.wallet</label>
                    <params>
                        <lang>fr</lang>
                    </params>
                    <action>index</action>
                    <controller>wallet</controller>
                    <route>langcontrolleraction</route>
                </test>
            </pages>
        </fr>
        <en>
            <label>menu.home</label>
            <params>
                <lang>en</lang>
            </params>
            <action>index</action>
            <controller>homepage</controller>
            <route>langcontrolleraction</route>
            <pages>
                <home>
                    <label>menu.home</label>
                    <params>
                        <lang>en</lang>
                    </params>
                    <action>index</action>
                    <controller>homepage</controller>
                    <route>langcontrolleraction</route>
                </home>
                <test>
                    <label>menu.wallet</label>
                    <params>
                        <lang>en</lang>
                    </params>
                    <action>index</action>
                    <controller>wallet</controller>
                    <route>langcontrolleraction</route>
                </test>
            </pages>
        </en>
    </nav>
</config>

As you can see it's quite long just for 2 pages.
Actually you will have (pages + 1) x languages items in the navigation

I did use a 'trick': the homepage is present twice.

  • once as main menu item
  • once as first submenu item

The idea is to define which submenu to display based on the selected lang and this is automatically done via the router, so all you have to do is to tell your application to draw the submenu instead of the menu. So here is the line I use for that in my layout file:

<?php 
echo $this->navigation()->menu()->renderSubMenu() 
?>

I didn't see how you added the Zend_Navigation so here is mine from Bootstrap.php:

<?php
protected function _initNavigation() {
    $config = new Zend_Config_Xml(Zend_Registry::get ( 'config' )
            ->resources->navigation->file, 'nav');
    $navigationContainer = new Zend_Navigation($config);
    // Store the container in the proxy helper:
    $view = $this->getResource ( 'view' );
    $view->getHelper('navigation')->setContainer($navigationContainer);
}
?>

By the way, in the same Bootstrap.php I can keep the default language to 'en', here is my _initRoutes():

<?php
protected function _initRoutes() {
    $router = Zend_Controller_Front::getInstance ()->getRouter ();
    $router->removeDefaultRoutes ();
    $myRoute = new Zend_Controller_Router_Route (
        ':lang/:controller/:action',
        array (
            'lang' => 'en',
            'controller' => 'index',
            'action' => 'index',
        )
    );
    $router->addRoute ( 'langcontrolleraction', $myRoute );
}
?>

And for translation it's automatically done if you registered your Zend_Translate. In my example: menu.home will give:

  • Home in english
  • Accueil in french

I hope this helps.

I would prefer another more subtle solution but I didn't find it yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文