Php get list of locales installed on system

By | March 26, 2012

On linux , the list of locales installed on the system can be fetched through the terminal command locale -a :

$ locale -a
C
C.UTF-8
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
es_AR.utf8
es_BO.utf8
es_CL.utf8
es_CO.utf8
es_CR.utf8
es_DO.utf8
es_EC.utf8
es_ES.utf8
es_GT.utf8
es_HN.utf8
es_MX.utf8
es_NI.utf8
es_PA.utf8
es_PE.utf8
es_PR.utf8
es_PY.utf8
es_SV.utf8
es_US.utf8
es_UY.utf8
es_VE.utf8
hi_IN
hi_IN.utf8
nl_AW
nl_AW.utf8
nl_BE.utf8
nl_NL.utf8
POSIX
si_LK
si_LK.utf8
zh_CN.utf8
zh_SG.utf8

Now this command can be used inside php to get the list. Along with this list we can also add some information like language name and country name using a list of countries and languages.

Here is a list of country codes :

<?php
/**
	References :
	1. http://en.wikipedia.org/wiki/ISO_3166-1#Officially_assigned_code_elements
*/
$country_codes = array(

'AF' => "AFGHANISTAN" , 
'AL' => "ALBANIA" , 
'DZ' => "ALGERIA" , 
'AS' => "AMERICAN SAMOA" , 
'AD' => "ANDORRA" , 
'AO' => "ANGOLA" , 
'AI' => "ANGUILLA" , 
'AQ' => "ANTARCTICA" , 
'AG' => "ANTIGUA AND BARBUDA" , 
'AR' => "ARGENTINA" , 
'AM' => "ARMENIA" , 
'AW' => "ARUBA" , 
'AU' => "AUSTRALIA" , 
'AT' => "AUSTRIA" , 
'AZ' => "AZERBAIJAN" , 
'BS' => "BAHAMAS" , 
'BH' => "BAHRAIN" , 
'BD' => "BANGLADESH" , 
'BB' => "BARBADOS" , 
'BY' => "BELARUS" , 
'BE' => "BELGIUM" , 
'BZ' => "BELIZE" , 
'BJ' => "BENIN" , 
'BM' => "BERMUDA" , 
'BT' => "BHUTAN" , 
'BO' => "BOLIVIA" , 
'BA' => "BOSNIA AND HERZEGOVINA" , 
'BW' => "BOTSWANA" , 
'BV' => "BOUVET ISLAND" , 
'BR' => "BRAZIL" , 
'IO' => "BRITISH INDIAN OCEAN TERRITORY" , 
'BN' => "BRUNEI DARUSSALAM" , 
'BG' => "BULGARIA" , 
'BF' => "BURKINA FASO" , 
'BI' => "BURUNDI" , 
'KH' => "CAMBODIA" , 
'CM' => "CAMEROON" , 
'CA' => "CANADA" , 
'CV' => "CAPE VERDE" , 
'KY' => "CAYMAN ISLANDS" , 
'CF' => "CENTRAL AFRICAN REPUBLIC" , 
'TD' => "CHAD" , 
'CL' => "CHILE" , 
'CN' => "CHINA" , 
'CX' => "CHRISTMAS ISLAND" , 
'CC' => "COCOS (KEELING) ISLANDS" , 
'CO' => "COLOMBIA" , 
'KM' => "COMOROS" , 
'CG' => "CONGO" , 
'CD' => "CONGO, THE DEMOCRATIC REPUBLIC OF THE" , 
'CK' => "COOK ISLANDS" , 
'CR' => "COSTA RICA" , 
'CI' => "COTE D'IVOIRE" , 
'HR' => "CROATIA" , 
'CU' => "CUBA" , 
'CY' => "CYPRUS" , 
'CZ' => "CZECH REPUBLIC" , 
'DK' => "DENMARK" , 
'DJ' => "DJIBOUTI" , 
'DM' => "DOMINICA" , 
'DO' => "DOMINICAN REPUBLIC" , 
'EC' => "ECUADOR" , 
'EG' => "EGYPT" , 
'SV' => "EL SALVADOR" , 
'GQ' => "EQUATORIAL GUINEA" , 
'ER' => "ERITREA" , 
'EE' => "ESTONIA" , 
'ET' => "ETHIOPIA" , 
'FK' => "FALKLAND ISLANDS (MALVINAS)" , 
'FO' => "FAROE ISLANDS" , 
'FJ' => "FIJI" , 
'FI' => "FINLAND" , 
'FR' => "FRANCE" , 
'GF' => "FRENCH GUIANA" , 
'PF' => "FRENCH POLYNESIA" , 
'TF' => "FRENCH SOUTHERN TERRITORIES" , 
'GA' => "GABON" , 
'GM' => "GAMBIA" , 
'GE' => "GEORGIA" , 
'DE' => "GERMANY" , 
'GH' => "GHANA" , 
'GI' => "GIBRALTAR" , 
'GR' => "GREECE" , 
'GL' => "GREENLAND" , 
'GD' => "GRENADA" , 
'GP' => "GUADELOUPE" , 
'GU' => "GUAM" , 
'GT' => "GUATEMALA" , 
'GN' => "GUINEA" , 
'GW' => "GUINEA-BISSAU" , 
'GY' => "GUYANA" , 
'HT' => "HAITI" , 
'HM' => "HEARD ISLAND AND MCDONALD ISLANDS" , 
'VA' => "HOLY SEE (VATICAN CITY STATE)" , 
'HN' => "HONDURAS" , 
'HK' => "HONG KONG" , 
'HU' => "HUNGARY" , 
'IS' => "ICELAND" , 
'IN' => "INDIA" , 
'ID' => "INDONESIA" , 
'IR' => "IRAN, ISLAMIC REPUBLIC OF" , 
'IQ' => "IRAQ" , 
'IE' => "IRELAND" , 
'IL' => "ISRAEL" , 
'IT' => "ITALY" , 
'JM' => "JAMAICA" , 
'JP' => "JAPAN" , 
'JO' => "JORDAN" , 
'KZ' => "KAZAKHSTAN" , 
'KE' => "KENYA" , 
'KI' => "KIRIBATI" , 
'KP' => "KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF" , 
'KR' => "KOREA, REPUBLIC OF" , 
'KW' => "KUWAIT" , 
'KG' => "KYRGYZSTAN" , 
'LA' => "LAO PEOPLE'S DEMOCRATIC REPUBLIC" , 
'LV' => "LATVIA" , 
'LB' => "LEBANON" , 
'LS' => "LESOTHO" , 
'LR' => "LIBERIA" , 
'LY' => "LIBYAN ARAB JAMAHIRIYA" , 
'LI' => "LIECHTENSTEIN" , 
'LT' => "LITHUANIA" , 
'LU' => "LUXEMBOURG" , 
'MO' => "MACAO" , 
'MK' => "MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF" , 
'MG' => "MADAGASCAR" , 
'MW' => "MALAWI" , 
'MY' => "MALAYSIA" , 
'MV' => "MALDIVES" , 
'ML' => "MALI" , 
'MT' => "MALTA" , 
'MH' => "MARSHALL ISLANDS" , 
'MQ' => "MARTINIQUE" , 
'MR' => "MAURITANIA" , 
'MU' => "MAURITIUS" , 
'YT' => "MAYOTTE" , 
'MX' => "MEXICO" , 
'FM' => "MICRONESIA, FEDERATED STATES OF" , 
'MD' => "MOLDOVA, REPUBLIC OF" , 
'MC' => "MONACO" , 
'MN' => "MONGOLIA" , 
'MS' => "MONTSERRAT" , 
'MA' => "MOROCCO" , 
'MZ' => "MOZAMBIQUE" , 
'MM' => "MYANMAR" , 
'NA' => "NAMIBIA" , 
'NR' => "NAURU" , 
'NP' => "NEPAL" , 
'NL' => "NETHERLANDS" , 
'AN' => "NETHERLANDS ANTILLES" , 
'NC' => "NEW CALEDONIA" , 
'NZ' => "NEW ZEALAND" , 
'NI' => "NICARAGUA" , 
'NE' => "NIGER" , 
'NG' => "NIGERIA" , 
'NU' => "NIUE" , 
'NF' => "NORFOLK ISLAND" , 
'MP' => "NORTHERN MARIANA ISLANDS" , 
'NO' => "NORWAY" , 
'OM' => "OMAN" , 
'PK' => "PAKISTAN" , 
'PW' => "PALAU" , 
'PS' => "PALESTINIAN TERRITORY, OCCUPIED" , 
'PA' => "PANAMA" , 
'PG' => "PAPUA NEW GUINEA" , 
'PY' => "PARAGUAY" , 
'PE' => "PERU" , 
'PH' => "PHILIPPINES" , 
'PN' => "PITCAIRN" , 
'PL' => "POLAND" , 
'PT' => "PORTUGAL" , 
'PR' => "PUERTO RICO" , 
'QA' => "QATAR" , 
'RE' => "REUNION" , 
'RO' => "ROMANIA" , 
'RU' => "RUSSIAN FEDERATION" , 
'RW' => "RWANDA" , 
'SH' => "SAINT HELENA" , 
'KN' => "SAINT KITTS AND NEVIS" , 
'LC' => "SAINT LUCIA" , 
'PM' => "SAINT PIERRE AND MIQUELON" , 
'VC' => "SAINT VINCENT AND THE GRENADINES" , 
'WS' => "SAMOA" , 
'SM' => "SAN MARINO" , 
'ST' => "SAO TOME AND PRINCIPE" , 
'SA' => "SAUDI ARABIA" , 
'SN' => "SENEGAL" , 
'CS' => "SERBIA AND MONTENEGRO" , 
'SC' => "SEYCHELLES" , 
'SL' => "SIERRA LEONE" , 
'SG' => "SINGAPORE" , 
'SK' => "SLOVAKIA" , 
'SI' => "SLOVENIA" , 
'SB' => "SOLOMON ISLANDS" , 
'SO' => "SOMALIA" , 
'ZA' => "SOUTH AFRICA" , 
'GS' => "SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS" , 
'ES' => "SPAIN" , 
'LK' => "SRI LANKA" , 
'SD' => "SUDAN" , 
'SR' => "SURINAME" , 
'SJ' => "SVALBARD AND JAN MAYEN" , 
'SZ' => "SWAZILAND" , 
'SE' => "SWEDEN" , 
'CH' => "SWITZERLAND" , 
'SY' => "SYRIAN ARAB REPUBLIC" , 
'TW' => "TAIWAN, PROVINCE OF CHINA" , 
'TJ' => "TAJIKISTAN" , 
'TZ' => "TANZANIA, UNITED REPUBLIC OF" , 
'TH' => "THAILAND" , 
'TL' => "TIMOR-LESTE" , 
'TG' => "TOGO" , 
'TK' => "TOKELAU" , 
'TO' => "TONGA" , 
'TT' => "TRINIDAD AND TOBAGO" , 
'TN' => "TUNISIA" , 
'TR' => "TURKEY" , 
'TM' => "TURKMENISTAN" , 
'TC' => "TURKS AND CAICOS ISLANDS" , 
'TV' => "TUVALU" , 
'UG' => "UGANDA" , 
'UA' => "UKRAINE" , 
'AE' => "UNITED ARAB EMIRATES" , 
'GB' => "UNITED KINGDOM" , 
'US' => "UNITED STATES" , 
'UM' => "UNITED STATES MINOR OUTLYING ISLANDS" , 
'UY' => "URUGUAY" , 
'UZ' => "UZBEKISTAN" , 
'VU' => "VANUATU" , 
'VE' => "VENEZUELA" , 
'VN' => "VIET NAM" , 
'VG' => "VIRGIN ISLANDS, BRITISH" , 
'VI' => "VIRGIN ISLANDS, U.S." , 
'WF' => "WALLIS AND FUTUNA" , 
'EH' => "WESTERN SAHARA" , 
'YE' => "YEMEN" , 
'ZM' => "ZAMBIA" , 
'ZW' => "ZIMBABWE" , 
);

//End of country codes

And now a list of languages :

<?php
/**
	ISO 639-1 Language Codes
	Useful in Locale analysis
	
	References :
	1. http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
	2. http://blog.xoundboy.com/?p=235
*/

$language_codes = array(
		'en' => 'English' , 
		'aa' => 'Afar' , 
		'ab' => 'Abkhazian' , 
		'af' => 'Afrikaans' , 
		'am' => 'Amharic' , 
		'ar' => 'Arabic' , 
		'as' => 'Assamese' , 
		'ay' => 'Aymara' , 
		'az' => 'Azerbaijani' , 
		'ba' => 'Bashkir' , 
		'be' => 'Byelorussian' , 
		'bg' => 'Bulgarian' , 
		'bh' => 'Bihari' , 
		'bi' => 'Bislama' , 
		'bn' => 'Bengali/Bangla' , 
		'bo' => 'Tibetan' , 
		'br' => 'Breton' , 
		'ca' => 'Catalan' , 
		'co' => 'Corsican' , 
		'cs' => 'Czech' , 
		'cy' => 'Welsh' , 
		'da' => 'Danish' , 
		'de' => 'German' , 
		'dz' => 'Bhutani' , 
		'el' => 'Greek' , 
		'eo' => 'Esperanto' , 
		'es' => 'Spanish' , 
		'et' => 'Estonian' , 
		'eu' => 'Basque' , 
		'fa' => 'Persian' , 
		'fi' => 'Finnish' , 
		'fj' => 'Fiji' , 
		'fo' => 'Faeroese' , 
		'fr' => 'French' , 
		'fy' => 'Frisian' , 
		'ga' => 'Irish' , 
		'gd' => 'Scots/Gaelic' , 
		'gl' => 'Galician' , 
		'gn' => 'Guarani' , 
		'gu' => 'Gujarati' , 
		'ha' => 'Hausa' , 
		'hi' => 'Hindi' , 
		'hr' => 'Croatian' , 
		'hu' => 'Hungarian' , 
		'hy' => 'Armenian' , 
		'ia' => 'Interlingua' , 
		'ie' => 'Interlingue' , 
		'ik' => 'Inupiak' , 
		'in' => 'Indonesian' , 
		'is' => 'Icelandic' , 
		'it' => 'Italian' , 
		'iw' => 'Hebrew' , 
		'ja' => 'Japanese' , 
		'ji' => 'Yiddish' , 
		'jw' => 'Javanese' , 
		'ka' => 'Georgian' , 
		'kk' => 'Kazakh' , 
		'kl' => 'Greenlandic' , 
		'km' => 'Cambodian' , 
		'kn' => 'Kannada' , 
		'ko' => 'Korean' , 
		'ks' => 'Kashmiri' , 
		'ku' => 'Kurdish' , 
		'ky' => 'Kirghiz' , 
		'la' => 'Latin' , 
		'ln' => 'Lingala' , 
		'lo' => 'Laothian' , 
		'lt' => 'Lithuanian' , 
		'lv' => 'Latvian/Lettish' , 
		'mg' => 'Malagasy' , 
		'mi' => 'Maori' , 
		'mk' => 'Macedonian' , 
		'ml' => 'Malayalam' , 
		'mn' => 'Mongolian' , 
		'mo' => 'Moldavian' , 
		'mr' => 'Marathi' , 
		'ms' => 'Malay' , 
		'mt' => 'Maltese' , 
		'my' => 'Burmese' , 
		'na' => 'Nauru' , 
		'ne' => 'Nepali' , 
		'nl' => 'Dutch' , 
		'no' => 'Norwegian' , 
		'oc' => 'Occitan' , 
		'om' => '(Afan)/Oromoor/Oriya' , 
		'pa' => 'Punjabi' , 
		'pl' => 'Polish' , 
		'ps' => 'Pashto/Pushto' , 
		'pt' => 'Portuguese' , 
		'qu' => 'Quechua' , 
		'rm' => 'Rhaeto-Romance' , 
		'rn' => 'Kirundi' , 
		'ro' => 'Romanian' , 
		'ru' => 'Russian' , 
		'rw' => 'Kinyarwanda' , 
		'sa' => 'Sanskrit' , 
		'sd' => 'Sindhi' , 
		'sg' => 'Sangro' , 
		'sh' => 'Serbo-Croatian' , 
		'si' => 'Singhalese' , 
		'sk' => 'Slovak' , 
		'sl' => 'Slovenian' , 
		'sm' => 'Samoan' , 
		'sn' => 'Shona' , 
		'so' => 'Somali' , 
		'sq' => 'Albanian' , 
		'sr' => 'Serbian' , 
		'ss' => 'Siswati' , 
		'st' => 'Sesotho' , 
		'su' => 'Sundanese' , 
		'sv' => 'Swedish' , 
		'sw' => 'Swahili' , 
		'ta' => 'Tamil' , 
		'te' => 'Tegulu' , 
		'tg' => 'Tajik' , 
		'th' => 'Thai' , 
		'ti' => 'Tigrinya' , 
		'tk' => 'Turkmen' , 
		'tl' => 'Tagalog' , 
		'tn' => 'Setswana' , 
		'to' => 'Tonga' , 
		'tr' => 'Turkish' , 
		'ts' => 'Tsonga' , 
		'tt' => 'Tatar' , 
		'tw' => 'Twi' , 
		'uk' => 'Ukrainian' , 
		'ur' => 'Urdu' , 
		'uz' => 'Uzbek' , 
		'vi' => 'Vietnamese' , 
		'vo' => 'Volapuk' , 
		'wo' => 'Wolof' , 
		'xh' => 'Xhosa' , 
		'yo' => 'Yoruba' , 
		'zh' => 'Chinese' , 
		'zu' => 'Zulu' , 
		);

Now the list of locales + countries + languages can be used to present information properly. Here is the code :

$locale_data = array();
		
//Get locales from Linux terminal command locale
$locales = shell_exec('locale -a');

$locales = explode("n" , $locales);

foreach($locales as $c => $l)
{
	if(strlen($l))
	{
		$parts = explode('.' , $l);
		$lc = $parts[0];
		
		list($lcode , $ccode) = explode('_' , $lc);
		
		$lcode = strtolower($lcode);
		
		$language = $language_codes[$lcode];
		$country = $country_codes[$ccode];
		
		if(strlen($language) and strlen($country))
		{
			$locale_data[$l] = "$language - $country - {$parts[1]}";
		}
	}
}

print_r($locale_data);

Output

Array
(
    [en_AG] => English - ANTIGUA AND BARBUDA - 
    [en_AG.utf8] => English - ANTIGUA AND BARBUDA - utf8
    [en_AU.utf8] => English - AUSTRALIA - utf8
    [en_BW.utf8] => English - BOTSWANA - utf8
    [en_CA.utf8] => English - CANADA - utf8
    [en_DK.utf8] => English - DENMARK - utf8
    [en_GB.utf8] => English - UNITED KINGDOM - utf8
    [en_HK.utf8] => English - HONG KONG - utf8
    [en_IE.utf8] => English - IRELAND - utf8
    [en_IN] => English - INDIA - 
    [en_IN.utf8] => English - INDIA - utf8
    [en_NG] => English - NIGERIA - 
    [en_NG.utf8] => English - NIGERIA - utf8
    [en_NZ.utf8] => English - NEW ZEALAND - utf8
    [en_PH.utf8] => English - PHILIPPINES - utf8
    [en_SG.utf8] => English - SINGAPORE - utf8
    [en_US.utf8] => English - UNITED STATES - utf8
    [en_ZA.utf8] => English - SOUTH AFRICA - utf8
    [en_ZM] => English - ZAMBIA - 
    [en_ZM.utf8] => English - ZAMBIA - utf8
    [en_ZW.utf8] => English - ZIMBABWE - utf8
    [es_AR.utf8] => Spanish - ARGENTINA - utf8
    [es_BO.utf8] => Spanish - BOLIVIA - utf8
    [es_CL.utf8] => Spanish - CHILE - utf8
    [es_CO.utf8] => Spanish - COLOMBIA - utf8
    [es_CR.utf8] => Spanish - COSTA RICA - utf8
    [es_DO.utf8] => Spanish - DOMINICAN REPUBLIC - utf8
    [es_EC.utf8] => Spanish - ECUADOR - utf8
    [es_ES.utf8] => Spanish - SPAIN - utf8
    [es_GT.utf8] => Spanish - GUATEMALA - utf8
    [es_HN.utf8] => Spanish - HONDURAS - utf8
    [es_MX.utf8] => Spanish - MEXICO - utf8
    [es_NI.utf8] => Spanish - NICARAGUA - utf8
    [es_PA.utf8] => Spanish - PANAMA - utf8
    [es_PE.utf8] => Spanish - PERU - utf8
    [es_PR.utf8] => Spanish - PUERTO RICO - utf8
    [es_PY.utf8] => Spanish - PARAGUAY - utf8
    [es_SV.utf8] => Spanish - EL SALVADOR - utf8
    [es_US.utf8] => Spanish - UNITED STATES - utf8
    [es_UY.utf8] => Spanish - URUGUAY - utf8
    [es_VE.utf8] => Spanish - VENEZUELA - utf8
    [hi_IN] => Hindi - INDIA - 
    [hi_IN.utf8] => Hindi - INDIA - utf8
    [nl_AW] => Dutch - ARUBA - 
    [nl_AW.utf8] => Dutch - ARUBA - utf8
    [nl_BE.utf8] => Dutch - BELGIUM - utf8
    [nl_NL.utf8] => Dutch - NETHERLANDS - utf8
    [si_LK] => Singhalese - SRI LANKA - 
    [si_LK.utf8] => Singhalese - SRI LANKA - utf8
    [zh_CN.utf8] => Chinese - CHINA - utf8
    [zh_SG.utf8] => Chinese - SINGAPORE - utf8
)

Now the above array can be used to generate a dropdown , from which the locale can be selected and saved.
The locales C and POSIX have been omitted.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

3 Comments

Php get list of locales installed on system

Leave a Reply

Your email address will not be published. Required fields are marked *