Getting a translated country name from an address field I have an address field from which I'm getting a country code and then, using the address.country_repository service, checking the country's name.

$full_country_list = \Drupal::service('address.country_repository')-&gt;getList();
$country_code = $user-&gt;field_nationality-&gt;first()-&gt;get("country_code")-&gt;getValue();
$result['nationality'] = t($full_country_list[$country_code], [], ['langcode'=&gt;$user-&gt;get('langcode')-&gt;value]);


This repository returns a list with the country names translated in Drupal's default language (in my case, portuguese), but I'm trying to t()-function it to translate the string into other languages.

The t() function doesn't cut it, even if I pass the langcode option. 

How can I get a translated version of the list so that I don't have to translate the string with t()?
The country name in the country list is already a translatable markup object. It's not possible to add new options to it. So your idea to translate the country name again, this time with a specific langcode in options, is right. But you have to get the untranslated string if you want to translate it again

t($full_country_list[$country_code]-&gt;getUntranslatedString(), [], ['langcode' =&gt; 'de'])


because how you do it you get the translated string when running the code in a non english language context and translating from a non english language is not supported.



Edit: this answer is valid for the core country list

$full_country_list = \Drupal::service('country_manager')-&gt;getList();


there are probably other versions in contrib, see comment from @Berdir.