How to Install the Intl Extension for Twig

How to install the Intl extension for Twig

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

How to enable the Intl extension from twig/intl-extra

When using twig with Symfony, you should register the extension as a service in your services.yaml file.

If installed using the twig/extensions flex recipe, it will provide the config/packages/twig_extensions.yaml file, you just have to uncomment the desired extensions (see the original comment on github).

The default configuration for reference:

services:
_defaults:
public: false
autowire: true
autoconfigure: true

# Uncomment any lines below to activate that Twig extension
#Twig\Extensions\ArrayExtension: null
#Twig\Extensions\DateExtension: null
#Twig\Extensions\IntlExtension: null
#Twig\Extensions\TextExtension: null

Symfony - PHP-Intl Extension

This should help you:

http://symfony.com/doc/current/components/intl.html

"require: {
"symfony/icu": "1.1.*"
}

Set the version to

"1.0.*" if the server does not have the intl extension installed;
"1.1.*" if the server is compiled with ICU 4.2 or lower.

EDIT:

Based on your require block.

Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing jdorn/sql-formatter (v1.2.17)
Loading from cache

- Installing psr/log (1.0.0)
Loading from cache

- Installing twig/twig (v1.16.0)
Loading from cache

- Installing doctrine/lexer (v1.0)
Loading from cache

- Installing doctrine/annotations (v1.2.0)
Loading from cache

- Installing doctrine/collections (v1.2)
Loading from cache

- Installing doctrine/cache (v1.3.0)
Loading from cache

- Installing doctrine/inflector (v1.0)
Loading from cache

- Installing doctrine/common (v2.4.2)
Loading from cache

- Removing symfony/finder (v2.5.3)
- Installing symfony/symfony (v2.5.3)
Loading from cache

- Installing symfony/icu (v1.0.1)
Downloading: 100%

- Installing doctrine/dbal (v2.4.2)
Loading from cache

- Installing doctrine/doctrine-bundle (v1.2.0)
Loading from cache

- Installing kriswallsmith/assetic (v1.1.2)
Loading from cache

- Installing symfony/assetic-bundle (v2.3.0)
Loading from cache

- Installing doctrine/orm (v2.4.4)
Loading from cache

- Installing twig/extensions (v1.1.0)
Loading from cache

- Installing swiftmailer/swiftmailer (v5.2.1)
Loading from cache

- Installing symfony/swiftmailer-bundle (v2.3.7)
Loading from cache

- Installing monolog/monolog (1.10.0)
Loading from cache

- Installing symfony/monolog-bundle (v2.6.1)
Loading from cache

- Installing sensiolabs/security-checker (v2.0.0)
Loading from cache

- Installing sensio/distribution-bundle (v3.0.4)
Loading from cache

- Installing sensio/framework-extra-bundle (v3.0.1)
Loading from cache

- Installing incenteev/composer-parameter-handler (v2.1.0)
Loading from cache

Symfony doesn't detect intl extension (running on XAMPP)

I don't understand precisely why but this fixed my problem : i updated the Symfony Intl package (from 5.0.4 to 5.0.7) inside my project and the message disappeared.

Symfony 2 - Install and enable the intl extension

I have found the solution in this answer

Copy all your icu*.dll files to wamp/www/bin/php/php.version/ in wamp/www/bin/apache/apache.version/bin/

How to install twig localizeddate filter?

You're looking for the twig/extensions composer package, the github repo is at fabpot/Twig-extensions.

You mention you're using twig standalone, then you must have a Twig_Environment object. It has an addExtension method, you need to call it, passing a new instance of the Intl extension:

$env->addExtension(new Twig_Extensions_Extension_Intl());

Install twig/extensions on a Symfony 4 or Symfony 5 new project

Finally it seems that running composer require twig/extensions is no more required to create custom Twig Extensions in Symfony 4.
By default a new Symfony built with website skeleton owes a use Twig\Extension\AbstractExtension; as per required to create a twig extension like below one :

namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class AppExtension extends AbstractExtension
{
public function getFilters()
{
return [
new TwigFilter('price', [$this, 'formatPrice']),
];
}

public function formatPrice($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
{
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
$price = '$'.$price;

return $price;
}
}

Twig i18n extension

After considerable effort, I've found that PhpMyAdmin has forked the extension I'm looking for, and have updated it to work with the new version of Twig. Seems like I'm not the only person who is having this issue .......

For those interested, you can find updated code here: https://github.com/phpmyadmin/twig-i18n-extension



Related Topics



Leave a reply



Submit