Is there a simple way to get the language code from a country code in PHP

后端 未结 5 1708
日久生厌
日久生厌 2020-12-17 16:15

I\'m using ISO 3166-1-alpha 2 codes to pass to an application to retrieve a localised feed e.g. /feeds/us for the USA. I have a switch statement which serves a feed based on

5条回答
  •  渐次进展
    2020-12-17 17:06

    the answer from TheJF is pretty good, however there are a few (general) issues that I came across:

    • his code will return br-FR if you call country_code_to_locale("FR") - now br (Breton) is not even an offical language according to Wikipedia. Although fr-FR is in the list, br-FR is the first in the array. this happens with many other countries too.

    • many other locale lists are trying to be extremly complete and consider all possible languages

    • it is difficult to draw the line here, good examples where you certainly want to keep multiple languages for a country are: Canada and Switzerland

    I went with a simple approach:

    • I kept only 1 language for most countries, and left multiple for some countries like BE, CA, CH, ZA. I kept es-US, but I am not sure about that (Wikipedia says: Official languages: None at federal level)

    • I also kept multiple languages for countries I was too lazy to research or that use both, Latin and Cyrillic

    • I added shuffle($locales); which will randomize the array, such that we get random locales for countries with multiple languages. It made sense for my use case, but you might want to remove that.

    • For my purpose, only languages that have relevant prevalence on the web are of interest. This list is by no means complete or correct, but pragmatic.

    So here is my locale list:

    $locales = array('af-ZA',
                    'am-ET',
                    '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-SY',
                    'ar-TN',
                    'ar-YE',
                    'az-Cyrl-AZ',
                    'az-Latn-AZ',
                    'be-BY',
                    'bg-BG',
                    'bn-BD',
                    'bs-Cyrl-BA',
                    'bs-Latn-BA',
                    'cs-CZ',
                    'da-DK',
                    'de-AT',
                    'de-CH',
                    'de-DE',
                    'de-LI',
                    'de-LU',
                    'dv-MV',
                    'el-GR',
                    'en-AU',
                    'en-BZ',
                    'en-CA',
                    'en-GB',
                    'en-IE',
                    'en-JM',
                    'en-MY',
                    'en-NZ',
                    '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',
                    'fa-IR',
                    'fi-FI',
                    'fil-PH',
                    'fo-FO',
                    'fr-BE',
                    'fr-CA',
                    'fr-CH',
                    'fr-FR',
                    'fr-LU',
                    'fr-MC',
                    'he-IL',
                    'hi-IN',
                    'hr-BA',
                    'hr-HR',
                    'hu-HU',
                    'hy-AM',
                    'id-ID',
                    'ig-NG',
                    'is-IS',
                    'it-CH',
                    'it-IT',
                    'ja-JP',
                    'ka-GE',
                    'kk-KZ',
                    'kl-GL',
                    'km-KH',
                    'ko-KR',
                    'ky-KG',
                    'lb-LU',
                    'lo-LA',
                    'lt-LT',
                    'lv-LV',
                    'mi-NZ',
                    'mk-MK',
                    'mn-MN',
                    'ms-BN',
                    'ms-MY',
                    'mt-MT',
                    'nb-NO',
                    'ne-NP',
                    'nl-BE',
                    'nl-NL',
                    'pl-PL',
                    'prs-AF',
                    'ps-AF',
                    'pt-BR',
                    'pt-PT',
                    'ro-RO',
                    'ru-RU',
                    'rw-RW',
                    'sv-SE',
                    'si-LK',
                    'sk-SK',
                    'sl-SI',
                    'sq-AL',
                    'sr-Cyrl-BA',
                    'sr-Cyrl-CS',
                    'sr-Cyrl-ME',
                    'sr-Cyrl-RS',
                    'sr-Latn-BA',
                    'sr-Latn-CS',
                    'sr-Latn-ME',
                    'sr-Latn-RS',
                    'sw-KE',
                    'tg-Cyrl-TJ',
                    'th-TH',
                    'tk-TM',
                    'tr-TR',
                    'uk-UA',
                    'ur-PK',
                    'uz-Cyrl-UZ',
                    'uz-Latn-UZ',
                    'vi-VN',
                    'wo-SN',
                    'yo-NG',
                    'zh-CN',
                    'zh-HK',
                    'zh-MO',
                    'zh-SG',
                    'zh-TW');
    

    and the code:

    function country_code_to_locale($country_code)
    {
        $locales = ...
    
        // randomize the array, such that we get random locales
        // for countries with multiple languages (CA, CH)
        shuffle($locales);
    
        foreach ($locales as $locale) {
            $locale_region = locale_get_region($locale);
    
            if (strtoupper($country_code) == $locale_region) {
                return $locale;
            }
        }
    
        return "en-US";
    }
    

提交回复
热议问题