gpt4 book ai didi

zend-framework2 - ZF2 翻译

转载 作者:行者123 更新时间:2023-12-04 07:12:51 25 4
gpt4 key购买 nike

我有两个模块 - 应用程序和 StickyNotes。我需要在所有页面上使用翻译。

我做什么:

1)查看:<?=$this->translate('Home');?>
2) 在应用程序\Module.php 中:

public function onBootstrap(MvcEvent $e)
{
$translator = $e->getApplication()->getServiceManager()->get('translator');
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$app = $e->getParam('application');
$app->getEventManager()->attach('render', array($this, 'setLayoutTitle'));
$translator->setLocale('ru_RU');
echo $translator->getLocale(); //ru_RU
}

3) 在 StickyNotes\Module.php 中:
public function onBootstrap(MvcEvent $e)
{
$translator = $e->getApplication()->getServiceManager()->get('translator');
$translator->setLocale('ru_RU');
echo $translator->getLocale(); //ru_RU
}

4) 应用程序\..\module.config.php:
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),

'aliases' => array(
'translator' => 'MvcTranslator',
),

'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),

5) StickyNotes\..\module.config.php 相同:
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),

'aliases' => array(
'translator' => 'MvcTranslator',
),

'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),

如果我尝试 $translator->getLocale();输出“ru_RU”,但翻译不起作用。

另外,如果我手动更改 'locale' => 'en_US','locale' => 'ru_RU',翻译工作正常。感谢您的回答!

最佳答案

在 Application\Module.php

public function onBootstrap(MvcEvent $e) {
$translator = $e->getApplication()->getServiceManager()->get('translator');
$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 = Settings::DEFAULT_LANGUAGE; // default language
}
}
$session->offsetSet('lang', $lang);
$loc = Settings::$locations[$lang];
$translator
->setLocale($loc)
->setFallbackLocale(Settings::DEFAULT_LANGUAGE .'_' . Settings::DEFAULT_LOCATION);
}

和设置类
class Settings{
const DEFAULT_LOCATION = 'IR';
const DEFAULT_LANGUAGE = 'fa';

public static $locations = array(
'fa'=>'fa_IR',
'sa'=>'sa_SA',//Arabic (sa, sa-SA)
'tr'=>'tr_TR',
'en'=>'en_US'
);
}

关于zend-framework2 - ZF2 翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22513140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com