php 如何为 Twig 安装 Intl 扩展

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25948853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:04:36  来源:igfitidea点击:

How to install the Intl extension for Twig

phptwigintltwig-extension

提问by julestruong

The Intl extensionis an extension for Twig that adds the localizeddate, localizednumberand localizedcurrencyfilters. How can I install and set up the extension so that I can use those filters in my Twig templates?

国际扩展是枝条的延伸,增加了的localizeddatelocalizednumberlocalizedcurrency过滤器。如何安装和设置扩展以便我可以在我的 Twig 模板中使用这些过滤器?

回答by Nic Wortel

Install the PHP intl extension

安装 PHP 国际扩展

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 instructionscan be found in the official PHP documentation.

首先,您将需要PHP intl 扩展,因为 Twig 扩展是在此基础上构建的。如果未启用 PHP 国际扩展,Twig 国际扩展将抛出异常。安装说明可以在官方 PHP 文档中找到。

On Ubuntu/Debian machines, this is as easy as running the following command:

在 Ubuntu/Debian 机器上,这就像运行以下命令一样简单:

sudo apt install php-intl

On Windows machines, you probably have to uncomment the following line in php.ini:

在 Windows 机器上,您可能必须在 php.ini 中取消注释以下行:

extension=php_intl.dll

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-pearand yum install gcc-c++.

对于 CentOS 或其他架构,请按照此处的说明进行操作。请注意,CentOS 需要安装 PECL 和 GCC C++ 编译器:yum install php-pearyum install gcc-c++.

Once the extension is added to php.ini, then restart the web server.

将扩展添加到 php.ini 后,然后重新启动 Web 服务器。

Install the Twig Extensions

安装 Twig 扩展

Next, you will need the Twig Extensionspackage (that contains the Intl extension, among others), which can be installed using Composer. Run this command in the command line:

接下来,您将需要可以使用 Composer 安装的Twig Extensions包(其中包含 Intl 扩展等)。在命令行中运行此命令:

composer require twig/extensions

This will add the dependency to your composer.jsonand download it.

这会将依赖项添加到您的composer.json并下载它。

Note: the localizednumberand localizedcurrencyfilters were introduced in version 1.2.0, so you need at least that version if you want to use them.

注意:localizednumberlocalizedcurrency过滤器是在 1.2.0 版本中引入的,所以如果你想使用它们,你至少需要那个版本。

Adding the extension to Twig

将扩展添加到 Twig

If you are using Twig directly (i.e. not in a Symfony project), add the extension to the Twig environment manually:

如果您直接使用 Twig(即不在 Symfony 项目中),请手动将扩展添加到 Twig 环境:

<?php

use Twig\Environment;
use Twig\Extensions\IntlExtension;

$twig = new Environment($loader);
$twig->addExtension(new IntlExtension());

Adding the extension to Twig (in Symfony)

将扩展添加到 Twig(在 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:

如果您使用的是 Symfony 应用程序,您可以通过创建一个服务并将其标记为 Twig 扩展来将扩展添加到 Twig config/services.yml

services:
    twig.extension.intl:
        class: Twig\Extensions\IntlExtension
        tags:
            - { name: twig.extension }

Setting the default locale

设置默认语言环境

<?php

Locale::setDefault('nl-NL');

Setting the default locale in Symfony

在 Symfony 中设置默认语言环境

In config/framework.yaml, uncomment the default_localesetting:

在 中config/framework.yaml,取消注释default_locale设置:

framework:
    default_locale: en

回答by emix

In Symfony 3/4/5, with the autoconfiguration feature enabled, it's as easy as registering the extension as a service:

在 Symfony 3/4/5 中,启用自动配置功能后,就像将扩展注册为服务一样简单:

// config/services.yaml

services:
    …

    Twig\Extensions\IntlExtension: ~ 

回答by Suhaib Ahmad

After Installation of extension, if you find following error: Attempted to load class "IntlTimeZone" from the global namespace. Did you forget a "use" statement? Just edit line 54 in the file:

安装扩展后,如果您发现以下错误: Attempted to load class "IntlTimeZone" from the global namespace。您是否忘记了“使用”语句?只需编辑文件中的第 54 行:

    vendor/twig/lib/Twig/Extensions/Extension/Intl.php

and replace it to:

并将其替换为:

if (PHP_VERSION_ID < 50500 || !class_exists('IntlTimeZone')) {

It worked for me..

它对我有用..