Skip to content

Commit

Permalink
feature #3742 Add function twig *_name for intl (seb-jean, fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x branch.

Discussion
----------

Add function twig *_name for intl

Hi,

I add `language_names`, `script_names`, `country_names`, `locale_names`, `currency_names`, `timezone_names`.

It allows us to have a list but on the Twig side and to use them in filters for example.

Commits
-------

6f9c7c0 Fix tests
b376bdd Add function twig *_name for intl list
  • Loading branch information
fabpot committed Dec 27, 2022
2 parents 4a7c8b3 + 6f9c7c0 commit 3b46c89
Show file tree
Hide file tree
Showing 13 changed files with 440 additions and 1 deletion.
47 changes: 47 additions & 0 deletions doc/functions/country_names.rst
@@ -0,0 +1,47 @@
``country_names``
=================

.. versionadded:: 3.5

The ``country_names`` function was added in Twig 3.5.

The ``country_names`` function returns the names of the countries:

.. code-block:: twig
{# Afghanistan, Åland Islands, ... #}
{{ country_names()|join(', ') }}
By default, the function uses the current locale. You can pass it explicitly:

.. code-block:: twig
{# Afghanistan, Afrique du Sud, ... #}
{{ country_names('fr')|join(', ') }}
.. note::

The ``country_names`` function is part of the ``IntlExtension`` which is not
installed by default. Install it first:

.. code-block:: bash
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:

.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::

use Twig\Extra\Intl\IntlExtension;

$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());

Arguments
---------

* ``locale``: The locale
47 changes: 47 additions & 0 deletions doc/functions/currency_names.rst
@@ -0,0 +1,47 @@
``currency_names``
==================

.. versionadded:: 3.5

The ``currency_names`` function was added in Twig 3.5.

The ``currency_names`` function returns the names of the currencies:

.. code-block:: twig
{# Afghan Afghani, Afghan Afghani (1927–2002), ... #}
{{ currency_names()|join(', ') }}
By default, the function uses the current locale. You can pass it explicitly:

.. code-block:: twig
{# afghani (1927–2002), afghani afghan, ... #}
{{ currency_names('fr')|join(', ') }}
.. note::

The ``currency_names`` function is part of the ``IntlExtension`` which is not
installed by default. Install it first:

.. code-block:: bash
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:

.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::

use Twig\Extra\Intl\IntlExtension;

$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());

Arguments
---------

* ``locale``: The locale
47 changes: 47 additions & 0 deletions doc/functions/language_names.rst
@@ -0,0 +1,47 @@
``language_names``
==================

.. versionadded:: 3.5

The ``language_names`` function was added in Twig 3.5.

The ``language_names`` function returns the names of the languages:

.. code-block:: twig
{# Abkhazian, Achinese, ... #}
{{ language_names()|join(', ') }}
By default, the function uses the current locale. You can pass it explicitly:

.. code-block:: twig
{# abkhaze, aceh, ... #}
{{ language_names('fr')|join(', ') }}
.. note::

The ``language_names`` function is part of the ``IntlExtension`` which is not
installed by default. Install it first:

.. code-block:: bash
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:

.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::

use Twig\Extra\Intl\IntlExtension;

$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());

Arguments
---------

* ``locale``: The locale
47 changes: 47 additions & 0 deletions doc/functions/locale_names.rst
@@ -0,0 +1,47 @@
``locale_names``
================

.. versionadded:: 3.5

The ``locale_names`` function was added in Twig 3.5.

The ``locale_names`` function returns the names of the locales:

.. code-block:: twig
{# Afrikaans, Afrikaans (Namibia), ... #}
{{ locale_names()|join(', ') }}
By default, the function uses the current locale. You can pass it explicitly:

.. code-block:: twig
{# afrikaans, afrikaans (Afrique du Sud), ... #}
{{ locale_names('fr')|join(', ') }}
.. note::

The ``locale_names`` function is part of the ``IntlExtension`` which is not
installed by default. Install it first:

.. code-block:: bash
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:

.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::

use Twig\Extra\Intl\IntlExtension;

$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());

Arguments
---------

* ``locale``: The locale
47 changes: 47 additions & 0 deletions doc/functions/script_names.rst
@@ -0,0 +1,47 @@
``script_names``
================

.. versionadded:: 3.5

The ``script_names`` function was added in Twig 3.5.

The ``script_names`` function returns the names of the scripts:

.. code-block:: twig
{# Adlam, Afaka, ... #}
{{ script_names()|join(', ') }}
By default, the function uses the current locale. You can pass it explicitly:

.. code-block:: twig
{# Adlam, Afaka, ... #}
{{ script_names('fr')|join(', ') }}
.. note::

The ``script_names`` function is part of the ``IntlExtension`` which is not
installed by default. Install it first:

.. code-block:: bash
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:

.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::

use Twig\Extra\Intl\IntlExtension;

$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());

Arguments
---------

* ``locale``: The locale
47 changes: 47 additions & 0 deletions doc/functions/timezone_names.rst
@@ -0,0 +1,47 @@
``timezone_names``
==================

.. versionadded:: 3.5

The ``timezone_names`` function was added in Twig 3.5.

The ``timezone_names`` function returns the names of the timezones:

.. code-block:: twig
{# Acre Time (Eirunepe), Acre Time (Rio Branco), ... #}
{{ timezone_names()|join(', ') }}
By default, the function uses the current locale. You can pass it explicitly:

.. code-block:: twig
{# heure : Antarctique (Casey), heure : Canada (Montreal), ... #}
{{ timezone_names('fr')|join(', ') }}
.. note::

The ``timezone_names`` function is part of the ``IntlExtension`` which is not
installed by default. Install it first:

.. code-block:: bash
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:

.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::

use Twig\Extra\Intl\IntlExtension;

$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());

Arguments
---------

* ``locale``: The locale
63 changes: 62 additions & 1 deletion extra/intl-extra/IntlExtension.php
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Intl\Exception\MissingResourceException;
use Symfony\Component\Intl\Languages;
use Symfony\Component\Intl\Locales;
use Symfony\Component\Intl\Scripts;
use Symfony\Component\Intl\Timezones;
use Twig\Environment;
use Twig\Error\RuntimeError;
Expand Down Expand Up @@ -152,6 +153,12 @@ public function getFunctions()
return [
// internationalized names
new TwigFunction('country_timezones', [$this, 'getCountryTimezones']),
new TwigFunction('language_names', [$this, 'getLanguageNames']),
new TwigFunction('script_names', [$this, 'getScriptNames']),
new TwigFunction('country_names', [$this, 'getCountryNames']),
new TwigFunction('locale_names', [$this, 'getLocaleNames']),
new TwigFunction('currency_names', [$this, 'getCurrencyNames']),
new TwigFunction('timezone_names', [$this, 'getTimezoneNames']),
];
}

Expand Down Expand Up @@ -242,6 +249,60 @@ public function getCountryTimezones(string $country): array
}
}

public function getLanguageNames(string $locale = null): array
{
try {
return Languages::getNames($locale);
} catch (MissingResourceException $exception) {
return [];
}
}

public function getScriptNames(string $locale = null): array
{
try {
return Scripts::getNames($locale);
} catch (MissingResourceException $exception) {
return [];
}
}

public function getCountryNames(string $locale = null): array
{
try {
return Countries::getNames($locale);
} catch (MissingResourceException $exception) {
return [];
}
}

public function getLocaleNames(string $locale = null): array
{
try {
return Locales::getNames($locale);
} catch (MissingResourceException $exception) {
return [];
}
}

public function getCurrencyNames(string $locale = null): array
{
try {
return Currencies::getNames($locale);
} catch (MissingResourceException $exception) {
return [];
}
}

public function getTimezoneNames(string $locale = null): array
{
try {
return Timezones::getNames($locale);
} catch (MissingResourceException $exception) {
return [];
}
}

public function formatCurrency($amount, string $currency, array $attrs = [], string $locale = null): string
{
$formatter = $this->createNumberFormatter($locale, 'currency', $attrs);
Expand Down Expand Up @@ -279,7 +340,7 @@ public function formatNumberStyle(string $style, $number, array $attrs = [], str
*/
public function formatDateTime(Environment $env, $date, ?string $dateFormat = 'medium', ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', string $locale = null): string
{
$date = \twig_date_converter($env, $date, $timezone);
$date = twig_date_converter($env, $date, $timezone);
$formatter = $this->createDateFormatter($locale, $dateFormat, $timeFormat, $pattern, $date->getTimezone(), $calendar);

if (false === $ret = $formatter->format($date)) {
Expand Down
16 changes: 16 additions & 0 deletions extra/intl-extra/Tests/Fixtures/country_names.test
@@ -0,0 +1,16 @@
--TEST--
"country_names" function
--TEMPLATE--
{{ country_names('UNKNOWN')|length }}
{{ country_names()|length }}
{{ country_names('fr')|length }}
{{ country_names()['BE'] }}
{{ country_names('fr')['BE'] }}
--DATA--
return [];
--EXPECT--
0
249
249
Belgium
Belgique

0 comments on commit 3b46c89

Please sign in to comment.