代码之家  ›  专栏  ›  技术社区  ›  Bluebaron

如何使用zend-i18n更改区域设置

  •  0
  • Bluebaron  · 技术社区  · 7 年前

    在为zend framework设置翻译的说明中,它以硬编码方式传递区域设置。 我试图在自定义中间件中设置区域设置。由于配置在第一个中间件之前被聚合,所以我不能使该区域设置成为动态的。

    https://docs.zendframework.com/tutorials/i18n/

    'translator' => [
        'locale' => 'en_US',
        'translation_file_patterns' => [
            [
                'type'     => 'gettext',
                'base_dir' => getcwd() .  '/data/language',
                'pattern'  => '%s.mo',
            ],
        ],
    ],
    

    我的中间件是这样的:

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $locale = null;
    
        if (isset($_GET[self::LOCALE_GET_NAME])) {
            $locale = $_GET[self::LOCALE_GET_NAME];
        } else if (!$locale && isset($_COOKIE[self::LOCALE_COOKIE_NAME])) {
            $locale = $_COOKIE[self::LOCALE_COOKIE_NAME];
        //...
        $handler->handle($request->withAttribute(self::LOCALE_ATTRIBUTE_NAME, $locale))
    

    我也试过用 Locale::setDefault() 但是a)被警告不要这样做,因为它不是线程安全的b)它仍然没有在配置聚合器之前得到处理。

    我一定是在做什么蠢事。我无法想象这个价值应该是那样的。

    0 回复  |  直到 7 年前