Skip to content

Commit b376bdd

Browse files
seb-jeanfabpot
authored andcommitted
Add function twig *_name for intl list
1 parent f6ef36a commit b376bdd

File tree

13 files changed

+416
-1
lines changed

13 files changed

+416
-1
lines changed

doc/functions/country_names.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
``country_names``
2+
=================
3+
4+
.. versionadded:: 3.5
5+
6+
The ``country_names`` function was added in Twig 3.5.
7+
8+
The ``country_names`` function returns the names of the countries:
9+
10+
.. code-block:: twig
11+
12+
{# Afghanistan, Åland Islands, ... #}
13+
{{ country_names()|join(', ') }}
14+
15+
By default, the function uses the current locale. You can pass it explicitly:
16+
17+
.. code-block:: twig
18+
19+
{# Afghanistan, Afrique du Sud, ... #}
20+
{{ country_names('fr')|join(', ') }}
21+
22+
.. note::
23+
24+
The ``country_names`` function is part of the ``IntlExtension`` which is not
25+
installed by default. Install it first:
26+
27+
.. code-block:: bash
28+
29+
$ composer require twig/intl-extra
30+
31+
Then, on Symfony projects, install the ``twig/extra-bundle``:
32+
33+
.. code-block:: bash
34+
35+
$ composer require twig/extra-bundle
36+
37+
Otherwise, add the extension explicitly on the Twig environment::
38+
39+
use Twig\Extra\Intl\IntlExtension;
40+
41+
$twig = new \Twig\Environment(...);
42+
$twig->addExtension(new IntlExtension());
43+
44+
Arguments
45+
---------
46+
47+
* ``locale``: The locale

doc/functions/currency_names.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
``currency_names``
2+
==================
3+
4+
.. versionadded:: 3.5
5+
6+
The ``currency_names`` function was added in Twig 3.5.
7+
8+
The ``currency_names`` function returns the names of the currencies:
9+
10+
.. code-block:: twig
11+
12+
{# Afghan Afghani, Afghan Afghani (1927–2002), ... #}
13+
{{ currency_names()|join(', ') }}
14+
15+
By default, the function uses the current locale. You can pass it explicitly:
16+
17+
.. code-block:: twig
18+
19+
{# afghani (1927–2002), afghani afghan, ... #}
20+
{{ currency_names('fr')|join(', ') }}
21+
22+
.. note::
23+
24+
The ``currency_names`` function is part of the ``IntlExtension`` which is not
25+
installed by default. Install it first:
26+
27+
.. code-block:: bash
28+
29+
$ composer require twig/intl-extra
30+
31+
Then, on Symfony projects, install the ``twig/extra-bundle``:
32+
33+
.. code-block:: bash
34+
35+
$ composer require twig/extra-bundle
36+
37+
Otherwise, add the extension explicitly on the Twig environment::
38+
39+
use Twig\Extra\Intl\IntlExtension;
40+
41+
$twig = new \Twig\Environment(...);
42+
$twig->addExtension(new IntlExtension());
43+
44+
Arguments
45+
---------
46+
47+
* ``locale``: The locale

doc/functions/language_names.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
``language_names``
2+
==================
3+
4+
.. versionadded:: 3.5
5+
6+
The ``language_names`` function was added in Twig 3.5.
7+
8+
The ``language_names`` function returns the names of the languages:
9+
10+
.. code-block:: twig
11+
12+
{# Abkhazian, Achinese, ... #}
13+
{{ language_names()|join(', ') }}
14+
15+
By default, the function uses the current locale. You can pass it explicitly:
16+
17+
.. code-block:: twig
18+
19+
{# abkhaze, aceh, ... #}
20+
{{ language_names('fr')|join(', ') }}
21+
22+
.. note::
23+
24+
The ``language_names`` function is part of the ``IntlExtension`` which is not
25+
installed by default. Install it first:
26+
27+
.. code-block:: bash
28+
29+
$ composer require twig/intl-extra
30+
31+
Then, on Symfony projects, install the ``twig/extra-bundle``:
32+
33+
.. code-block:: bash
34+
35+
$ composer require twig/extra-bundle
36+
37+
Otherwise, add the extension explicitly on the Twig environment::
38+
39+
use Twig\Extra\Intl\IntlExtension;
40+
41+
$twig = new \Twig\Environment(...);
42+
$twig->addExtension(new IntlExtension());
43+
44+
Arguments
45+
---------
46+
47+
* ``locale``: The locale

doc/functions/locale_names.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
``locale_names``
2+
================
3+
4+
.. versionadded:: 3.5
5+
6+
The ``locale_names`` function was added in Twig 3.5.
7+
8+
The ``locale_names`` function returns the names of the locales:
9+
10+
.. code-block:: twig
11+
12+
{# Afrikaans, Afrikaans (Namibia), ... #}
13+
{{ locale_names()|join(', ') }}
14+
15+
By default, the function uses the current locale. You can pass it explicitly:
16+
17+
.. code-block:: twig
18+
19+
{# afrikaans, afrikaans (Afrique du Sud), ... #}
20+
{{ locale_names('fr')|join(', ') }}
21+
22+
.. note::
23+
24+
The ``locale_names`` function is part of the ``IntlExtension`` which is not
25+
installed by default. Install it first:
26+
27+
.. code-block:: bash
28+
29+
$ composer require twig/intl-extra
30+
31+
Then, on Symfony projects, install the ``twig/extra-bundle``:
32+
33+
.. code-block:: bash
34+
35+
$ composer require twig/extra-bundle
36+
37+
Otherwise, add the extension explicitly on the Twig environment::
38+
39+
use Twig\Extra\Intl\IntlExtension;
40+
41+
$twig = new \Twig\Environment(...);
42+
$twig->addExtension(new IntlExtension());
43+
44+
Arguments
45+
---------
46+
47+
* ``locale``: The locale

doc/functions/script_names.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
``script_names``
2+
================
3+
4+
.. versionadded:: 3.5
5+
6+
The ``script_names`` function was added in Twig 3.5.
7+
8+
The ``script_names`` function returns the names of the scripts:
9+
10+
.. code-block:: twig
11+
12+
{# Adlam, Afaka, ... #}
13+
{{ script_names()|join(', ') }}
14+
15+
By default, the function uses the current locale. You can pass it explicitly:
16+
17+
.. code-block:: twig
18+
19+
{# Adlam, Afaka, ... #}
20+
{{ script_names('fr')|join(', ') }}
21+
22+
.. note::
23+
24+
The ``script_names`` function is part of the ``IntlExtension`` which is not
25+
installed by default. Install it first:
26+
27+
.. code-block:: bash
28+
29+
$ composer require twig/intl-extra
30+
31+
Then, on Symfony projects, install the ``twig/extra-bundle``:
32+
33+
.. code-block:: bash
34+
35+
$ composer require twig/extra-bundle
36+
37+
Otherwise, add the extension explicitly on the Twig environment::
38+
39+
use Twig\Extra\Intl\IntlExtension;
40+
41+
$twig = new \Twig\Environment(...);
42+
$twig->addExtension(new IntlExtension());
43+
44+
Arguments
45+
---------
46+
47+
* ``locale``: The locale

doc/functions/timezone_names.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
``timezone_names``
2+
==================
3+
4+
.. versionadded:: 3.5
5+
6+
The ``timezone_names`` function was added in Twig 3.5.
7+
8+
The ``timezone_names`` function returns the names of the timezones:
9+
10+
.. code-block:: twig
11+
12+
{# Acre Time (Eirunepe), Acre Time (Rio Branco), ... #}
13+
{{ timezone_names()|join(', ') }}
14+
15+
By default, the function uses the current locale. You can pass it explicitly:
16+
17+
.. code-block:: twig
18+
19+
{# heure : Antarctique (Casey), heure : Canada (Montreal), ... #}
20+
{{ timezone_names('fr')|join(', ') }}
21+
22+
.. note::
23+
24+
The ``timezone_names`` function is part of the ``IntlExtension`` which is not
25+
installed by default. Install it first:
26+
27+
.. code-block:: bash
28+
29+
$ composer require twig/intl-extra
30+
31+
Then, on Symfony projects, install the ``twig/extra-bundle``:
32+
33+
.. code-block:: bash
34+
35+
$ composer require twig/extra-bundle
36+
37+
Otherwise, add the extension explicitly on the Twig environment::
38+
39+
use Twig\Extra\Intl\IntlExtension;
40+
41+
$twig = new \Twig\Environment(...);
42+
$twig->addExtension(new IntlExtension());
43+
44+
Arguments
45+
---------
46+
47+
* ``locale``: The locale

extra/intl-extra/IntlExtension.php

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Intl\Exception\MissingResourceException;
1717
use Symfony\Component\Intl\Languages;
1818
use Symfony\Component\Intl\Locales;
19+
use Symfony\Component\Intl\Scripts;
1920
use Symfony\Component\Intl\Timezones;
2021
use Twig\Environment;
2122
use Twig\Error\RuntimeError;
@@ -152,6 +153,12 @@ public function getFunctions()
152153
return [
153154
// internationalized names
154155
new TwigFunction('country_timezones', [$this, 'getCountryTimezones']),
156+
new TwigFunction('language_names', [$this, 'getLanguageNames']),
157+
new TwigFunction('script_names', [$this, 'getScriptNames']),
158+
new TwigFunction('country_names', [$this, 'getCountryNames']),
159+
new TwigFunction('locale_names', [$this, 'getLocaleNames']),
160+
new TwigFunction('currency_names', [$this, 'getCurrencyNames']),
161+
new TwigFunction('timezone_names', [$this, 'getTimezoneNames']),
155162
];
156163
}
157164

@@ -242,6 +249,60 @@ public function getCountryTimezones(string $country): array
242249
}
243250
}
244251

252+
public function getLanguageNames(string $locale = null): array
253+
{
254+
try {
255+
return Languages::getNames($locale);
256+
} catch (MissingResourceException $exception) {
257+
return [];
258+
}
259+
}
260+
261+
public function getScriptNames(string $locale = null): array
262+
{
263+
try {
264+
return Scripts::getNames($locale);
265+
} catch (MissingResourceException $exception) {
266+
return [];
267+
}
268+
}
269+
270+
public function getCountryNames(string $locale = null): array
271+
{
272+
try {
273+
return Countries::getNames($locale);
274+
} catch (MissingResourceException $exception) {
275+
return [];
276+
}
277+
}
278+
279+
public function getLocaleNames(string $locale = null): array
280+
{
281+
try {
282+
return Locales::getNames($locale);
283+
} catch (MissingResourceException $exception) {
284+
return [];
285+
}
286+
}
287+
288+
public function getCurrencyNames(string $locale = null): array
289+
{
290+
try {
291+
return Currencies::getNames($locale);
292+
} catch (MissingResourceException $exception) {
293+
return [];
294+
}
295+
}
296+
297+
public function getTimezoneNames(string $locale = null): array
298+
{
299+
try {
300+
return Timezones::getNames($locale);
301+
} catch (MissingResourceException $exception) {
302+
return [];
303+
}
304+
}
305+
245306
public function formatCurrency($amount, string $currency, array $attrs = [], string $locale = null): string
246307
{
247308
$formatter = $this->createNumberFormatter($locale, 'currency', $attrs);
@@ -279,7 +340,7 @@ public function formatNumberStyle(string $style, $number, array $attrs = [], str
279340
*/
280341
public function formatDateTime(Environment $env, $date, ?string $dateFormat = 'medium', ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', string $locale = null): string
281342
{
282-
$date = \twig_date_converter($env, $date, $timezone);
343+
$date = twig_date_converter($env, $date, $timezone);
283344
$formatter = $this->createDateFormatter($locale, $dateFormat, $timeFormat, $pattern, $date->getTimezone(), $calendar);
284345

285346
if (false === $ret = $formatter->format($date)) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
"country_names" function
3+
--TEMPLATE--
4+
{{ country_names('UNKNOWN')|length }}
5+
{{ country_names()|join(', ') }}
6+
{{ country_names('fr')|join(', ') }}
7+
--DATA--
8+
return [];
9+
--EXPECT--
10+
0
11+
Afghanistan, Åland Islands, Albania, Algeria, American Samoa, Andorra, Angola, Anguilla, Antarctica, Antigua & Barbuda, Argentina, Armenia, Aruba, Australia, Austria, Azerbaijan, Bahamas, Bahrain, Bangladesh, Barbados, Belarus, Belgium, Belize, Benin, Bermuda, Bhutan, Bolivia, Bosnia & Herzegovina, Botswana, Bouvet Island, Brazil, British Indian Ocean Territory, British Virgin Islands, Brunei, Bulgaria, Burkina Faso, Burundi, Cambodia, Cameroon, Canada, Cape Verde, Caribbean Netherlands, Cayman Islands, Central African Republic, Chad, Chile, China, Christmas Island, Cocos (Keeling) Islands, Colombia, Comoros, Congo - Brazzaville, Congo - Kinshasa, Cook Islands, Costa Rica, Côte d’Ivoire, Croatia, Cuba, Curaçao, Cyprus, Czechia, Denmark, Djibouti, Dominica, Dominican Republic, Ecuador, Egypt, El Salvador, Equatorial Guinea, Eritrea, Estonia, Eswatini, Ethiopia, Falkland Islands, Faroe Islands, Fiji, Finland, France, French Guiana, French Polynesia, French Southern Territories, Gabon, Gambia, Georgia, Germany, Ghana, Gibraltar, Greece, Greenland, Grenada, Guadeloupe, Guam, Guatemala, Guernsey, Guinea, Guinea-Bissau, Guyana, Haiti, Heard & McDonald Islands, Honduras, Hong Kong SAR China, Hungary, Iceland, India, Indonesia, Iran, Iraq, Ireland, Isle of Man, Israel, Italy, Jamaica, Japan, Jersey, Jordan, Kazakhstan, Kenya, Kiribati, Kuwait, Kyrgyzstan, Laos, Latvia, Lebanon, Lesotho, Liberia, Libya, Liechtenstein, Lithuania, Luxembourg, Macao SAR China, Madagascar, Malawi, Malaysia, Maldives, Mali, Malta, Marshall Islands, Martinique, Mauritania, Mauritius, Mayotte, Mexico, Micronesia, Moldova, Monaco, Mongolia, Montenegro, Montserrat, Morocco, Mozambique, Myanmar (Burma), Namibia, Nauru, Nepal, Netherlands, New Caledonia, New Zealand, Nicaragua, Niger, Nigeria, Niue, Norfolk Island, North Korea, North Macedonia, Northern Mariana Islands, Norway, Oman, Pakistan, Palau, Palestinian Territories, Panama, Papua New Guinea, Paraguay, Peru, Philippines, Pitcairn Islands, Poland, Portugal, Puerto Rico, Qatar, Réunion, Romania, Russia, Rwanda, Samoa, San Marino, São Tomé & Príncipe, Saudi Arabia, Senegal, Serbia, Seychelles, Sierra Leone, Singapore, Sint Maarten, Slovakia, Slovenia, Solomon Islands, Somalia, South Africa, South Georgia & South Sandwich Islands, South Korea, South Sudan, Spain, Sri Lanka, St. Barthélemy, St. Helena, St. Kitts & Nevis, St. Lucia, St. Martin, St. Pierre & Miquelon, St. Vincent & Grenadines, Sudan, Suriname, Svalbard & Jan Mayen, Sweden, Switzerland, Syria, Taiwan, Tajikistan, Tanzania, Thailand, Timor-Leste, Togo, Tokelau, Tonga, Trinidad & Tobago, Tunisia, Turkey, Turkmenistan, Turks & Caicos Islands, Tuvalu, U.S. Outlying Islands, U.S. Virgin Islands, Uganda, Ukraine, United Arab Emirates, United Kingdom, United States, Uruguay, Uzbekistan, Vanuatu, Vatican City, Venezuela, Vietnam, Wallis & Futuna, Western Sahara, Yemen, Zambia, Zimbabwe
12+
Afghanistan, Afrique du Sud, Albanie, Algérie, Allemagne, Andorre, Angola, Anguilla, Antarctique, Antigua-et-Barbuda, Arabie saoudite, Argentine, Arménie, Aruba, Australie, Autriche, Azerbaïdjan, Bahamas, Bahreïn, Bangladesh, Barbade, Belgique, Belize, Bénin, Bermudes, Bhoutan, Biélorussie, Bolivie, Bosnie-Herzégovine, Botswana, Brésil, Brunei, Bulgarie, Burkina Faso, Burundi, Cambodge, Cameroun, Canada, Cap-Vert, Chili, Chine, Chypre, Colombie, Comores, Congo-Brazzaville, Congo-Kinshasa, Corée du Nord, Corée du Sud, Costa Rica, Côte d’Ivoire, Croatie, Cuba, Curaçao, Danemark, Djibouti, Dominique, Égypte, Émirats arabes unis, Équateur, Érythrée, Espagne, Estonie, Eswatini, État de la Cité du Vatican, États-Unis, Éthiopie, Fidji, Finlande, France, Gabon, Gambie, Géorgie, Géorgie du Sud-et-les Îles Sandwich du Sud, Ghana, Gibraltar, Grèce, Grenade, Groenland, Guadeloupe, Guam, Guatemala, Guernesey, Guinée, Guinée équatoriale, Guinée-Bissau, Guyana, Guyane française, Haïti, Honduras, Hongrie, Île Bouvet, Île Christmas, Île de Man, Île Norfolk, Îles Åland, Îles Caïmans, Îles Cocos, Îles Cook, Îles Féroé, Îles Heard-et-MacDonald, Îles Malouines, Îles Mariannes du Nord, Îles Marshall, Îles mineures éloignées des États-Unis, Îles Pitcairn, Îles Salomon, Îles Turques-et-Caïques, Îles Vierges britanniques, Îles Vierges des États-Unis, Inde, Indonésie, Irak, Iran, Irlande, Islande, Israël, Italie, Jamaïque, Japon, Jersey, Jordanie, Kazakhstan, Kenya, Kirghizstan, Kiribati, Koweït, La Réunion, Laos, Lesotho, Lettonie, Liban, Liberia, Libye, Liechtenstein, Lituanie, Luxembourg, Macédoine du Nord, Madagascar, Malaisie, Malawi, Maldives, Mali, Malte, Maroc, Martinique, Maurice, Mauritanie, Mayotte, Mexique, Micronésie, Moldavie, Monaco, Mongolie, Monténégro, Montserrat, Mozambique, Myanmar (Birmanie), Namibie, Nauru, Népal, Nicaragua, Niger, Nigeria, Niue, Norvège, Nouvelle-Calédonie, Nouvelle-Zélande, Oman, Ouganda, Ouzbékistan, Pakistan, Palaos, Panama, Papouasie-Nouvelle-Guinée, Paraguay, Pays-Bas, Pays-Bas caribéens, Pérou, Philippines, Pologne, Polynésie française, Porto Rico, Portugal, Qatar, R.A.S. chinoise de Hong Kong, R.A.S. chinoise de Macao, République centrafricaine, République dominicaine, Roumanie, Royaume-Uni, Russie, Rwanda, Sahara occidental, Saint-Barthélemy, Saint-Christophe-et-Niévès, Saint-Marin, Saint-Martin, Saint-Martin (partie néerlandaise), Saint-Pierre-et-Miquelon, Saint-Vincent-et-les Grenadines, Sainte-Hélène, Sainte-Lucie, Salvador, Samoa, Samoa américaines, Sao Tomé-et-Principe, Sénégal, Serbie, Seychelles, Sierra Leone, Singapour, Slovaquie, Slovénie, Somalie, Soudan, Soudan du Sud, Sri Lanka, Suède, Suisse, Suriname, Svalbard et Jan Mayen, Syrie, Tadjikistan, Taïwan, Tanzanie, Tchad, Tchéquie, Terres australes françaises, Territoire britannique de l’océan Indien, Territoires palestiniens, Thaïlande, Timor oriental, Togo, Tokelau, Tonga, Trinité-et-Tobago, Tunisie, Turkménistan, Turquie, Tuvalu, Ukraine, Uruguay, Vanuatu, Venezuela, Viêt Nam, Wallis-et-Futuna, Yémen, Zambie, Zimbabwe

0 commit comments

Comments
 (0)