1

我有一个包含所有国家/地区名称的数组,我想翻译这些名称,但我不知道如何

在我的控制器中,我做了:

$countries = array("Afghanistan", "Albania", "Algeria", "Angola", ....);

return $this->render('xBundle:x:xs.html.twig', array('countries' => json_encode($countries))

在我的模板中,我有:

List : {{countries}}
4

1 回答 1

3

由于 symfony 2.3 国家名称可以使用 Intl 和RegionBundle进行翻译。

默认返回的数组getCountryNames()如下所示:

=> 数组('AF' => '阿富汗', ...)

如果您只对国家/地区名称感兴趣,请使用以下内容:

use Symfony\Component\Intl\Intl;

// get all country names in locale "locale"
$countries = array_values(Intl::getRegionBundle()->getCountryNames('de'));

// get all country names for current locale
$countries = array_values(Intl::getRegionBundle()->getCountryNames());

...如果您只想使用翻译器翻译数组。

$translator = $this->get('translator');

foreach ($countries as $key => $country) {
    $countries[$key] = $translator->trans($country, array(), null, 'de');
}

请参阅翻译 API 文档并阅读说明书章节翻译

于 2013-09-12T12:43:37.377 回答