0

In php, how to convert the English country name to a localized version?

For example, if the locale is set to fr, so that JAPAN would become JAPON.

Update: managed to make it work from the country code but still need country to country code without resorting to a handcrafted array.

echo locale_get_display_region('-JP', 'fr'); // Japon
8ctopus
  • 2,617
  • 2
  • 18
  • 25
  • You can take inspiration or inherit from [`this`](https://codex.wordpress.org/I18n_for_WordPress_Developers) link – nice_dev Jul 14 '22 at 12:23
  • I understand how localization works in general, I would rather have an answer to the specific question I've asked. – 8ctopus Jul 14 '22 at 13:11
  • Fair enough. A little off-topic but I find it weird that people change the country spellings in their own language itself. – nice_dev Jul 14 '22 at 13:17
  • I'm getting the country name in English but would like to localize it within the customer's invoice: have the country in the same language as the rest of the invoice. – 8ctopus Jul 14 '22 at 13:21
  • Ideally mappings are created like `['JAPAN' => 'JAPON']`. – nice_dev Jul 14 '22 at 13:24
  • and I would like to avoid doing the monkey job if it's already been done :) – 8ctopus Jul 14 '22 at 13:28
  • I don't think such language conversion exists inbuilt into PHP. The best I can think of right now is to use google translation API. – nice_dev Jul 14 '22 at 13:53

1 Answers1

0

I solved it with the help of an external library:

composer require league/iso3166
require __DIR__ .'/vendor/autoload.php';

$data = (new League\ISO3166\ISO3166())->name('Japan');

echo Locale::getDisplayRegion('-' . $data['alpha2'], 'fr'); // Japon

Here's link to the package: https://github.com/thephpleague/iso3166

8ctopus
  • 2,617
  • 2
  • 18
  • 25