<p>You need to register the extension with the Twig environment. First, install the extension with Composer. Then see the code example in the documentation page you linked:</p>
<pre class="lang-php prettyprint-override"><code>$twig = new \Twig\Environment(...);
$twig-&gt;addExtension(new IntlExtension());
</code></pre>
<p>This adds the extension to the environment so its filters will be available in your templates. Of course, Craft creates the Twig environment for you, so you need to use hooks to register the extension at the appropriate point in the lifecycle. You can put the following code in a simple <a href="https://craftcms.com/docs/3.x/extend/module-guide.html" rel="nofollow noreferrer">site module</a>:</p>
<pre class="lang-php prettyprint-override"><code>use Twig\Extra\Intl\IntlExtension;

public function init()
{
    parent::init();

    if (Craft::$app-&gt;request-&gt;getIsSiteRequest()) {
        $extension = new IntlExtension();
        Craft::$app-&gt;view-&gt;registerTwigExtension($extension);
    }
}
</code></pre>
<p>See <a href="https://craftcms.com/docs/3.x/extend/extending-twig.html#register-a-twig-extension" rel="nofollow noreferrer">Register a Twig Extension</a> for details.</p>

CC BY-SA 4.0
2021-12-21T03:31:38.623Z
Thanks for the details. Do we need to develop a module/plugin to register the extension to use in craft cms? or, is there a way to proceed without them?
Parthiban
CC BY-SA 4.0
2021-12-21T11:55:30.053Z
@Parthiban Modules and plugins aren't the same thing. Modules are site-specific components that can contain as little or as much you need them to. Creating a small module to include the hook code above is very simple and only takes a couple of minutes. Check the link above to get started. You can also create a starter module on [pluginfactory.io](https://pluginfactory.io/), this will help you get started.
MoritzLost
CC BY-SA 4.0
2021-12-22T06:40:15.160Z
Sure, I will do a simple module and proceed. Thanks.
Parthiban
CC BY-SA 4.0
2021-12-20T09:37:07.697Z
2021-12-20T09:37:07.697Z
MoritzLost
<p>Craft CMS doesn't have TWIG filter country_name. Even by default country name filter package is not available in twig. We need to install the corresponding package. <a href="https://twig.symfony.com/doc/3.x/filters/country_name.html" rel="nofollow noreferrer">https://twig.symfony.com/doc/3.x/filters/country_name.html</a></p>
<p>How can I install the package and use country_name filter in Craft CMS?</p>
<p><code>composer require twig/intl-extra</code> installs the package. But still, the filter is not working. Am I missing anything? Is the installation method works with Craft cms?</p>

<templating><filter>
Twig filter country_name in craft CMS
# Twig filter country_name in craft CMS

(Asked by: username_0 on 2021-12-20)

Craft CMS doesn't have TWIG filter country\_name. Even by default country name filter package is not available in twig. We need to install the corresponding package. <https://twig.symfony.com/doc/3.x/filters/country_name.html>

How can I install the package and use country\_name filter in Craft CMS?

`composer require twig/intl-extra` installs the package. But still, the filter is not working. Am I missing anything? Is the installation method works with Craft cms?

**Tags:** templating, filter

---

## 1 answers

### Answer by username_1 on 2021-12-20. Score: 2 | Accepted answer

You need to register the extension with the Twig environment. First, install the extension with Composer. Then see the code example in the documentation page you linked:
$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());

This adds the extension to the environment so its filters will be available in your templates. Of course, Craft creates the Twig environment for you, so you need to use hooks to register the extension at the appropriate point in the lifecycle. You can put the following code in a simple site module:
use Twig\Extra\Intl\IntlExtension;

public function init()
{
    parent::init();

    if (Craft::$app->request->getIsSiteRequest()) {
        $extension = new IntlExtension();
        Craft::$app->view->registerTwigExtension($extension);
    }
}

See Register a Twig Extension for details.

> **Comments:**
> **username_0**: Thanks for the details. Do we need to develop a module/plugin to register the extension to use in craft cms? or, is there a way to proceed without them?  
> **username_1**: @Parthiban Modules and plugins aren't the same thing. Modules are site-specific components that can contain as little or as much you need them to. Creating a small module to include the hook code above is very simple and only takes a couple of minutes. Check the link above to get started. You can also create a starter module on [pluginfactory.io](https://pluginfactory.io/), this will help you get started.  
> **username_0**: Sure, I will do a simple module and proceed. Thanks.  

---

