diff --git a/doc/functions/country_names.rst b/doc/functions/country_names.rst new file mode 100644 index 000000000..692137b04 --- /dev/null +++ b/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 diff --git a/doc/functions/currency_names.rst b/doc/functions/currency_names.rst new file mode 100644 index 000000000..dfb446c86 --- /dev/null +++ b/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 diff --git a/doc/functions/language_names.rst b/doc/functions/language_names.rst new file mode 100644 index 000000000..f1cce488a --- /dev/null +++ b/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 diff --git a/doc/functions/locale_names.rst b/doc/functions/locale_names.rst new file mode 100644 index 000000000..320ab6724 --- /dev/null +++ b/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 diff --git a/doc/functions/script_names.rst b/doc/functions/script_names.rst new file mode 100644 index 000000000..79b20c65f --- /dev/null +++ b/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 diff --git a/doc/functions/timezone_names.rst b/doc/functions/timezone_names.rst new file mode 100644 index 000000000..69db196fd --- /dev/null +++ b/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 diff --git a/extra/intl-extra/IntlExtension.php b/extra/intl-extra/IntlExtension.php index 1fce0c788..76c2b271e 100644 --- a/extra/intl-extra/IntlExtension.php +++ b/extra/intl-extra/IntlExtension.php @@ -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; @@ -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']), ]; } @@ -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); @@ -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)) { diff --git a/extra/intl-extra/Tests/Fixtures/country_names.test b/extra/intl-extra/Tests/Fixtures/country_names.test new file mode 100644 index 000000000..f3cb07974 --- /dev/null +++ b/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 diff --git a/extra/intl-extra/Tests/Fixtures/currency_names.test b/extra/intl-extra/Tests/Fixtures/currency_names.test new file mode 100644 index 000000000..bc2c54d02 --- /dev/null +++ b/extra/intl-extra/Tests/Fixtures/currency_names.test @@ -0,0 +1,16 @@ +--TEST-- +"currency_names" function +--TEMPLATE-- +{{ currency_names('UNKNOWN')|length }} +{{ currency_names()|length }} +{{ currency_names('fr')|length }} +{{ currency_names()['USD'] }} +{{ currency_names('fr')['USD'] }} +--DATA-- +return []; +--EXPECT-- +0 +292 +292 +US Dollar +dollar des États-Unis diff --git a/extra/intl-extra/Tests/Fixtures/language_names.test b/extra/intl-extra/Tests/Fixtures/language_names.test new file mode 100644 index 000000000..bd30607f8 --- /dev/null +++ b/extra/intl-extra/Tests/Fixtures/language_names.test @@ -0,0 +1,16 @@ +--TEST-- +"language_names" function +--TEMPLATE-- +{{ language_names('UNKNOWN')|length }} +{{ language_names()|length }} +{{ language_names('fr')|length }} +{{ language_names()['fr'] }} +{{ language_names('fr')['fr'] }} +--DATA-- +return []; +--EXPECT-- +0 +634 +634 +French +français diff --git a/extra/intl-extra/Tests/Fixtures/locale_names.test b/extra/intl-extra/Tests/Fixtures/locale_names.test new file mode 100644 index 000000000..f7e830f66 --- /dev/null +++ b/extra/intl-extra/Tests/Fixtures/locale_names.test @@ -0,0 +1,16 @@ +--TEST-- +"locale_names" function +--TEMPLATE-- +{{ locale_names('UNKNOWN')|length }} +{{ locale_names()|length }} +{{ locale_names('fr')|length }} +{{ locale_names()['fr'] }} +{{ locale_names('fr')['fr'] }} +--DATA-- +return []; +--EXPECT-- +0 +637 +637 +French +français diff --git a/extra/intl-extra/Tests/Fixtures/script_names.test b/extra/intl-extra/Tests/Fixtures/script_names.test new file mode 100644 index 000000000..c65daf550 --- /dev/null +++ b/extra/intl-extra/Tests/Fixtures/script_names.test @@ -0,0 +1,16 @@ +--TEST-- +"script_names" function +--TEMPLATE-- +{{ script_names('UNKNOWN')|length }} +{{ script_names()|length }} +{{ script_names('fr')|length }} +{{ script_names()['Marc'] }} +{{ script_names('fr')['Marc'] }} +--DATA-- +return []; +--EXPECT-- +0 +201 +201 +Marchen +Marchen diff --git a/extra/intl-extra/Tests/Fixtures/timezone_names.test b/extra/intl-extra/Tests/Fixtures/timezone_names.test new file mode 100644 index 000000000..51b50704c --- /dev/null +++ b/extra/intl-extra/Tests/Fixtures/timezone_names.test @@ -0,0 +1,16 @@ +--TEST-- +"timezone_names" function +--TEMPLATE-- +{{ timezone_names('UNKNOWN')|length }} +{{ timezone_names()|length }} +{{ timezone_names('fr')|length }} +{{ timezone_names()['Europe/Paris'] }} +{{ timezone_names('fr')['Europe/Paris'] }} +--DATA-- +return []; +--EXPECT-- +0 +434 +434 +Central European Time (Paris) +heure d’Europe centrale (Paris)