Retrieve Country full name from Country Id Using Magento 2 by Country Class of Directory Module.
You need to instantiate CountryFactory in your class __construct() method to get the country full name.
The class responsible to get name is Magento\Directory\Model\Country.
<?php namespace Rbj\Country\Model; use Magento\Directory\Model\Country; use Magento\Directory\Model\CountryFactory; class Country { /** * @var Country */ public $countryFactory; public function __construct( CountryFactory $countryFactory ) { $this->countryFactory = $countryFactory; } /** * country full name * * @return string */ public function getCountryName($countryId): string { $countryName = ''; $country = $this->countryFactory->create()->loadByCode($countryId); if ($country) { $countryName = $country->getName(); } return $countryName; } }
call from template or other class,
$countryId = “IN”; // or IND
echo $countryName = $this->getCountryName($countryId);
Output:
India
For Country Id “US” or “USA” => United States.