Best Approach to Get Country Code by Country Name (in Japanese text)

Viewed 146

I know how to get country codes by using Country Name (english) but my task needed to get the country code using a country name that is translated in Japanese text. Example for Indonesia:

Country : インドネシア Code : ID

Right now, I made an array list of countries (in jp) with the code but it's too manual and needed to add the all other countries possible. I am thinking if there are still other way to do it.

private function getCountryCode($countryName)
    {
        $list = [
            ['value' => 'インドネシア', 'code' => 'ID'],
            ['value' => 'カンボジア', 'code' => 'KH'],
            ['value' => 'ベトナム', 'code' => 'VN'],
            ['value' => 'アフガニスタン', 'code' => 'AF']
        ];
        
        if($countryName){
            foreach($list as $l){
                if($l['value'] == $countryName){
                    return $l['code'];
                }
            }
    
            return '';
        }
        
    }
1 Answers

I just found the answer to my question and this might also be useful for others. I used below code to get the translated list of countries in Japanese

$locale = new \Zend_Locale('ja_JP');
$countries = $locale->getTranslationList('Territory', $locale->getLanguage(), 2);
Related