简体   繁体   English

在zend framework 2网站上选择语言

[英]choose language in the zend framework 2 website

I use the Zend/Translator to translate the text in my website. 我使用Zend / Translator翻译我网站上的文字。 I set a default local and it works. 我设置了一个默认本地,它的工作原理。 Now the user should be able to choose the language in the layout.html: 现在,用户应该能够在layout.html中选择语言:

...

<li class="dropdown">
   <a href="<?php echo $this->url('home');?>" class="dropdown-toggle" data-toggle="dropdown">Sprache<b class="caret"></b></a>
        <ul class="dropdown-menu">
          <li><a href="#">Deutsch</a></li>
          <li><a href="<?php echo $this->url('application/default', array('action'=>'en'));?>">Englisch</a></li>
          <li><a href="#">Französisch</a></li>
          <li><a href="#">Spanisch</a></li>
        </ul>
</li>

...

The default language is German. 默认语言是德语。 Now I try to change the language to English by choosing 'Englisch' in the dropdown-menu. 现在我尝试通过在下拉菜单中选择“Englisch”将语言更改为英语。 But I don't know what to write in the controller. 但我不知道在控制器中写什么。 I tried it like this, but it doesn't work: 我试过这样,但它不起作用:

public function enAction()
{
    $translator = $this->getServiceLocator()->get('translator');
    $translator->setLocale('en');

    return $this->redirect()->toRoute('home');

}

Can someone help me? 有人能帮我吗?

Thanks. 谢谢。

You can have a look at SlmLocale , a module that performs locale detection and storage (either via a session or cookie). 您可以查看SlmLocale ,这是一个执行区域设置检测和存储的模块(通过会话或cookie)。 With SlmLocale, you can render a menu to switch from locale, all urls are updated accordingly. 使用SlmLocale,您可以渲染菜单以从区域设置切换,相应地更新所有URL。

echo $this->localeMenu();

SlmLocale is capable of selecting a locale based on the hostname, part of the path (ie /en/my/url ) or query parameter. SlmLocale能够根据主机名,路径的一部分(即/en/my/url )或查询参数选择区域设置。

Disclaimer: I am the author of SlmLocale :) 免责声明:我是SlmLocale的作者:)

the translator doesn't memorize the selected language when you set the locale. 设置区域设置时,翻译器不会记住所选语言。 you need to keep the selected lang in session for future request. 您需要将选定的lang保留在会话中以供将来请求使用。 and in you modules bootstrap get the lang from session and set translators locale. 在你的模块bootstrap从会话中获取lang并设置翻译器语言环境。

when you want change language should have code to refresh page with append lang parameter to url . 当你想要更改语言时,应该有代码来刷新页面,并将lang参数附加到url。 Setting is 设置是

class Settings{

   const DEFAULT_LANGUAGE = 'en';
   public static $locations = array(
    'sa'=>'sa_SA',
        'en'=>'en_US'
    );
}

and in Application\\Module.php 并在Application \\ Module.php中

public function onBootstrap(MvcEvent $e) {
    $lang = $e->getRequest()->getQuery('lang'); // new language
    $session = new Container('base');
    if($lang == null || $lang == ''){
        if ($session->offsetExists('lang')) {
            $lang = $session->offsetGet('lang'); // current language
        }else{
            $lang = 'en; // default language
        }
    }
    $session->offsetSet('lang', $lang);
    $loc = Settings::$locations[$lang];
    $translator
    ->setLocale($loc)
    ->setFallbackLocale(Settings::DEFAULT_LANGUAGE .'_' . Settings::DEFAULT_LOCATION);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM