Skip to content

Instantly share code, notes, and snippets.

@mrmartin
Last active September 18, 2017 11:51
Show Gist options
  • Save mrmartin/8b86a855e782c6178c800b96efe61bbe to your computer and use it in GitHub Desktop.
Save mrmartin/8b86a855e782c6178c800b96efe61bbe to your computer and use it in GitHub Desktop.
convert any Accept-Language value to human-readable form in php
#associative array of codes and human-readable codes for broswer languages
#including countries for multi-country languages
$langs_accept_language_and_country = array("af"=>"Afrikaans", "hr"=>"Croatian", "el"=>"Greek", "pl"=>"Polish", "sx"=>"Sutu", "sq"=>"Albanian", "cs"=>"Czech", "gu"=>"Gujurati", "pt"=>"Portuguese", "sw"=>"Swahili", "ar"=>"Arabic (Standard)", "da"=>"Danish", "ht"=>"Haitian", "pt_br"=>"Portuguese (Brazil)", "sv"=>"Swedish", "ar_dz"=>"Arabic (Algeria)", "nl"=>"Dutch (Standard)", "he"=>"Hebrew", "pa"=>"Punjabi", "sv_fi"=>"Swedish (Finland)", "ar_bh"=>"Arabic (Bahrain)", "nl_be"=>"Dutch (Belgian)", "hi"=>"Hindi", "pa_in"=>"Punjabi (India)", "sv_sv"=>"Swedish (Sweden)", "ar_eg"=>"Arabic (Egypt)", "en"=>"English", "hu"=>"Hungarian", "pa_pk"=>"Punjabi (Pakistan)", "ta"=>"Tamil", "ar_iq"=>"Arabic (Iraq)", "en_au"=>"English (Australia)", "is"=>"Icelandic", "qu"=>"Quechua", "tt"=>"Tatar", "ar_jo"=>"Arabic (Jordan)", "en_bz"=>"English (Belize)", "id"=>"Indonesian", "rm"=>"Rhaeto-Romanic", "te"=>"Teluga", "ar_kw"=>"Arabic (Kuwait)", "en_ca"=>"English (Canada)", "iu"=>"Inuktitut", "ro"=>"Romanian", "th"=>"Thai", "ar_lb"=>"Arabic (Lebanon)", "en_ie"=>"English (Ireland)", "ga"=>"Irish", "ro_mo"=>"Romanian (Moldavia)", "tig"=>"Tigre", "ar_ly"=>"Arabic (Libya)", "en_jm"=>"English (Jamaica)", "it"=>"Italian (Standard)", "ru"=>"Russian", "ts"=>"Tsonga", "ar_ma"=>"Arabic (Morocco)", "en_nz"=>"English (New Zealand)", "it_ch"=>"Italian (Switzerland)", "ru_mo"=>"Russian (Moldavia)", "tn"=>"Tswana", "ar_om"=>"Arabic (Oman)", "en_ph"=>"English (Philippines)", "ja"=>"Japanese", "sz"=>"Sami (Lappish)", "tr"=>"Turkish", "ar_qa"=>"Arabic (Qatar)", "en_za"=>"English (South Africa)", "kn"=>"Kannada", "sg"=>"Sango", "tk"=>"Turkmen", "ar_sa"=>"Arabic (Saudi Arabia)", "en_tt"=>"English (Trinidad & Tobago)", "ks"=>"Kashmiri", "sa"=>"Sanskrit", "uk"=>"Ukrainian", "ar_sy"=>"Arabic (Syria)", "en_gb"=>"English (United Kingdom)", "kk"=>"Kazakh", "sc"=>"Sardinian", "hsb"=>"Upper Sorbian", "ar_tn"=>"Arabic (Tunisia)", "en_us"=>"English (United States)", "km"=>"Khmer", "gd"=>"Scots Gaelic", "ur"=>"Urdu", "ar_ae"=>"Arabic (U.A.E.)", "en_zw"=>"English (Zimbabwe)", "ky"=>"Kirghiz", "sd"=>"Sindhi", "ve"=>"Venda", "ar_ye"=>"Arabic (Yemen)", "eo"=>"Esperanto", "tlh"=>"Klingon", "si"=>"Singhalese", "vi"=>"Vietnamese", "ar"=>"Aragonese", "et"=>"Estonian", "ko"=>"Korean", "sr"=>"Serbian", "vo"=>"Volapuk", "hy"=>"Armenian", "fo"=>"Faeroese", "ko_kp"=>"Korean (North Korea)", "sk"=>"Slovak", "wa"=>"Walloon", "as"=>"Assamese", "fa"=>"Farsi", "ko_kr"=>"Korean (South Korea)", "sl"=>"Slovenian", "cy"=>"Welsh", "ast"=>"Asturian", "fj"=>"Fijian", "la"=>"Latin", "so"=>"Somani", "xh"=>"Xhosa", "az"=>"Azerbaijani", "fi"=>"Finnish", "lv"=>"Latvian", "sb"=>"Sorbian", "ji"=>"Yiddish", "eu"=>"Basque", "fr"=>"French (Standard)", "lt"=>"Lithuanian", "es"=>"Spanish", "zu"=>"Zulu", "bg"=>"Bulgarian", "fr_be"=>"French (Belgium)", "lb"=>"Luxembourgish", "es_ar"=>"Spanish (Argentina)", "be"=>"Belarusian", "fr_ca"=>"French (Canada)", "mk"=>"FYRO Macedonian", "es_bo"=>"Spanish (Bolivia)", "bn"=>"Bengali", "fr_fr"=>"French (France)", "ms"=>"Malay", "es_cl"=>"Spanish (Chile)", "bs"=>"Bosnian", "fr_lu"=>"French (Luxembourg)", "ml"=>"Malayalam", "es_co"=>"Spanish (Colombia)", "br"=>"Breton", "fr_mc"=>"French (Monaco)", "mt"=>"Maltese", "es_cr"=>"Spanish (Costa Rica)", "bg"=>"Bulgarian", "fr_ch"=>"French (Switzerland)", "mi"=>"Maori", "es_do"=>"Spanish (Dominican Republic)", "my"=>"Burmese", "fy"=>"Frisian", "mr"=>"Marathi", "es_ec"=>"Spanish (Ecuador)", "ca"=>"Catalan", "fur"=>"Friulian", "mo"=>"Moldavian", "es_sv"=>"Spanish (El Salvador)", "ch"=>"Chamorro", "gd"=>"Gaelic (Scots)", "nv"=>"Navajo", "es_gt"=>"Spanish (Guatemala)", "ce"=>"Chechen", "gd_ie"=>"Gaelic (Irish)", "ng"=>"Ndonga", "es_hn"=>"Spanish (Honduras)", "zh"=>"Chinese", "gl"=>"Galacian", "ne"=>"Nepali", "es_mx"=>"Spanish (Mexico)", "zh_hk"=>"Chinese (Hong Kong)", "ka"=>"Georgian", "no"=>"Norwegian", "es_ni"=>"Spanish (Nicaragua)", "zh_cn"=>"Chinese (PRC)", "de"=>"German (Standard)", "nb"=>"Norwegian (Bokmal)", "es_pa"=>"Spanish (Panama)", "zh_sg"=>"Chinese (Singapore)", "de_at"=>"German (Austria)", "nn"=>"Norwegian (Nynorsk)", "es_py"=>"Spanish (Paraguay)", "zh_tw"=>"Chinese (Taiwan)", "de_de"=>"German (Germany)", "oc"=>"Occitan", "es_pe"=>"Spanish (Peru)", "cv"=>"Chuvash", "de_li"=>"German (Liechtenstein)", "or"=>"Oriya", "es_pr"=>"Spanish (Puerto Rico)", "co"=>"Corsican", "de_lu"=>"German (Luxembourg)", "om"=>"Oromo", "es_es"=>"Spanish (Spain)", "cr"=>"Cree", "de_ch"=>"German (Switzerland)", "fa"=>"Persian", "es_uy"=>"Spanish (Uruguay)", "fa_ir"=>"Persian/Iran", "es_ve"=>"Spanish (Venezuela)");
#excluding countries
$langs_accept_language = array("af"=>"Afrikaans", "hr"=>"Croatian", "el"=>"Greek", "pl"=>"Polish", "sx"=>"Sutu", "sq"=>"Albanian", "cs"=>"Czech", "gu"=>"Gujurati", "pt"=>"Portuguese", "sw"=>"Swahili", "ar"=>"Arabic", "da"=>"Danish", "ht"=>"Haitian", "pt_br"=>"Portuguese", "sv"=>"Swedish", "ar_dz"=>"Arabic", "nl"=>"Dutch", "he"=>"Hebrew", "pa"=>"Punjabi", "sv_fi"=>"Swedish", "ar_bh"=>"Arabic", "nl_be"=>"Dutch", "hi"=>"Hindi", "pa_in"=>"Punjabi", "sv_sv"=>"Swedish", "ar_eg"=>"Arabic", "en"=>"English", "hu"=>"Hungarian", "pa_pk"=>"Punjabi", "ta"=>"Tamil", "ar_iq"=>"Arabic", "en_au"=>"English", "is"=>"Icelandic", "qu"=>"Quechua", "tt"=>"Tatar", "ar_jo"=>"Arabic", "en_bz"=>"English", "id"=>"Indonesian", "rm"=>"Rhaeto-Romanic", "te"=>"Teluga", "ar_kw"=>"Arabic", "en_ca"=>"English", "iu"=>"Inuktitut", "ro"=>"Romanian", "th"=>"Thai", "ar_lb"=>"Arabic", "en_ie"=>"English", "ga"=>"Irish", "ro_mo"=>"Romanian", "tig"=>"Tigre", "ar_ly"=>"Arabic", "en_jm"=>"English", "it"=>"Italian", "ru"=>"Russian", "ts"=>"Tsonga", "ar_ma"=>"Arabic", "en_nz"=>"English", "it_ch"=>"Italian", "ru_mo"=>"Russian", "tn"=>"Tswana", "ar_om"=>"Arabic", "en_ph"=>"English", "ja"=>"Japanese", "sz"=>"Sami", "tr"=>"Turkish", "ar_qa"=>"Arabic", "en_za"=>"English", "kn"=>"Kannada", "sg"=>"Sango", "tk"=>"Turkmen", "ar_sa"=>"Arabic", "en_tt"=>"English", "ks"=>"Kashmiri", "sa"=>"Sanskrit", "uk"=>"Ukrainian", "ar_sy"=>"Arabic", "en_gb"=>"English", "kk"=>"Kazakh", "sc"=>"Sardinian", "hsb"=>"Upper Sorbian", "ar_tn"=>"Arabic", "en_us"=>"English", "km"=>"Khmer", "gd"=>"Scots Gaelic", "ur"=>"Urdu", "ar_ae"=>"Arabic", "en_zw"=>"English", "ky"=>"Kirghiz", "sd"=>"Sindhi", "ve"=>"Venda", "ar_ye"=>"Arabic", "eo"=>"Esperanto", "tlh"=>"Klingon", "si"=>"Singhalese", "vi"=>"Vietnamese", "ar"=>"Aragonese", "et"=>"Estonian", "ko"=>"Korean", "sr"=>"Serbian", "vo"=>"Volapuk", "hy"=>"Armenian", "fo"=>"Faeroese", "ko_kp"=>"Korean", "sk"=>"Slovak", "wa"=>"Walloon", "as"=>"Assamese", "fa"=>"Farsi", "ko_kr"=>"Korean", "sl"=>"Slovenian", "cy"=>"Welsh", "ast"=>"Asturian", "fj"=>"Fijian", "la"=>"Latin", "so"=>"Somani", "xh"=>"Xhosa", "az"=>"Azerbaijani", "fi"=>"Finnish", "lv"=>"Latvian", "sb"=>"Sorbian", "ji"=>"Yiddish", "eu"=>"Basque", "fr"=>"French", "lt"=>"Lithuanian", "es"=>"Spanish", "zu"=>"Zulu", "bg"=>"Bulgarian", "fr_be"=>"French", "lb"=>"Luxembourgish", "es_ar"=>"Spanish", "be"=>"Belarusian", "fr_ca"=>"French", "mk"=>"FYRO Macedonian", "es_bo"=>"Spanish", "bn"=>"Bengali", "fr_fr"=>"French", "ms"=>"Malay", "es_cl"=>"Spanish", "bs"=>"Bosnian", "fr_lu"=>"French", "ml"=>"Malayalam", "es_co"=>"Spanish", "br"=>"Breton", "fr_mc"=>"French", "mt"=>"Maltese", "es_cr"=>"Spanish", "bg"=>"Bulgarian", "fr_ch"=>"French", "mi"=>"Maori", "es_do"=>"Spanish", "my"=>"Burmese", "fy"=>"Frisian", "mr"=>"Marathi", "es_ec"=>"Spanish", "ca"=>"Catalan", "fur"=>"Friulian", "mo"=>"Moldavian", "es_sv"=>"Spanish", "ch"=>"Chamorro", "gd"=>"Gaelic", "nv"=>"Navajo", "es_gt"=>"Spanish", "ce"=>"Chechen", "gd_ie"=>"Gaelic", "ng"=>"Ndonga", "es_hn"=>"Spanish", "zh"=>"Chinese", "gl"=>"Galacian", "ne"=>"Nepali", "es_mx"=>"Spanish", "zh_hk"=>"Chinese", "ka"=>"Georgian", "no"=>"Norwegian", "es_ni"=>"Spanish", "zh_cn"=>"Chinese", "de"=>"German", "nb"=>"Norwegian", "es_pa"=>"Spanish", "zh_sg"=>"Chinese", "de_at"=>"German", "nn"=>"Norwegian", "es_py"=>"Spanish", "zh_tw"=>"Chinese", "de_de"=>"German", "oc"=>"Occitan", "es_pe"=>"Spanish", "cv"=>"Chuvash", "de_li"=>"German", "or"=>"Oriya", "es_pr"=>"Spanish", "co"=>"Corsican", "de_lu"=>"German", "om"=>"Oromo", "es_es"=>"Spanish", "cr"=>"Cree", "de_ch"=>"German", "fa"=>"Persian", "es_uy"=>"Spanish", "fa_ir"=>"Persian/Iran", "es_ve"=>"Spanish");
#example usage
$locale = strtok(locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']), ',');#first preferred language
if(is_null($locale)){
echo "undefined language";
}else{
echo $langs_accept_language[strtolower($locale)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment