Drupal 8 - Country name list by code

By kenneth, Tue, 07/16/2019 - 16:17
/** @var array $countries */
$countries = \Drupal::service('country_manager')
  ->getList();
// Transform TranslatableMarkup object into a string.
array_walk($countries, function (&$translation) {
  /** @var  Drupal\Core\StringTranslation\TranslatableMarkup $translation */
  $translation = $translation->render();
});
var_dump($countries);

Drupal 8 ships with a service to easily retrieve all countries by code. Be sure to use dependency injection.