How to Get the List of Available Locales in PHP

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

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('');

List of All Locales and Their Short Codes?

The importance of locales is that your environment/os can provide formatting functionality for all installed locales even if you don't know about them when you write your application. My Windows 7 system has 211 locales installed (listed below), so you wouldn't likely write any custom code or translation specific to this many locales.

Edit: The original list of locales has been edited to add additional locales that were not included before. Now 228 listed.

The most important thing for various versions of English is in formatting numbers and dates. Other differences are significant to the extent that you want and able to cater to specific variations.

af-ZA
am-ET
ar-AE
ar-BH
ar-DZ
ar-EG
ar-IQ
ar-JO
ar-KW
ar-LB
ar-LY
ar-MA
arn-CL
ar-OM
ar-QA
ar-SA
ar-SD
ar-SY
ar-TN
ar-YE
as-IN
az-az
az-Cyrl-AZ
az-Latn-AZ
ba-RU
be-BY
bg-BG
bn-BD
bn-IN
bo-CN
br-FR
bs-Cyrl-BA
bs-Latn-BA
ca-ES
co-FR
cs-CZ
cy-GB
da-DK
de-AT
de-CH
de-DE
de-LI
de-LU
dsb-DE
dv-MV
el-CY
el-GR
en-029
en-AU
en-BZ
en-CA
en-cb
en-GB
en-IE
en-IN
en-JM
en-MT
en-MY
en-NZ
en-PH
en-SG
en-TT
en-US
en-ZA
en-ZW
es-AR
es-BO
es-CL
es-CO
es-CR
es-DO
es-EC
es-ES
es-GT
es-HN
es-MX
es-NI
es-PA
es-PE
es-PR
es-PY
es-SV
es-US
es-UY
es-VE
et-EE
eu-ES
fa-IR
fi-FI
fil-PH
fo-FO
fr-BE
fr-CA
fr-CH
fr-FR
fr-LU
fr-MC
fy-NL
ga-IE
gd-GB
gd-ie
gl-ES
gsw-FR
gu-IN
ha-Latn-NG
he-IL
hi-IN
hr-BA
hr-HR
hsb-DE
hu-HU
hy-AM
id-ID
ig-NG
ii-CN
in-ID
is-IS
it-CH
it-IT
iu-Cans-CA
iu-Latn-CA
iw-IL
ja-JP
ka-GE
kk-KZ
kl-GL
km-KH
kn-IN
kok-IN
ko-KR
ky-KG
lb-LU
lo-LA
lt-LT
lv-LV
mi-NZ
mk-MK
ml-IN
mn-MN
mn-Mong-CN
moh-CA
mr-IN
ms-BN
ms-MY
mt-MT
nb-NO
ne-NP
nl-BE
nl-NL
nn-NO
no-no
nso-ZA
oc-FR
or-IN
pa-IN
pl-PL
prs-AF
ps-AF
pt-BR
pt-PT
qut-GT
quz-BO
quz-EC
quz-PE
rm-CH
ro-mo
ro-RO
ru-mo
ru-RU
rw-RW
sah-RU
sa-IN
se-FI
se-NO
se-SE
si-LK
sk-SK
sl-SI
sma-NO
sma-SE
smj-NO
smj-SE
smn-FI
sms-FI
sq-AL
sr-BA
sr-CS
sr-Cyrl-BA
sr-Cyrl-CS
sr-Cyrl-ME
sr-Cyrl-RS
sr-Latn-BA
sr-Latn-CS
sr-Latn-ME
sr-Latn-RS
sr-ME
sr-RS
sr-sp
sv-FI
sv-SE
sw-KE
syr-SY
ta-IN
te-IN
tg-Cyrl-TJ
th-TH
tk-TM
tlh-QS
tn-ZA
tr-TR
tt-RU
tzm-Latn-DZ
ug-CN
uk-UA
ur-PK
uz-Cyrl-UZ
uz-Latn-UZ
uz-uz
vi-VN
wo-SN
xh-ZA
yo-NG
zh-CN
zh-HK
zh-MO
zh-SG
zh-TW
zu-ZA

PHP- Get current locale

You can call setlocale like so, and it'll return the current local.

$currentLocale = setlocale(LC_ALL, 0);
echo $currentLocale; //outputs C/en_US.UTF-8/C/C/C/C on my machine

Here is documentation from php.net as commented by @JROB

locale

If locale is "0", the locale setting is not affected, only the current setting is returned.

If locale is NULL or the empty string "", the locale names will be set from the values of environment variables with the same names as the above categories, or from "LANG".

If locale is an array or followed by additional parameters then each array element or parameter is tried to be set as new locale until success. This is useful if a locale is known under different names on different systems or for providing a fallback for a possibly not available locale.

List of available collators in PHP?

I think the Collator is using the locale information from the CLDR repository.

I compiled a list from it's current trunk (at the time of writing: 2020-02-23):

af          af_NA       af_ZA       ar          ar_001      ar_AE       
ar_BH ar_DZ ar_EG ar_IQ ar_JO ar_KW
ar_LB ar_LY ar_MA ar_OM ar_QA ar_SA
ar_SD ar_SY ar_TN ar_YE as as_IN
az az_Latn az_Latn_AZ be be_BY bg
bg_BG bn bn_BD bn_IN bs bs_BA
ca ca_ES chr chr_US cs cs_CZ
cy cy_GB da da_DK de de_AT
de_BE de_CH de_DE de_LI de_LU dz
ee ee_GH ee_TG el el_CY el_GR
en en_AS en_AU en_BB en_BE en_BM
en_BW en_BZ en_CA en_GB en_GU en_HK
en_IE en_IN en_JM en_MH en_MP en_MT
en_MU en_NA en_NZ en_PH en_PK en_SG
en_TT en_UM en_US en_US_POSIX en_VI en_ZA
en_ZW eo es es_419 es_AR es_BO
es_CL es_CO es_CR es_DO es_EC es_ES
es_GQ es_GT es_HN es_MX es_NI es_PA
es_PE es_PR es_PY es_SV es_US es_UY
es_VE et et_EE fa fa_AF fa_IR
fi fi_FI fil fil_PH fo fo_FO
fr fr_BE fr_BF fr_BI fr_BJ fr_BL
fr_CA fr_CD fr_CF fr_CG fr_CH fr_CI
fr_CM fr_DJ fr_FR fr_GA fr_GN fr_GP
fr_GQ fr_KM fr_LU fr_MC fr_MF fr_MG
fr_ML fr_MQ fr_NE fr_RE fr_RW fr_SN
fr_TD fr_TG ga ga_IE gu gu_IN
ha ha_Latn ha_Latn_GH ha_Latn_NE ha_Latn_NG haw
haw_US he he_IL hi hi_IN hr
hr_HR hu hu_HU hy hy_AM id
id_ID ig ig_NG is is_IS it
it_CH it_IT ja ja_JP ka ka_GE
kk kk_KZ kl kl_GL km km_KH
kn kn_IN ko ko_KR kok kok_IN
ky ky_KG ln ln_CD ln_CG lt
lt_LT lv lv_LV mk mk_MK ml
ml_IN mr mr_IN ms ms_BN ms_MY
mt mt_MT my my_MM nb nb_NO
nl nl_AW nl_BE nl_CW nl_NL nl_SX
nn nn_NO nso nso_ZA om om_ET
om_KE or or_IN pa pa_Arab pa_Arab_PK
pa_Guru pa_Guru_IN pl pl_PL ps ps_AF
pt pt_AO pt_BR pt_GW pt_MZ pt_PT
pt_ST ro ro_MD ro_RO ru ru_MD
ru_RU ru_UA se se_FI se_NO si
si_LK sk sk_SK sl sl_SI sq
sq_AL sr sr_Cyrl sr_Cyrl_BA sr_Cyrl_ME sr_Cyrl_RS
sr_Latn sr_Latn_BA sr_Latn_ME sr_Latn_RS st st_LS
st_ZA sv sv_FI sv_SE sw sw_KE
sw_TZ ta ta_IN ta_LK te te_IN
th th_TH tn tn_ZA to to_TO
tr tr_TR uk uk_UA ur ur_IN
ur_PK vi vi_VN wae wae_CH xh
xh_ZA yo yo_NG zh zh_Hans zh_Hans_CN
zh_Hans_SG zh_Hant zh_Hant_HK zh_Hant_MO zh_Hant_TW zu
zu_ZA

Or grouped:

af (af_NA, af_ZA)
ar (ar_001, ar_AE, ar_BH, ar_DZ, ar_EG, ar_IQ, ar_JO, ar_KW, ar_LB, ar_LY, ar_MA, ar_OM, ar_QA, ar_SA, ar_SD, ar_SY, ar_TN, ar_YE)
as (as_IN)
az (az_Latn, az_Latn_AZ)
be (be_BY)
bg (bg_BG)
bn (bn_BD, bn_IN)
bs (bs_BA)
ca (ca_ES)
cs (cs_CZ)
cy (cy_GB)
da (da_DK)
de (de_AT, de_BE, de_CH, de_DE, de_LI, de_LU)
dz
ee (ee_GH, ee_TG)
el (el_CY, el_GR)
en (en_AS, en_AU, en_BB, en_BE, en_BM, en_BW, en_BZ, en_CA, en_GB, en_GU, en_HK, en_IE, en_IN, en_JM, en_MH, en_MP, en_MT, en_MU, en_NA, en_NZ, en_PH, en_PK, en_SG, en_TT, en_UM, en_VI, en_ZA, en_ZW)
en_US
en_US_POSIX
eo
es (es_419, es_AR, es_BO, es_CL, es_CO, es_CR, es_DO, es_EC, es_ES, es_GQ, es_GT, es_HN, es_MX, es_NI, es_PA, es_PE, es_PR, es_PY, es_SV, es_US, es_UY, es_VE)
et (et_EE)
fa (fa_IR)
fa_AF
fi (fi_FI)
fil (fil_PH)
fo (fo_FO)
fr (fr_BE, fr_BF, fr_BI, fr_BJ, fr_BL, fr_CD, fr_CF, fr_CG, fr_CH, fr_CI, fr_CM, fr_DJ, fr_FR, fr_GA, fr_GN, fr_GP, fr_GQ, fr_KM, fr_LU, fr_MC, fr_MF, fr_MG, fr_ML, fr_MQ, fr_NE, fr_RE, fr_RW, fr_SN, fr_TD, fr_TG)
fr_CA
gu (gu_IN)
ha (ha_Latn, ha_Latn_GH, ha_Latn_NE, ha_Latn_NG)
haw (haw_US)
he (he_IL)
hi (hi_IN)
hr (hr_HR)
hu (hu_HU)
hy (hy_AM)
ig (ig_NG)
is (is_IS)
ja (ja_JP)
kk (kk_KZ)
kl (kl_GL)
km (km_KH)
kn (kn_IN)
ko (ko_KR)
kok (kok_IN)
ln (ln_CD, ln_CG)
lt (lt_LT)
lv (lv_LV)
mk (mk_MK)
ml (ml_IN)
mr (mr_IN)
mt (mt_MT)
my (my_MM)
nb (nb_NO)
nn (nn_NO)
nso (nso_ZA)
om (om_ET, om_KE)
or (or_IN)
pa (pa_Arab, pa_Arab_PK, pa_Guru, pa_Guru_IN)
pl (pl_PL)
ps (ps_AF)
ro (ro_RO, ro_MD)
root (chr, chr_US, ga, ga_IE, id, id_ID, it, it_CH, it_IT, ka, ka_GE, ky, ky_KG, ms, ms_BN, ms_MY, nl, nl_AW, nl_BE, nl_CW, nl_NL, nl_SX, pt, pt_AO, pt_BR, pt_GW, pt_MZ, pt_PT, pt_ST, st, st_LS, st_ZA, sw, sw_KE, sw_TZ, xh, xh_ZA, zu, zu_ZA)
ru (ru_MD, ru_RU, ru_UA)
se (se_FI, se_NO)
si (si_LK)
sk (sk_SK)
sl (sl_SI)
sq (sq_AL)
sr (sr_Cyrl, sr_Cyrl_BA, sr_Cyrl_ME, sr_Cyrl_RS)
sr_Latn (sr_Latn_RS, sr_Latn_BA, sr_Latn_ME)
sv (sv_FI, sv_SE)
ta (ta_IN, ta_LK)
te (te_IN)
th (th_TH)
tn (tn_ZA)
to (to_TO)
tr (tr_TR)
uk (uk_UA)
ur (ur_PK, ur_IN)
vi (vi_VN)
wae (wae_CH)
yo (yo_NG)
zh (zh_Hans, zh_Hans_CN, zh_Hans_SG)
zh_Hant (zh_Hant_HK, zh_Hant_MO, zh_Hant_TW)

You can see the base locale in the filename, e.g. de.xml, when you go inside the file, you fined an element like this one:

<collations validSubLocales="de_AT de_BE de_CH de_DE de_LI de_LU">

which lists additional Sublocales. Script and Territory is available in the file as well.

It takes some time until the CLDR data comes into Intl (C) which then is bind against PHP Intl extension. So cross check with the Intl library version / binding on your system. Just in case it differs a bit from your installation.

Get default locale for language in PHP

Having thought more about the problem and the particular setup I have, I came up with this solution, which seems to work. Note that I don't have control over what languages I need to support: there are translation files dropped into a predefined place and system locales installed by someone else. During runtime, I need to support a particular language if corresponding translation file exists and and system locale is installed. This got me to this solution:

function getLocale($lang)
{
$locs = array();
exec('locale -a', $locs);

$locale = 'en_GB';
foreach($locs as $l)
{
$regex = "/$lang\_[A-Z]{2}$/";
if(preg_match($regex, $l) && file_exists(TRANSROOT . "/$lang.php"))
{
$locale = $l;
break;
}
}

return $locale;
}

I'm defaulting to en_GB if I cannot resolve a locale, because I know for certain that en_GB is installed (we're located in the UK as are our servers).

How to pass list of existing locales into dropdown menu

If you have multiple locales defined in config/app.php, like described here:

'locales' => ['en' => 'English', 'sv' => 'Swedish'],

You could try to do this:

{!! Form::select('language', array_flip(config('app.locales')), null, ['placeholder' => 'Pick a language']) !!}

config() will get locales list and array_flip() will swap keys and values for Form::select.

Google App engine: List of available locales for PHP

Google App Engine supports only standard "C" locale.

PHP: Where does setlocale's Value Come From?

It appears that PHP is picking these values up directly from the shell's environment, and that the value for LC_CTYPE will also check LANG's value

% cat test.php
<?php
echo setlocale(LC_ALL, 0),"\n";

% echo $LANG
en_US.UTF-8

% echo $LC_CTYPE

% php test.php
C/en_US.UTF-8/C/C/C/C

% LC_CTYPE=C php test.php
C

% LANG=C php test.php
C

% LANG=C LC_CTYPE=en_US.UTF-8 php test.php
C/en_US.UTF-8/C/C/C/C

So, MacOS appears to set LANG to en_US.UTF-8 for a user's personal shell. My presumption is when I'm starting my php-fpm instance via

brew services start php

that the -- LaunchAgent? -- is using a shell where LANG or LC_CTYPE is set to C -- presumably because only humans are interesting in seeing things formatted as UTF-8.



Related Topics



Leave a reply



Submit