rezzza/locale-voter-bundle

此包已被废弃,不再维护。未建议替代包。

通过投票器获取项目地区

安装: 11

依赖者: 0

建议者: 0

安全: 0

星标: 6

关注者: 2

分支: 1

开放问题: 0

类型:symfony-bundle

dev-master / 1.0.x-dev 2012-11-20 11:14 UTC

This package is not auto-updated.

Last update: 2020-01-24 15:11:03 UTC


README

已弃用

请使用 LocaleBundle

通过投票器获取项目地区。

安装

使用 composer

    "rezzza/locale-voter-bundle": "1.0.*",

然后

composer update "rezzza/locale-voter-bundle"

在您的 AppKernel 中添加

    $bundles = array(
        ...
        new Rezzza\LocaleVoterBundle\RezzzaLocaleVoterBundle(),
        ...
    )

在您的 config.yml 中定义配置

rezzza_locale_voter:
    strategy: request        # only one strategy available, you can set null to manually decide of locale.
    default_locale: %locale% # set fallback locale.
    locales: ['en', 'fr']    # Define valid locales.
    voters:                  # A list of voter. Order define priority.
        - rezzza.locale_voter.request_parameter.voter
        - rezzza.locale_voter.accept_language.voter

使用方法

使用上面定义的默认配置,它将首先查看第一次请求的 _locale GET 参数,如果不匹配,它将查看接受语言头。默认的投票器中定义的 locale 将设置在请求中。

您可以轻松定义新的投票器或定义自己的策略。

编写投票器

只需继承 VoterInterface 并在配置中添加投票器的服务 ID。

投票器示例

<?php

namespace Acme\DoudaBundle\Locale\Voter;

use Rezzza\LocaleVoterBundle\Locale\Context\LocaleContextInterface;
use Rezzza\LocaleVoterBundle\Locale\Voter\AbstractVoter;

class MyVoter extends AbstractVoter implements VoterInterface
{
    /**
     * {@inheritdoc}
     */
    public function vote(LocaleContextInterface $context)
    {
        $request = $context->getRequest();

        // suggestLocale will check if locale is supported.
        if ($request->get('chuck') === 'testa' && $this->suggestLocale('NOPE')) {
            return 'NOPE';
        }
    }

}

待办事项

  • 编写测试。