Pol*_*ino 24 symfony-forms symfony twig
我正在使用Symfony2 国家/地区字段类型,它运行良好并且国家/地区名称已翻译.我将两位数的国家/地区代码存储在country我的实体列中.
如何显示完整的已翻译国家/地区名称?这是我将字段添加到表单的方式:
$builder
->add('country', 'country', array(
'label' => 'Paese', 'preferred_choices' => array('IT')
));
Run Code Online (Sandbox Code Playgroud)
然后在我的控制器中:
$user = $this->getDoctrine()->getRepository('AcmeHelloBundle:User');
$countryCode = $user->getCountry();
$countryName = null; // Get translated country name from code
Run Code Online (Sandbox Code Playgroud)
或者在我的树枝模板中:
{# Output the country code and name #}
{{ user.country }}
{# translated country name from code #}
Run Code Online (Sandbox Code Playgroud)
Han*_*sir 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';
}
}
Run Code Online (Sandbox Code Playgroud)
并在您的services.yml文件中
# src/Acme/DemoBundle/Resources/config/services.yml
services:
acme.twig.country_extension:
class: Acme\DemoBundle\Twig\CountryExtension
tags:
- { name: twig.extension }
Run Code Online (Sandbox Code Playgroud)
twig文件中的用法示例:
{{ 'US'|country(app.request.locale) }}
Run Code Online (Sandbox Code Playgroud)
小智 18
根据@Rvanlaak上面的评论,\ Symfony\Component\Locale\Locale现已弃用.我认为现在最简洁的方法是:
use Symfony\Component\Intl\Intl;
...
$country = Intl::getRegionBundle()->getCountryName($countryCode);
Run Code Online (Sandbox Code Playgroud)
Fab*_*e G 16
受Hannoun Yassir回答的启发,我在国家类型字段中使用Intl.树枝延伸的代码是
<?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';
}
}
?>
Run Code Online (Sandbox Code Playgroud)
在services.yml中添加twig扩展
# src/Acme/DemoBundle/Resources/config/services.yml
services:
acme.twig.acme_extension:
class: Acme\DemoBundle\Twig\CountryExtension
tags:
- { name: twig.extension }
Run Code Online (Sandbox Code Playgroud)
模板中的用法(默认情况下,国家/地区名称将以区域设置显示(请参阅Symfony/Component/Intl/ResourceBundle/RegionBundleInterface.php)
{{ user.countryCode|countryName }}
Run Code Online (Sandbox Code Playgroud)
非常感谢Yassir,这个版本不使用自版本2.3以来不推荐使用的语言环境>> http://symfony.com/components/Locale
smo*_*eno 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)
Run Code Online (Sandbox Code Playgroud)
sbc*_*czk 11
您可以使用Symfony用于国家/地区字段类型的相同组件
public function humanCountry() {
$c = \Symfony\Component\Locale\Locale::getDisplayCountries('en');
return array_key_exists($this->getCountry(), $c)
? $c[$this->getCountry()]
: $this->getCountry();
}
Run Code Online (Sandbox Code Playgroud)
小智 9
好吧,如果你使用实体,一个选项而不是做枝条过滤器是创建功能,以获取实体内的国家名称.
use Symfony\Component\Intl\Intl;
public function getCountryName() {
return Intl::getRegionBundle()->getCountryName($this->getCountry());
}
Run Code Online (Sandbox Code Playgroud)
所以在树枝后你可以做到
{{ user.countryName }}
Run Code Online (Sandbox Code Playgroud)
使用树枝过滤器(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);
}),
Run Code Online (Sandbox Code Playgroud)
树枝模板中的用法
{{ 'GB' | country_name }} {# displays United Kingdom #}
Run Code Online (Sandbox Code Playgroud)
在Symfony 2.3中为我工作