22

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • See as well: [Get default locale for language in PHP](http://stackoverflow.com/q/8568762/367456) and [List of available collators in PHP?](http://stackoverflow.com/q/9422553/367456) – hakre Jun 02 '12 at 15:42

8 Answers8

27

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('');
John Flatness
  • 32,469
  • 5
  • 79
  • 81
  • Thanks so much, I was indeed looking for a list of ICU locales at the time, and totally missed this function! – BenMorel Aug 23 '17 at 08:38
  • This does not work at least in PHP 5.6 ` Class 'ResourceBundle' not found ` – The Godfather Jun 01 '20 at 15:14
  • 2
    This does answer the question on how to get _locales_, but if one wants to instead get a list of _languages_, then this will no work. The list will be incomplete. See https://stackoverflow.com/a/63276420/37706 for a list of _languages_. – PowerKiKi Aug 06 '20 at 03:30
15

I don't think there is a built in functions for this. You need to ask the operating system which locales are installed.

For example, if you run on a unix system you will need to execute the command:

$ locale -a
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    For Windows see http://msdn.microsoft.com/en-us/library/39cwe7zf%28v=vs.100%29.aspx – Valentin Despa Nov 11 '13 at 14:27
  • I've written a php script that checks against @user2627106's list to see what locales are and aren't supported. This might be useful if you're on a shared webhost and don't have access to the shell or `shell_exec('...')` https://gist.github.com/Azeirah/266c52619ae8930ed4ef45776fc36532 – Azeirah Dec 26 '16 at 23:10
4

I also now solved this problem. Here's what happened.

<?php
return array(
    'aa_DJ' => 'Afar (Djibouti)',
    'aa_ER' => 'Afar (Eritrea)',
    'aa_ET' => 'Afar (Ethiopia)',
    'af_ZA' => 'Afrikaans (South Africa)',
    'sq_AL' => 'Albanian (Albania)',
    'sq_MK' => 'Albanian (Macedonia)',
    'am_ET' => 'Amharic (Ethiopia)',
    'ar_DZ' => 'Arabic (Algeria)',
    'ar_BH' => 'Arabic (Bahrain)',
    'ar_EG' => 'Arabic (Egypt)',
    'ar_IN' => 'Arabic (India)',
    'ar_IQ' => 'Arabic (Iraq)',
    'ar_JO' => 'Arabic (Jordan)',
    'ar_KW' => 'Arabic (Kuwait)',
    'ar_LB' => 'Arabic (Lebanon)',
    'ar_LY' => 'Arabic (Libya)',
    'ar_MA' => 'Arabic (Morocco)',
    'ar_OM' => 'Arabic (Oman)',
    'ar_QA' => 'Arabic (Qatar)',
    'ar_SA' => 'Arabic (Saudi Arabia)',
    'ar_SD' => 'Arabic (Sudan)',
    'ar_SY' => 'Arabic (Syria)',
    'ar_TN' => 'Arabic (Tunisia)',
    'ar_AE' => 'Arabic (United Arab Emirates)',
    'ar_YE' => 'Arabic (Yemen)',
    'an_ES' => 'Aragonese (Spain)',
    'hy_AM' => 'Armenian (Armenia)',
    'as_IN' => 'Assamese (India)',
    'ast_ES' => 'Asturian (Spain)',
    'az_AZ' => 'Azerbaijani (Azerbaijan)',
    'az_TR' => 'Azerbaijani (Turkey)',
    'eu_FR' => 'Basque (France)',
    'eu_ES' => 'Basque (Spain)',
    'be_BY' => 'Belarusian (Belarus)',
    'bem_ZM' => 'Bemba (Zambia)',
    'bn_BD' => 'Bengali (Bangladesh)',
    'bn_IN' => 'Bengali (India)',
    'ber_DZ' => 'Berber (Algeria)',
    'ber_MA' => 'Berber (Morocco)',
    'byn_ER' => 'Blin (Eritrea)',
    'bs_BA' => 'Bosnian (Bosnia and Herzegovina)',
    'br_FR' => 'Breton (France)',
    'bg_BG' => 'Bulgarian (Bulgaria)',
    'my_MM' => 'Burmese (Myanmar [Burma])',
    'ca_AD' => 'Catalan (Andorra)',
    'ca_FR' => 'Catalan (France)',
    'ca_IT' => 'Catalan (Italy)',
    'ca_ES' => 'Catalan (Spain)',
    'zh_CN' => 'Chinese (China)',
    'zh_HK' => 'Chinese (Hong Kong SAR China)',
    'zh_SG' => 'Chinese (Singapore)',
    'zh_TW' => 'Chinese (Taiwan)',
    'cv_RU' => 'Chuvash (Russia)',
    'kw_GB' => 'Cornish (United Kingdom)',
    'crh_UA' => 'Crimean Turkish (Ukraine)',
    'hr_HR' => 'Croatian (Croatia)',
    'cs_CZ' => 'Czech (Czech Republic)',
    'da_DK' => 'Danish (Denmark)',
    'dv_MV' => 'Divehi (Maldives)',
    'nl_AW' => 'Dutch (Aruba)',
    'nl_BE' => 'Dutch (Belgium)',
    'nl_NL' => 'Dutch (Netherlands)',
    'dz_BT' => 'Dzongkha (Bhutan)',
    'en_AG' => 'English (Antigua and Barbuda)',
    'en_AU' => 'English (Australia)',
    'en_BW' => 'English (Botswana)',
    'en_CA' => 'English (Canada)',
    'en_DK' => 'English (Denmark)',
    'en_HK' => 'English (Hong Kong SAR China)',
    'en_IN' => 'English (India)',
    'en_IE' => 'English (Ireland)',
    'en_NZ' => 'English (New Zealand)',
    'en_NG' => 'English (Nigeria)',
    'en_PH' => 'English (Philippines)',
    'en_SG' => 'English (Singapore)',
    'en_ZA' => 'English (South Africa)',
    'en_GB' => 'English (United Kingdom)',
    'en_US' => 'English (United States)',
    'en_ZM' => 'English (Zambia)',
    'en_ZW' => 'English (Zimbabwe)',
    'eo' => 'Esperanto',
    'et_EE' => 'Estonian (Estonia)',
    'fo_FO' => 'Faroese (Faroe Islands)',
    'fil_PH' => 'Filipino (Philippines)',
    'fi_FI' => 'Finnish (Finland)',
    'fr_BE' => 'French (Belgium)',
    'fr_CA' => 'French (Canada)',
    'fr_FR' => 'French (France)',
    'fr_LU' => 'French (Luxembourg)',
    'fr_CH' => 'French (Switzerland)',
    'fur_IT' => 'Friulian (Italy)',
    'ff_SN' => 'Fulah (Senegal)',
    'gl_ES' => 'Galician (Spain)',
    'lg_UG' => 'Ganda (Uganda)',
    'gez_ER' => 'Geez (Eritrea)',
    'gez_ET' => 'Geez (Ethiopia)',
    'ka_GE' => 'Georgian (Georgia)',
    'de_AT' => 'German (Austria)',
    'de_BE' => 'German (Belgium)',
    'de_DE' => 'German (Germany)',
    'de_LI' => 'German (Liechtenstein)',
    'de_LU' => 'German (Luxembourg)',
    'de_CH' => 'German (Switzerland)',
    'el_CY' => 'Greek (Cyprus)',
    'el_GR' => 'Greek (Greece)',
    'gu_IN' => 'Gujarati (India)',
    'ht_HT' => 'Haitian (Haiti)',
    'ha_NG' => 'Hausa (Nigeria)',
    'iw_IL' => 'Hebrew (Israel)',
    'he_IL' => 'Hebrew (Israel)',
    'hi_IN' => 'Hindi (India)',
    'hu_HU' => 'Hungarian (Hungary)',
    'is_IS' => 'Icelandic (Iceland)',
    'ig_NG' => 'Igbo (Nigeria)',
    'id_ID' => 'Indonesian (Indonesia)',
    'ia' => 'Interlingua',
    'iu_CA' => 'Inuktitut (Canada)',
    'ik_CA' => 'Inupiaq (Canada)',
    'ga_IE' => 'Irish (Ireland)',
    'it_IT' => 'Italian (Italy)',
    'it_CH' => 'Italian (Switzerland)',
    'ja_JP' => 'Japanese (Japan)',
    'kl_GL' => 'Kalaallisut (Greenland)',
    'kn_IN' => 'Kannada (India)',
    'ks_IN' => 'Kashmiri (India)',
    'csb_PL' => 'Kashubian (Poland)',
    'kk_KZ' => 'Kazakh (Kazakhstan)',
    'km_KH' => 'Khmer (Cambodia)',
    'rw_RW' => 'Kinyarwanda (Rwanda)',
    'ky_KG' => 'Kirghiz (Kyrgyzstan)',
    'kok_IN' => 'Konkani (India)',
    'ko_KR' => 'Korean (South Korea)',
    'ku_TR' => 'Kurdish (Turkey)',
    'lo_LA' => 'Lao (Laos)',
    'lv_LV' => 'Latvian (Latvia)',
    'li_BE' => 'Limburgish (Belgium)',
    'li_NL' => 'Limburgish (Netherlands)',
    'lt_LT' => 'Lithuanian (Lithuania)',
    'nds_DE' => 'Low German (Germany)',
    'nds_NL' => 'Low German (Netherlands)',
    'mk_MK' => 'Macedonian (Macedonia)',
    'mai_IN' => 'Maithili (India)',
    'mg_MG' => 'Malagasy (Madagascar)',
    'ms_MY' => 'Malay (Malaysia)',
    'ml_IN' => 'Malayalam (India)',
    'mt_MT' => 'Maltese (Malta)',
    'gv_GB' => 'Manx (United Kingdom)',
    'mi_NZ' => 'Maori (New Zealand)',
    'mr_IN' => 'Marathi (India)',
    'mn_MN' => 'Mongolian (Mongolia)',
    'ne_NP' => 'Nepali (Nepal)',
    'se_NO' => 'Northern Sami (Norway)',
    'nso_ZA' => 'Northern Sotho (South Africa)',
    'nb_NO' => 'Norwegian Bokmål (Norway)',
    'nn_NO' => 'Norwegian Nynorsk (Norway)',
    'oc_FR' => 'Occitan (France)',
    'or_IN' => 'Oriya (India)',
    'om_ET' => 'Oromo (Ethiopia)',
    'om_KE' => 'Oromo (Kenya)',
    'os_RU' => 'Ossetic (Russia)',
    'pap_AN' => 'Papiamento (Netherlands Antilles)',
    'ps_AF' => 'Pashto (Afghanistan)',
    'fa_IR' => 'Persian (Iran)',
    'pl_PL' => 'Polish (Poland)',
    'pt_BR' => 'Portuguese (Brazil)',
    'pt_PT' => 'Portuguese (Portugal)',
    'pa_IN' => 'Punjabi (India)',
    'pa_PK' => 'Punjabi (Pakistan)',
    'ro_RO' => 'Romanian (Romania)',
    'ru_RU' => 'Russian (Russia)',
    'ru_UA' => 'Russian (Ukraine)',
    'sa_IN' => 'Sanskrit (India)',
    'sc_IT' => 'Sardinian (Italy)',
    'gd_GB' => 'Scottish Gaelic (United Kingdom)',
    'sr_ME' => 'Serbian (Montenegro)',
    'sr_RS' => 'Serbian (Serbia)',
    'sid_ET' => 'Sidamo (Ethiopia)',
    'sd_IN' => 'Sindhi (India)',
    'si_LK' => 'Sinhala (Sri Lanka)',
    'sk_SK' => 'Slovak (Slovakia)',
    'sl_SI' => 'Slovenian (Slovenia)',
    'so_DJ' => 'Somali (Djibouti)',
    'so_ET' => 'Somali (Ethiopia)',
    'so_KE' => 'Somali (Kenya)',
    'so_SO' => 'Somali (Somalia)',
    'nr_ZA' => 'South Ndebele (South Africa)',
    'st_ZA' => 'Southern Sotho (South Africa)',
    'es_AR' => 'Spanish (Argentina)',
    'es_BO' => 'Spanish (Bolivia)',
    'es_CL' => 'Spanish (Chile)',
    'es_CO' => 'Spanish (Colombia)',
    'es_CR' => 'Spanish (Costa Rica)',
    'es_DO' => 'Spanish (Dominican Republic)',
    'es_EC' => 'Spanish (Ecuador)',
    'es_SV' => 'Spanish (El Salvador)',
    'es_GT' => 'Spanish (Guatemala)',
    'es_HN' => 'Spanish (Honduras)',
    'es_MX' => 'Spanish (Mexico)',
    'es_NI' => 'Spanish (Nicaragua)',
    'es_PA' => 'Spanish (Panama)',
    'es_PY' => 'Spanish (Paraguay)',
    'es_PE' => 'Spanish (Peru)',
    'es_ES' => 'Spanish (Spain)',
    'es_US' => 'Spanish (United States)',
    'es_UY' => 'Spanish (Uruguay)',
    'es_VE' => 'Spanish (Venezuela)',
    'sw_KE' => 'Swahili (Kenya)',
    'sw_TZ' => 'Swahili (Tanzania)',
    'ss_ZA' => 'Swati (South Africa)',
    'sv_FI' => 'Swedish (Finland)',
    'sv_SE' => 'Swedish (Sweden)',
    'tl_PH' => 'Tagalog (Philippines)',
    'tg_TJ' => 'Tajik (Tajikistan)',
    'ta_IN' => 'Tamil (India)',
    'tt_RU' => 'Tatar (Russia)',
    'te_IN' => 'Telugu (India)',
    'th_TH' => 'Thai (Thailand)',
    'bo_CN' => 'Tibetan (China)',
    'bo_IN' => 'Tibetan (India)',
    'tig_ER' => 'Tigre (Eritrea)',
    'ti_ER' => 'Tigrinya (Eritrea)',
    'ti_ET' => 'Tigrinya (Ethiopia)',
    'ts_ZA' => 'Tsonga (South Africa)',
    'tn_ZA' => 'Tswana (South Africa)',
    'tr_CY' => 'Turkish (Cyprus)',
    'tr_TR' => 'Turkish (Turkey)',
    'tk_TM' => 'Turkmen (Turkmenistan)',
    'ug_CN' => 'Uighur (China)',
    'uk_UA' => 'Ukrainian (Ukraine)',
    'hsb_DE' => 'Upper Sorbian (Germany)',
    'ur_PK' => 'Urdu (Pakistan)',
    'uz_UZ' => 'Uzbek (Uzbekistan)',
    've_ZA' => 'Venda (South Africa)',
    'vi_VN' => 'Vietnamese (Vietnam)',
    'wa_BE' => 'Walloon (Belgium)',
    'cy_GB' => 'Welsh (United Kingdom)',
    'fy_DE' => 'Western Frisian (Germany)',
    'fy_NL' => 'Western Frisian (Netherlands)',
    'wo_SN' => 'Wolof (Senegal)',
    'xh_ZA' => 'Xhosa (South Africa)',
    'yi_US' => 'Yiddish (United States)',
    'yo_NG' => 'Yoruba (Nigeria)',
    'zu_ZA' => 'Zulu (South Africa)'
);
  • 13
    This is a fixed list that may be valid for you, but that may be wrong on other systems. I was asking for the list of locales available on *a given system*, not a hardcoded list! – BenMorel Dec 28 '13 at 19:53
  • This will be useful if you are using translation files. Locally stored in the database and in accordance with it selected a translation file. This approach does not depend on the operating system. –  Dec 28 '13 at 20:12
  • 2
    Locales are much more than just strings to be used in translation files, they provide i18n support for date/time and numbers formatting, etc. Hence a hardcoded list is of next to no use if not backed up by [ICU](http://site.icu-project.org/) data. – BenMorel Dec 28 '13 at 20:35
  • "...This extension is bundled with PHP as of PHP version 5.3.0. ..." http://www.php.net/manual/en/intl.requirements.php –  Dec 28 '13 at 20:52
  • 4
    The extension is bundled with PHP, but AFAIK the Locale list (data) is system-dependent. – BenMorel Dec 28 '13 at 20:53
  • Nowhere in the ICU documentation I do not see that it uses system resources. Nevertheless, this should reassure you finally: "...Essential part ICU's resource management framework is the fallback mechanism. It ensures that if the data for the requested locale is missing, an effort will be made to obtain the most usable data..." (http://userguide.icu-project.org/locale/resources) –  Dec 28 '13 at 21:22
  • P.S.: Can use it without any problems :) http://site.icu-project.org "... This data also comes from the Common Locale Data Repository... " (http://cldr.unicode.org) –  Dec 28 '13 at 22:17
2

On Windows you can try to call the setlocale() php function with all the items of the following list:
http://msdn.microsoft.com/en-us/goglobal/bb895996.aspx

Here is a code sniplet to list all available locales on a Windows based host:

<?php
header( 'Content-Type: text/html; charset=utf-8' );
// source of the list:
// http://msdn.microsoft.com/en-us/library/39cwe7zf(v=vs.90).aspx
$langs = array(
    // language, sublanguage, codes
    array( 'Chinese', 'Chinese', array( 'chinese' ) ),
    array( 'Chinese', 'Chinese (simplified)', array( 'chinese-simplified', 'chs' ) ),
    array( 'Chinese', 'Chinese (traditional)', array( 'chinese-traditional', 'cht' ) ),
    array( 'Czech', 'Czech', array( 'csy', 'czech' ) ),
    array( 'Danish', 'Danish', array( 'dan', 'danish' ) ),
    array( 'Dutch', 'Dutch (default)', array( 'dutch', 'nld' ) ),
    array( 'Dutch', 'Dutch (Belgium)', array( 'belgian', 'dutch-belgian', 'nlb' ) ),
    array( 'English', 'English (default)', array( 'english' ) ),
    array( 'English', 'English (Australia)', array( 'australian', 'ena', 'english-aus' ) ),
    array( 'English', 'English (Canada)', array( 'canadian', 'enc', 'english-can' ) ),
    array( 'English', 'English (New Zealand)', array( 'english-nz', 'enz' ) ),
    array( 'English', 'English (United Kingdom)', array( 'eng', 'english-uk', 'uk' ) ),
    array( 'English', 'English (United States)', array( 'american', 'american english', 'american-english', 'english-american', 'english-us', 'english-usa', 'enu', 'us', 'usa' ) ),
    array( 'Finnish', 'Finnish', array( 'fin', 'finnish' ) ),
    array( 'French', 'French (default)', array( 'fra', 'french' ) ),
    array( 'French', 'French (Belgium)', array( 'frb', 'french-belgian' ) ),
    array( 'French', 'French (Canada)', array( 'frc', 'french-canadian' ) ),
    array( 'French', 'French (Switzerland)', array( 'french-swiss', 'frs' ) ),
    array( 'German', 'German (default)', array( 'deu', 'german' ) ),
    array( 'German', 'German (Austria)', array( 'dea', 'german-austrian' ) ),
    array( 'German', 'German (Switzerland)', array( 'des', 'german-swiss', 'swiss' ) ),
    array( 'Greek', 'Greek', array( 'ell', 'greek' ) ),
    array( 'Hungarian', 'Hungarian', array( 'hun', 'hungarian' ) ),
    array( 'Icelandic', 'Icelandic', array( 'icelandic', 'isl' ) ),
    array( 'Italian', 'Italian (default)', array( 'ita', 'italian' ) ),
    array( 'Italian', 'Italian (Switzerland)', array( 'italian-swiss', 'its' ) ),
    array( 'Japanese', 'Japanese', array( 'japanese', 'jpn' ) ),
    array( 'Korean', 'Korean', array( 'kor', 'korean' ) ),
    array( 'Norwegian', 'Norwegian (default)', array( 'norwegian' ) ),
    array( 'Norwegian', 'Norwegian (Bokmal)', array( 'nor', 'norwegian-bokmal' ) ),
    array( 'Norwegian', 'Norwegian (Nynorsk)', array( 'non', 'norwegian-nynorsk' ) ),
    array( 'Polish', 'Polish', array( 'plk', 'polish' ) ),
    array( 'Portuguese', 'Portuguese (default)', array( 'portuguese', 'ptg' ) ),
    array( 'Portuguese', 'Portuguese (Brazil)', array( 'portuguese-brazilian', 'ptb' ) ),
    array( 'Russian', 'Russian (default)', array( 'rus', 'russian' ) ),
    array( 'Slovak', 'Slovak', array( 'sky', 'slovak' ) ),
    array( 'Spanish', 'Spanish (default)', array( 'esp', 'spanish' ) ),
    array( 'Spanish', 'Spanish (Mexico)', array( 'esm', 'spanish-mexican' ) ),
    array( 'Spanish', 'Spanish (Modern)', array( 'esn', 'spanish-modern' ) ),
    array( 'Swedish', 'Swedish', array( 'sve', 'swedish' ) ),
    array( 'Turkish', 'Turkish', array( 'trk', 'turkish' ) )
);
echo '<table>'."\n";
echo '<tr>'."\n";
echo '  <th>Languange</th>'."\n";
echo '  <th>Sub-Languange</th>'."\n";
echo '  <th>Languange String</th>'."\n";
echo '</tr>'."\n";
foreach ( $langs as $lang ) {
    echo '<tr>'."\n";
    echo '  <td>'.$lang[0].'</td>'."\n";
    echo '  <td>'.$lang[1].'</td>'."\n";
    $a = array();
    foreach ( $lang[2] as $lang_code ) {
        $loc = setlocale( LC_ALL, $lang_code );
        $a []= $lang_code.' '.( false === $loc ? '✖' : '✔ - '.$loc );
    }
    echo '  <td>'.implode( '<br>', $a ).'</td>'."\n";
    echo '</tr>'."\n";
}
echo '</table>'."\n";
// Note: Norvegian (Bokmal) is Norvegian (Bokmål), see: http://en.wikipedia.org/wiki/Bokmål
?>
Balazs Nemeth
  • 497
  • 4
  • 3
2

you normally need only locales that compatible with UTF-8 to use it in html

thus, the ResourceBundle is useless, but you still can call it like this

\ResourceBundle::getLocales('')

to be sure how useless it is.

So, you need to get a list of what supported by your Ubuntu (because Ubuntu is what you need)

    $string           = shell_exec("locale -a|grep .utf8");
    $array            = explode('.utf8' . "\n", $string);

you will get things like en_US, ru_RU, zh_CN - whatever you installed on your ubuntu. Now take one and use it like so

$locale  = $array[0].'.UTF-8';
setlocale(LC_MONETARY, $locale);

but you can install more locales on your Ubuntu

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
2

This might be a slight deviation from OP, but I feel it is a common enough use-case to be mentioned here. What if you want a list of language code, as defined by ICU, instead of a list of locales ?

List of all languages

The solution based on ResourceBundle::getLocales(''); will get you locales. You then need to map them to language codes. But even after that the list of language is not complete.

Here is a comparison with how to do it properly:

<?php

function getListFromLocales(): array
{
    return array_filter(ResourceBundle::getLocales(''), function ($value) {
        return strpos($value, '_') === false;
    });
}

function getListFromLanguages(): array
{
    $resourceBundle = new ResourceBundle('en', 'ICUDATA-lang', false);
    $languagesBundle = $resourceBundle['Languages'];

    $languages = [];
    foreach ($languagesBundle as $languageCode => $languageName) {
        $languages[] = $languageCode;
    }

    return $languages;
}

function languageForEndUser($language): string
{
    return Locale::getDisplayLanguage($language, $language) . ' (' . Locale::getDisplayLanguage($language) . ')';
}

$list1 = getListFromLocales();
$list2 = getListFromLanguages();

echo 'languages in list1: ' . count($list1) . PHP_EOL;
echo 'languages in list2: ' . count($list2) . PHP_EOL;
echo 'languages that exists in list1, but not in list2: ' . count(array_diff($list1, $list2)) . PHP_EOL;
echo 'languages that exists in list2, but not in list1: ' . count(array_diff($list2, $list1)) . PHP_EOL;

Locale::setDefault('fr'); // Assume end-user speaks french

echo 'examples of language for end-user: ' . PHP_EOL;
echo '    - ' . languageForEndUser('en') . PHP_EOL;
echo '    - ' . languageForEndUser('fr') . PHP_EOL;
echo '    - ' . languageForEndUser('ko') . PHP_EOL;

This will output something similar to:

languages in list1: 204
languages in list2: 620
languages that exists in list1, but not in list2: 0
languages that exists in list2, but not in list1: 416
examples of language for end-user: 
    - English (anglais)
    - français (français)
    - 한국어 (coréen)

where we can see that the proper way yields ~400 (!) more languages.

You can see the code in action on https://3v4l.org/TS292

PowerKiKi
  • 4,539
  • 4
  • 39
  • 47
1

There is a method called getAvailableLocales in IntlCalendar PHP class. It returns all available and supported locales in PHP. The intl extension is required to run this method.

print_r(IntlCalendar::getAvailableLocales());

I executed the code in PHP 8.1.x and returned 797 records.

Array
(
    [0]    af
    [1]    af_NA
    .....
    [454]    ko
    [455]    ko_KP
    .....
    [795]    zu
    [796]    zu_ZA
)
Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
0

system("locale -a|grep XX"); where XX is language code ie. system("locale -a|grep de");

Qrzysio
  • 1,147
  • 3
  • 12
  • 25