How to install the Intl extension for Twig

前端 未结 3 854
滥情空心
滥情空心 2020-12-02 15:56

The Intl extension is an extension for Twig that adds the localizeddate, localizednumber and localizedcurrency filters. How can I inst

相关标签:
3条回答
  • 2020-12-02 16:18

    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.

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

    sudo apt install php-intl
    

    On Windows machines, you probably have to uncomment the following line in 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-pear and yum install gcc-c++.

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

    Install the Twig Extensions

    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
    

    This will add the dependency to your composer.json and download it.

    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.

    Adding the extension to 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());
    

    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 }
    

    Setting the default locale

    <?php
    
    Locale::setDefault('nl-NL');
    

    Setting the default locale in Symfony

    In config/framework.yaml, uncomment the default_locale setting:

    framework:
        default_locale: en
    
    0 讨论(0)
  • 2020-12-02 16:23

    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:

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

    and replace it to:

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

    It worked for me..

    0 讨论(0)
  • 2020-12-02 16:39

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

    // config/services.yaml
    
    services:
        …
    
        Twig\Extensions\IntlExtension: ~ 
    
    0 讨论(0)
提交回复
热议问题