如何为Twig安装Intl扩展 [英] How to install the Intl extension for Twig

查看:85
本文介绍了如何为Twig安装Intl扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

国际扩展是Twig的扩展,其中添加了localizeddatelocalizednumberlocalizedcurrency过滤器.如何安装和设置扩展,以便可以在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国际扩展,作为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-pearyum 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.

注意:localizednumberlocalizedcurrency过滤器是在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 Twig\Environment;
use Twig\Extensions\IntlExtension;

$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: Twig\Extensions\IntlExtension
        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扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆