在 Symfony2/Twig 中从 2 位国家代码获取翻译后的国家名称?

问题描述 投票:0回答:8

我正在使用 Symfony2 国家/地区字段类型,它运行良好并且国家/地区名称已翻译。我将两位数的国家/地区代码存储在我的实体的

country
列中。

如何显示完整的翻译国家/地区名称? 这就是我将字段添加到表单中的方法:

$builder
    ->add('country', 'country', array(
        'label' => 'Paese', 'preferred_choices' => array('IT')
    ));

然后在我的控制器中:

$user = $this->getDoctrine()->getRepository('AcmeHelloBundle:User');
$countryCode = $user->getCountry();
$countryName = null; // Get translated country name from code

或者在我的树枝模板中:

{# Output the country code and name #}
{{ user.country }}

{# translated country name from code #}
symfony twig symfony-forms
8个回答
21
投票

我不确定您是否仍然需要...但这可能对其他人有帮助。这可以通过树枝扩展轻松完成(此代码基于@tomaszsobczak的答案)

<?php
    // src/Acme/DemoBundle/Twig/CountryExtension.php
    namespace Acme\DemoBundle\Twig;


    class CountryExtension extends \Twig_Extension {
        public function getFilters()
        {
            return array(
                new \Twig_SimpleFilter('country', array($this, 'countryFilter')),
            );
        }

        public function countryFilter($countryCode,$locale = "en"){
            $c = \Symfony\Component\Locale\Locale::getDisplayCountries($locale);

            return array_key_exists($countryCode, $c)
                ? $c[$countryCode]
                : $countryCode;
        }

        public function getName()
        {
            return 'country_extension';
        }
    }

在你的 services.yml 文件中

# src/Acme/DemoBundle/Resources/config/services.yml
services:
    acme.twig.country_extension:
        class: Acme\DemoBundle\Twig\CountryExtension
        tags:
            - { name: twig.extension }

twig 文件中的使用示例:

{{ 'US'|country(app.request.locale) }}

19
投票

根据上面 @Rvanlaak 的评论,\Symfony\Component\Locale\Locale 现在已已弃用。我认为现在最简洁的方法是:

use Symfony\Component\Intl\Intl;

...

$country = Intl::getRegionBundle()->getCountryName($countryCode);

17
投票

受 Hannoun Yassir 答案的启发,我在国家/地区类型字段中使用 Intl。 twig 扩展的代码是

<?php
namespace Tbl\SagaBundle\Twig;

use Symfony\Component\Intl\Intl;

class CountryExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('countryName', array($this, 'countryName')),
        );
    }

    public function countryName($countryCode){
        return Intl::getRegionBundle()->getCountryName($countryCode);
    }

    public function getName()
    {
        return 'country_extension';
    }
}
?>

在 services.yml 中添加 twig 扩展

# src/Acme/DemoBundle/Resources/config/services.yml
services:
    acme.twig.acme_extension:
        class: Acme\DemoBundle\Twig\CountryExtension
        tags:
            - { name: twig.extension }

模板中的用法(默认情况下,国家/地区名称将显示在语言环境中(参见 Symfony/Component/Intl/ResourceBundle/RegionBundleInterface.php)

{{ user.countryCode|countryName }}

非常感谢 Yassir,此版本不使用自 2.3 版以来已弃用的语言环境 >> http://symfony.com/components/Locale


11
投票

使用 SonanaIntlBundle,你可以做这样的事情:

{{ 'FR' | country }} => France (if the current locale in request is 'fr')
{{ 'FR' | country('de') }} => Frankreich (force the locale)

{{ 'fr' | language }} => français (if the current locale in request is 'fr')
{{ 'fr' | language('en') }} => French (force the locale)

{{ 'fr' | locale }} => français (if the current locale in request is 'fr')
{{ 'fr' | locale('en') }} => French (force the locale)

了解更多相关信息


11
投票

您可以使用 Symfony 用于国家/地区字段类型的相同组件

public function humanCountry() {
    $c = \Symfony\Component\Locale\Locale::getDisplayCountries('en');

    return array_key_exists($this->getCountry(), $c)
           ? $c[$this->getCountry()]
           : $this->getCountry();
}

9
投票

如果您使用实体,除了执行树枝过滤器之外,一种选择是创建用于在实体内获取国家/地区名称的函数。

use Symfony\Component\Intl\Intl;

public function getCountryName() {
    return Intl::getRegionBundle()->getCountryName($this->getCountry());
}

所以稍后你可以在树枝中做

{{ user.countryName }}

3
投票

为了方便起见,如果几年后有人读到这篇文章:

对于 twig 2 或更高版本,您可以使用

composer require twig/intl-extra

使过滤器language_name可用。它还提供了一些配置选项。

看医生。


0
投票

Symfony 7.1(PHP)

<?php

namespace App\Twig;

use Twig\TwigFilter;
use Symfony\Component\Intl\Countries;
use Twig\Extension\AbstractExtension;

class AppFiltersExtension extends AbstractExtension
{
    public function getFilters(): array
    {
        return [
            new TwigFilter('countryName', array($this, 'getCountryName')),
        ];
    }

    public function getCountryName(string $countryCode, ?string $locale = 'en'): string
    {
        return Countries::getName($countryCode, $locale);
    }
}

树枝:

{{ 'GE'|countryName(app.request.locale) }}
{{ user.countryCode|countryName(app.request.locale) }}

享受;)

© www.soinside.com 2019 - 2024. All rights reserved.