Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function twig *_name for intl #3742

Merged
merged 2 commits into from Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions doc/functions/country_names.rst
@@ -0,0 +1,47 @@
``country_names``
=================

seb-jean marked this conversation as resolved.
Show resolved Hide resolved
.. 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());
seb-jean marked this conversation as resolved.
Show resolved Hide resolved

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