Symfony 4:使用本地化日期
Symfony 4: use localizeddate
我正在尝试使用 localizeddate
在我的网站上以法语显示日期:
<td>{{ saison.start|localizeddate('medium', 'none') }}</td>
我遵循了各种文档并且:
- 我安装了 PHP 国际扩展:
sudo pacman -S php-intl
- 我在 Symfony 上安装了 Twig 扩展:
composer require twig/extensions
- 以及国际扩展:
composer require symfony/intl
我也编辑了config/services.yaml
来更改语言环境,但似乎没有效果:
parameters:
locale: 'fr'
我在文件 config
packages\twig_extensions.yaml`:
中启用了日期和国际扩展
services:
_defaults:
public: false
autowire: true
autoconfigure: true
# Uncomment any lines below to activate that Twig extension
#Twig\Extensions\ArrayExtension: ~
Twig\Extensions\DateExtension: ~
Twig\Extensions\IntlExtension: ~
#Twig\Extensions\TextExtension: ~
但是我在输出文件中仍然有英文显示的日期。
我还尝试在 localizeddate
中更清楚地指定语言环境,如下所示:
<td>{{ saison.start|localizeddate('medium', 'none', 'fr') }}</td>
但在这种情况下我得到一个错误:
An exception has been thrown during the rendering of a template ("The Symfony\Component\Intl\DateFormatter\IntlDateFormatter::__construct() method's argument $locale value 'fr' behavior is not implemented. Only the locale "en" is supported. Please install the "intl" extension for full localization capabilities.").
不知道少了什么
因为我没有找到正确答案,所以我是这样解决问题的(因为我不需要国际支持):
<date variable>|date('d/m/Y')
这并不令人满意,因为 localizedate
正是为此目的而完成的,但同时它起作用了。
过滤器名称已更改。
您现在应该使用 twig/intl-extra
包提供的 format_datetime
。
https://twig.symfony.com/doc/2.x/filters/format_datetime.html
似乎 PHP 国际扩展未正确安装,因为 symfony/intl
仅适用于 en
语言环境。
The replacement layer is limited to the en
locale. If you want to use other locales, you should install the intl extension. There is no conflict between the two because, even if you use the extension, this package can still be useful to access the ICU data.
https://symfony.com/doc/current/components/intl.html
如果您查找 Symfony\Component\Intl\NumberFormatter
的 __construct()
方法,您可以了解您的代码引发此异常的原因:
if ('en' !== $locale && null !== $locale) {
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'locale', $locale, 'Only the locale "en" is supported');
}
https://github.com/symfony/intl/blob/v4.0.0/NumberFormatter/NumberFormatter.php#L262
我正在尝试使用 localizeddate
在我的网站上以法语显示日期:
<td>{{ saison.start|localizeddate('medium', 'none') }}</td>
我遵循了各种文档并且:
- 我安装了 PHP 国际扩展:
sudo pacman -S php-intl
- 我在 Symfony 上安装了 Twig 扩展:
composer require twig/extensions
- 以及国际扩展:
composer require symfony/intl
我也编辑了config/services.yaml
来更改语言环境,但似乎没有效果:
parameters:
locale: 'fr'
我在文件 config
packages\twig_extensions.yaml`:
services:
_defaults:
public: false
autowire: true
autoconfigure: true
# Uncomment any lines below to activate that Twig extension
#Twig\Extensions\ArrayExtension: ~
Twig\Extensions\DateExtension: ~
Twig\Extensions\IntlExtension: ~
#Twig\Extensions\TextExtension: ~
但是我在输出文件中仍然有英文显示的日期。
我还尝试在 localizeddate
中更清楚地指定语言环境,如下所示:
<td>{{ saison.start|localizeddate('medium', 'none', 'fr') }}</td>
但在这种情况下我得到一个错误:
An exception has been thrown during the rendering of a template ("The Symfony\Component\Intl\DateFormatter\IntlDateFormatter::__construct() method's argument $locale value 'fr' behavior is not implemented. Only the locale "en" is supported. Please install the "intl" extension for full localization capabilities.").
不知道少了什么
因为我没有找到正确答案,所以我是这样解决问题的(因为我不需要国际支持):
<date variable>|date('d/m/Y')
这并不令人满意,因为 localizedate
正是为此目的而完成的,但同时它起作用了。
过滤器名称已更改。
您现在应该使用 twig/intl-extra
包提供的 format_datetime
。
https://twig.symfony.com/doc/2.x/filters/format_datetime.html
似乎 PHP 国际扩展未正确安装,因为 symfony/intl
仅适用于 en
语言环境。
The replacement layer is limited to the
en
locale. If you want to use other locales, you should install the intl extension. There is no conflict between the two because, even if you use the extension, this package can still be useful to access the ICU data.
https://symfony.com/doc/current/components/intl.html
如果您查找 Symfony\Component\Intl\NumberFormatter
的 __construct()
方法,您可以了解您的代码引发此异常的原因:
if ('en' !== $locale && null !== $locale) {
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'locale', $locale, 'Only the locale "en" is supported');
}
https://github.com/symfony/intl/blob/v4.0.0/NumberFormatter/NumberFormatter.php#L262