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

后端 未结 5 1680
日久生厌
日久生厌 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 16:57

    As other have pointed out, there is no built-in function as this likely due to the reality of many countries having multiple languages. So unfortunately, I can't point you to a library that does this, but I did go ahead and write a little function which does what you want.

    There are two caveats, one being if it isn't provided a language it will just pick the first locale in the list. To get around this, you'd have to put some logic around the function call to provide it with the appropriate language. The other is that it needs to have php5-intl installed.

     $locale_language,
                                 'region' => $locale_region);
    
            if (strtoupper($country_code) == $locale_region &&
                $language_code == '')
            {
                return locale_compose($locale_array);
            }
            elseif (strtoupper($country_code) == $locale_region &&
                    strtolower($language_code) == $locale_language)
            {
                return locale_compose($locale_array);
            }
        }
    
        return null;
    }
    ?>
    

提交回复
热议问题