Skip to content

Commit

Permalink
Add function twig *_name for intl list
Browse files Browse the repository at this point in the history
  • Loading branch information
seb-jean authored and fabpot committed Dec 27, 2022
1 parent 9e13996 commit bf0cb12
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 1 deletion.
63 changes: 62 additions & 1 deletion IntlExtension.php
Original file line number Diff line number Diff line change
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
12 changes: 12 additions & 0 deletions Tests/Fixtures/country_names.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
"country_names" function
--TEMPLATE--
{{ country_names('UNKNOWN')|length }}
{{ country_names()|join(', ') }}
{{ country_names('fr')|join(', ') }}
--DATA--
return [];
--EXPECT--
0
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
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
Loading

0 comments on commit bf0cb12

Please sign in to comment.