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?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
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:
On Windows machines, you probably have to uncomment the following line in php.ini:
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
andyum 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:
This will add the dependency to your
composer.json
and download it.Note: the
localizednumber
andlocalizedcurrency
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 in a simple PHP project, you can add the extension using the
addExtension()
method.Adding the extension to Twig (in Symfony)
If you are using a Symfony application, you can add the extension to Twig using the service configuration in
app/config/services.yml
:Setting the locale
Setting the locale in Symfony
Edit
app/config/config.yml
: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:
and replace it to:
It worked for me..