Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get the list of available locales in PHP?

Tags:

In Java, you can call Locale.getAvailableLocales() to get the list of available locales.

I was expecting an equivalent from the PHP Locale class, but could not find one.

Is there a way to get an array of all valid Locales?

like image 841
BenMorel Avatar asked Apr 09 '12 12:04

BenMorel


People also ask

What is locale PHP?

Locale information is language, monetary, time and other information specific for a geographical area. Note: The setlocale() function changes the locale only for the current script. Tip: The locale information can be set to system default with setlocale(LC_ALL,NULL)

What is fr FR locale?

fr is the locale name for standard French. fr_FR is the locale name for French in France. fr_CA is the locale name for French in Canada.

Is locale a code?

In computing, a locale is a set of parameters that defines the user's language, region and any special variant preferences that the user wants to see in their user interface. Usually a locale identifier consists of at least a language code and a country/region code.


1 Answers

Part of the confusion here is that PHP has two concepts called "locale" that are pretty much totally separate.

The first is the older one, which basically just uses the C locale features. That's what's behind setlocale and the locale support in some of PHP's functions (like money_format for example). This is what other answers that mention running locale -a on the command line and using setlocale are talking about.

PHP's Locale class and the other related functionality from the intl extension is newer, and doesn't work the same way. Instead of using the libc locale stuff, it uses a library called ICU, which ships its own locale data. PHP does provide a method to determine which locales are supported by this system: ResourceBundle::getLocales. The documentation is a little wooly here, but you can call this as a static method and pass the blank string to use ICU's default resources, thus getting a list of the supported locales for intl:

ResourceBundle::getLocales(''); 
like image 133
John Flatness Avatar answered Sep 21 '22 18:09

John Flatness