Get translated country name from a 2 digit country

2019-03-08 20:39发布

问题:

I'm using the Symfony2 country Field Type, it works well and country names are translated. I am storing the two-digit country code in the column country of my entity.

How can I display the full, translated country name? This is how I added the field to the form:

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

And then in my controller:

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

Or in my twig template:

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

{# translated country name from code #}

回答1:

I'm not sure if you still need... but it might help someone else. this can be done through a twig extension easily (this code is based on @tomaszsobczak's answer )

<?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';
        }
    }

And in your services.yml files

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

Usage example inside a twig file:

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


回答2:

As per @Rvanlaak's comment above, \Symfony\Component\Locale\Locale is now deprecated. I think the most concise way to do this now is:

use Symfony\Component\Intl\Intl;

...

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


回答3:

Inspired by Hannoun Yassir answer, I use the Intl as in the country type field. The code of twig extension is

<?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';
    }
}
?>

Add twig extension in services.yml

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

usage in the template (the country name will be display in locale by default (see Symfony/Component/Intl/ResourceBundle/RegionBundleInterface.php)

{{ user.countryCode|countryName }}

Many thanks Yassir, this version don't use locale deprecated since version 2.3 >> http://symfony.com/components/Locale



回答4:

You can use the same component that Symfony is using for country Field Type

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

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


回答5:

Use SonanaIntlBundle, you could do something like this:

{{ '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)

Read more about this



回答6:

Well if you are using entities one option instead of doing twig filters is to create function for getting country name inside entity.

use Symfony\Component\Intl\Intl;

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

So in twig later you can do

{{ user.countryName }}


回答7:

With a twig filter (http://symfony.com/doc/current/cookbook/templating/twig_extension.html):

new \Twig_SimpleFilter('country_name', function($value) {
    return \Symfony\Component\Intl\Intl::getRegionBundle()->getCountryName($value);
}),

usage in a twig template

{{ 'GB' | country_name }} {# displays United Kingdom #} 

worked for me in Symfony 2.3