Created
December 27, 2015 15:40
-
-
Save armandduijn/ffedae5cbb45695ea054 to your computer and use it in GitHub Desktop.
Class to convert a language to a 2-letter country code and vice versa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Language { | |
/* | |
* Dictionary of languages with 2-letter country codes (adapted ISO 639-1) | |
* @var array | |
*/ | |
protected static $dictionary = [ | |
'Abkhaz' => 'AB', | |
'Afar' => 'AA', | |
'Afrikaans' => 'AF', | |
'Akan' => 'AK', | |
'Albanian' => 'SQ', | |
'Amharic' => 'AM', | |
'Arabic' => 'AR', | |
'Aragonese' => 'AN', | |
'Armenian' => 'HY', | |
'Assamese' => 'AS', | |
'Avaric' => 'AV', | |
'Avestan' => 'AE', | |
'Aymara' => 'AY', | |
'Azerbaijani' => 'AZ', | |
'Bambara' => 'BM', | |
'Bashkir' => 'BA', | |
'Basque' => 'EU', | |
'Belarusian' => 'BE', | |
'Bengali' => 'BN', | |
'Bangla' => 'BN', | |
'Bihari' => 'BH', | |
'Bislama' => 'BI', | |
'Bosnian' => 'BS', | |
'Breton' => 'BR', | |
'Bulgarian' => 'BG', | |
'Burmese' => 'MY', | |
'Catalan' => 'CA', | |
'Chamorro' => 'CH', | |
'Chechen' => 'CE', | |
'Chichewa' => 'NY', | |
'Chewa' => 'NY', | |
'Nyanja' => 'NY', | |
'Chinese' => 'ZH', | |
'Chuvash' => 'CV', | |
'Cornish' => 'KW', | |
'Corsican' => 'CO', | |
'Cree' => 'CR', | |
'Croatian' => 'HR', | |
'Czech' => 'CS', | |
'Danish' => 'DA', | |
'Divehi' => 'DV', | |
'Dhivehi' => 'DV', | |
'Maldivian' => 'DV', | |
'Dutch' => 'NL', | |
'Dzongkha' => 'DZ', | |
'English' => 'EN', | |
'Esperanto' => 'EO', | |
'Estonian' => 'ET', | |
'Ewe' => 'EE', | |
'Faroese' => 'FO', | |
'Fijian' => 'FJ', | |
'Finnish' => 'FI', | |
'French' => 'FR', | |
'Fula' => 'FF', | |
'Fulah' => 'FF', | |
'Pulaar' => 'FF', | |
'Pular' => 'FF', | |
'Galician' => 'GL', | |
'Georgian' => 'KA', | |
'German' => 'DE', | |
'Greek' => 'EL', | |
'Guaraní' => 'GN', | |
'Gujarati' => 'GU', | |
'Haitian' => 'HT', | |
'Haitian Creole' => 'HT', | |
'Hausa' => 'HA', | |
'Hebrew' => 'HE', | |
'Herero' => 'HZ', | |
'Hindi' => 'HI', | |
'Hiri Motu' => 'HO', | |
'Hungarian' => 'HU', | |
'Interlingua' => 'IA', | |
'Indonesian' => 'ID', | |
'Interlingue' => 'IE', | |
'Irish' => 'GA', | |
'Igbo' => 'IG', | |
'Inupiaq' => 'IK', | |
'Ido' => 'IO', | |
'Icelandic' => 'IS', | |
'Italian' => 'IT', | |
'Inuktitut' => 'IU', | |
'Japanese' => 'JA', | |
'Javanese' => 'JV', | |
'Kalaallisut' => 'KL', | |
'Greenlandic' => 'KL', | |
'Kannada' => 'KN', | |
'Kanuri' => 'KR', | |
'Kashmiri' => 'KS', | |
'Kazakh' => 'KK', | |
'Khmer' => 'KM', | |
'Kikuyu, Gikuyu' => 'KI', | |
'Kinyarwanda' => 'RW', | |
'Kyrgyz' => 'KY', | |
'Komi' => 'KV', | |
'Kongo' => 'KG', | |
'Korean' => 'KO', | |
'Kurdish' => 'KU', | |
'Kwanyama' => 'KJ', | |
'Kuanyama' => 'KJ', | |
'Latin' => 'LA', | |
'Luxembourgish' => 'LB', | |
'Letzeburgesch' => 'LB', | |
'Ganda' => 'LG', | |
'Limburgish' => 'LI', | |
'Limburgan' => 'LI', | |
'Limburger' => 'LI', | |
'Lingala' => 'LN', | |
'Lao' => 'LO', | |
'Lithuanian' => 'LT', | |
'Luba-Katanga' => 'LU', | |
'Latvian' => 'LV', | |
'Manx' => 'GV', | |
'Macedonian' => 'MK', | |
'Malagasy' => 'MG', | |
'Malay' => 'MS', | |
'Malayalam' => 'ML', | |
'Maltese' => 'MT', | |
'Māori' => 'MI', | |
'Marathi' => 'MR', | |
'Marshallese' => 'MH', | |
'Mongolian' => 'MN', | |
'Nauru' => 'NA', | |
'Navajo' => 'NV', | |
'Navaho' => 'NV', | |
'Northern Ndebele' => 'ND', | |
'Nepali' => 'NE', | |
'Ndonga' => 'NG', | |
'Norwegian Bokmål' => 'NB', | |
'Norwegian Nynorsk' => 'NN', | |
'Norwegian' => 'NO', | |
'Nuosu' => 'II', | |
'Southern Ndebele' => 'NR', | |
'Occitan' => 'OC', | |
'Ojibwe' => 'OJ', | |
'Ojibwa' => 'OJ', | |
'Old Church Slavonic' => 'CU', | |
'Church Slavonic' => 'CU', | |
'Old Bulgarian' => 'CU', | |
'Oromo' => 'OM', | |
'Oriya' => 'OR', | |
'Ossetian' => 'OS', | |
'Ossetic' => 'OS', | |
'Panjabi' => 'PA', | |
'Punjabi' => 'PA', | |
'Pāli' => 'PI', | |
'Persian' => 'FA', | |
'Farsi' => 'FA', | |
'Polish' => 'PL', | |
'Pashto' => 'PS', | |
'Pushto' => 'PS', | |
'Portuguese' => 'PT', | |
'Quechua' => 'QU', | |
'Romansh' => 'RM', | |
'Kirundi' => 'RN', | |
'Romanian' => 'RO', | |
'Russian' => 'RU', | |
'Sanskrit' => 'SA', | |
'Sardinian' => 'SC', | |
'Sindhi' => 'SD', | |
'Northern Sami' => 'SE', | |
'Samoan' => 'SM', | |
'Sango' => 'SG', | |
'Serbian' => 'SR', | |
'Scottish Gaelic' => 'GD', | |
'Gaelic' => 'GD', | |
'Shona' => 'SN', | |
'Sinhala' => 'SI', | |
'Sinhalese' => 'SI', | |
'Slovak' => 'SK', | |
'Slovene' => 'SL', | |
'Somali' => 'SO', | |
'Southern Sotho' => 'ST', | |
'Spanish' => 'ES', | |
'Sundanese' => 'SU', | |
'Swahili' => 'SW', | |
'Swati' => 'SS', | |
'Swedish' => 'SV', | |
'Tamil' => 'TA', | |
'Telugu' => 'TE', | |
'Tajik' => 'TG', | |
'Thai' => 'TH', | |
'Tigrinya' => 'TI', | |
'Tibetan Standard' => 'BO', | |
'Tibetan' => 'BO', | |
'Central' => 'BO', | |
'Turkmen' => 'TK', | |
'Tagalog' => 'TL', | |
'Tswana' => 'TN', | |
'Tonga' => 'TO', | |
'Turkish' => 'TR', | |
'Tsonga' => 'TS', | |
'Tatar' => 'TT', | |
'Twi' => 'TW', | |
'Tahitian' => 'TY', | |
'Uyghur' => 'UG', | |
'Ukrainian' => 'UK', | |
'Urdu' => 'UR', | |
'Uzbek' => 'UZ', | |
'Venda' => 'VE', | |
'Vietnamese' => 'VI', | |
'Volapük' => 'VO', | |
'Walloon' => 'WA', | |
'Welsh' => 'CY', | |
'Wolof' => 'WO', | |
'Western Frisian' => 'FY', | |
'Xhosa' => 'XH', | |
'Yiddish' => 'YI', | |
'Yoruba' => 'YO', | |
'Zhuang' => 'ZA', | |
'Chuang' => 'ZA', | |
'Zulu' => 'ZU', | |
]; | |
/* | |
* Get the 2-letter country code of a language | |
* | |
* @param string $language The language | |
* @return string|null | |
*/ | |
public static function getCountryCode($language) { | |
$language = ucwords(strtolower($language)); | |
foreach (self::$dictionary as $key => $value) { | |
if ($key === $language) { | |
return $value; | |
} | |
} | |
return null; | |
} | |
/* | |
* Get the language spoken in a country | |
* | |
* @param string $countryCode The 2-letter code of the country | |
* @return string|null | |
*/ | |
public static function getLanguage($countryCode) { | |
$countryCode = strtoupper($countryCode); | |
foreach (self::$dictionary as $key => $value) { | |
if ($value === $countryCode) { | |
return $key; | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment