How to install the Intl extension for Twig(如何为 Twig 安装 Intl 扩展)
问题描述
Intl extension 是 Twig 的扩展,它添加了 localizeddate
、localizednumber
和 localizedcurrency
过滤器.如何安装和设置扩展,以便在我的 Twig 模板中使用这些过滤器?
The Intl extension is an extension for Twig that adds the localizeddate
, localizednumber
and localizedcurrency
filters. How can I install and set up the extension so that I can use those filters in my Twig templates?
推荐答案
安装 PHP intl 扩展
首先,您需要 PHP intl extension,作为Twig 扩展建立在此之上.如果未启用 PHP intl 扩展,Twig Intl 扩展将抛出异常.安装说明可以在PHP官方文档中找到.
Install the PHP intl extension
First of all, you will need the PHP intl extension, as the Twig extension is built on top of that. The Twig Intl extension will throw an Exception if the PHP intl extension is not enabled. Installation instructions can be found in the official PHP documentation.
在 Ubuntu/Debian 机器上,这就像运行以下命令一样简单:
On Ubuntu/Debian machines, this is as easy as running the following command:
sudo apt install php-intl
在 Windows 机器上,您可能必须取消注释 php.ini 中的以下行:
On Windows machines, you probably have to uncomment the following line in php.ini:
extension=php_intl.dll
对于 CentOS 或其他架构,请按照 此处的说明进行操作.请注意,CentOS 需要同时安装 PECL 和 GCC C++ 编译器:yum install php-pear
和 yum install gcc-c++
.
For CentOS, or other architectures, follow the instructions here. Note that CentOS requires both PECL and the GCC C++ compiler to be installed: yum install php-pear
and yum install gcc-c++
.
将扩展添加到 php.ini 后,重新启动 Web 服务器.
Once the extension is added to php.ini, then restart the web server.
接下来,您将需要 Twig Extensions 包(包含 Intl 扩展等),可以使用 Composer 安装.在命令行中运行这个命令:
Next, you will need the Twig Extensions package (that contains the Intl extension, among others), which can be installed using Composer. Run this command in the command line:
composer require twig/extensions
这会将依赖项添加到您的 composer.json
并下载它.
This will add the dependency to your composer.json
and download it.
注意:localizednumber
和 localizedcurrency
过滤器是在 1.2.0 版本中引入的,因此如果要使用它们,至少需要该版本.
Note: the localizednumber
and localizedcurrency
filters were introduced in version 1.2.0, so you need at least that version if you want to use them.
如果您直接使用 Twig(即不在 Symfony 项目中),请手动将扩展添加到 Twig 环境中:
If you are using Twig directly (i.e. not in a Symfony project), add the extension to the Twig environment manually:
<?php
use TwigEnvironment;
use TwigExtensionsIntlExtension;
$twig = new Environment($loader);
$twig->addExtension(new IntlExtension());
将扩展添加到 Twig(在 Symfony 中)
如果您使用的是 Symfony 应用程序,您可以通过创建服务并将其标记为 config/services.yml
中的 Twig 扩展来将扩展添加到 Twig:
Adding the extension to Twig (in Symfony)
If you are using a Symfony application, you can add the extension to Twig by creating a service and tagging it as a Twig extension in config/services.yml
:
services:
twig.extension.intl:
class: TwigExtensionsIntlExtension
tags:
- { name: twig.extension }
设置默认语言环境
<?php
Locale::setDefault('nl-NL');
在 Symfony 中设置默认语言环境
在 config/framework.yaml
中,取消注释 default_locale
设置:
Setting the default locale in Symfony
In config/framework.yaml
, uncomment the default_locale
setting:
framework:
default_locale: en
这篇关于如何为 Twig 安装 Intl 扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何为 Twig 安装 Intl 扩展
基础教程推荐
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01