简体   繁体   English

ZF2中的翻译无效

[英]Translations not working in ZF2

I know this has been posted multiple times but I've gone through each answer and found nothing worked for me... 我知道这已被发布多次,但是我已经遍历了每个答案,却发现没有任何解决方法……

In module.config file 在module.config文件中

'service_manager' => array(
    'abstract_factories' => array(
        'Zend\Db\Adapter\AdapterAbstractServiceFactory',
    ),
    'factories' => array(
        'translator'                    => 'Zend\I18n\Translator\TranslatorServiceFactory',
        // various app services, but only listing first one
        'Application\Service\Fetch'     => 'Application\Factory\Service\FetchServiceFactory',
    ),
),
'translator' => array(
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),

In module/Application/language/ folder, I have these .mo and .po files: module / Application / language /文件夹中,我有以下.mo和.po文件:

  • de.mo de.mo
  • de.po de.po
  • en.mo en.mo
  • en.po en.po
  • es.mo es.mo
  • es.po es.po
  • it.mo it.mo
  • it.po it.po

In one of the template view files: 在模板视图文件之一中:

<div id="l_activite" class="homeTitle">
    <?php echo $this->translate('Activités', __NAMESPACE__);?>
</div>

In Module.php 在Module.php中

I've cut this down to show how the setProjectName is invoked (which handles language translations and other things...) 我将其简化以显示setProjectName是如何调用的(它处理语言翻译和其他操作...)

public function onBootstrap(MvcEvent $e)
{
    $application          = $e->getApplication();
    $eventManager         = $application->getEventManager();

    $moduleRouteListener  = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
    $eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'setProjectName'));
}

I've cut the function down a bit to only show the lines concerning the language... 我将功能缩减了一点,只显示了与语言有关的行...

public function setProjectName(MvcEvent $e)
{
    // Session (lang & background)
    $session        = new Session();
    $request        = $e->getRequest();

    $lang = null;

    if (!$request instanceof \Zend\Console\Request) {
        $rm             = $request->getQuery();
        $lang           = $rm->get('lang');
    }

    $session->lang        = $lang        ?: $session->lang ?: 'fr';
    $lang                 = $session->lang;

    $session->langLocale  = strtolower($session->lang) . '_' . strtoupper($session->lang) . '.UTF-8';
    if ($lang == 'en') {
        $session->langLocale  = strtolower($session->lang) . '_' . strtoupper('US') . '.UTF-8';
    }

    $loc = setlocale(LC_ALL, $session->langLocale);
    // for Windows
    if (!$loc) {
        switch ($session->lang) {
            case 'en':
                $session->langLocale = 'eng';
            break;
            case 'de':
                $session->langLocale = 'deu';
            break;
            case 'fr':
                $session->langLocale = 'fra';
            break;
            case 'it':
                $session->langLocale = 'ita';
            break;
            case 'es':
                $session->langLocale = 'esn';
            break;
        }
        setlocale(LC_ALL, $session->langLocale);
    }

    \Locale::setDefault($session->langLocale);

    $translator          = $e->getApplication()->getServiceManager()->get('translator');
    $langLocale          = $translator->getLocale();


    $e->setParam('lang', $langLocale);
}

I'm guessing the things I do in Module.php needs fixing in some way? 我猜我在Module.php中要做的事情需要某种方式修复吗? I have that block of Windows code since we plan to run the app locally on some "offline" machines, but also running the app on the server (which runs linux). 由于我们计划在某些“脱机”计算机上本地运行该应用程序,而且还要在服务器(运行linux)上运行该应用程序,因此我具有该Windows代码块。

We use $lang as fr/de/en/it/es because the database (sqlite) file is stored as _fr.sqlite , _de.sqlite , etc. 我们将$lang用作fr/de/en/it/es因为数据库(sqlite)文件存储为_fr.sqlite_de.sqlite等。

尝试将.mo.po文件重命名为与“ eng”,“ deu”,“ fra”,“ ita”,“ esp”相同。

Which version of ZF2. 哪个版本的ZF2。 The translatorinterface changed in 2.3 转换器界面在2.3中进行了更改

In module.config file You are using this translator 在module.config文件中,您正在使用此转换器

   'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',

I think the problem is that you are some how importing or using an old translator namespace (path) file. 我认为问题在于您是如何导入或使用旧的转换器命名空间(路径)文件的。

You need to look for a file where you are importing the zend translator, like listener, that can be in Listener/ApplicationListener.php 您需要查找要在其中导入zend转换器的文件,例如侦听器,该文件可以位于Listener / ApplicationListener.php中。

You will probably see some thing like this. 您可能会看到类似这样的东西。

you need to activate the first one and comment the second one 您需要激活第一个并注释第二个

use Zend\I18n\Translator\Translator; //use this namespace, it fits your configs.

// use Zend\\Mvc\\I18n\\Translator; //使用Zend \\ Mvc \\ I18n \\ Translator;

Hope that may give some guidance. 希望可以提供一些指导。

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

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