Laravel-Lang / common

Easily connect the necessary language packs to the application
https://laravel-lang.com/packages-common.html
MIT License
100 stars 10 forks source link

[Bug]: Missing Country: Canada #205

Open timjeep opened 2 days ago

timjeep commented 2 days ago

Lang Package Name

laravel-lang/native-country-names

Lang Package Version

1.4.0

Laravel Framework Version

11

PHP Version

8.2.13

Dependencies

No response

Issue description

When I run: CountryNames::get('en', SortBy::Value) the country 'Canada' is not in the list.

Steps to reproduce

CountryNames::get('en', SortBy::Value)

andrey-helldar commented 1 day ago

This list is generated from an array of language localization codes, and since English is spoken in Canada, the localization code will be en_CA.

Thus, the Native Country Names list returns country names for localizations. Since the primary localization of en is Unites States, Canada is not displayed in this list.

You can also directly use the Punic library, which contains data from the CLDR service:

use Punic\Data;
use Punic\Territory;

function name(string $locale, string $forLocale): string
{
    return Territory::getName(Data::getTerritory($locale), $forLocale);
}

return name('en_CA', 'en');

This is a feature of the Laravel Lang ecosystem. The list of localization codes can be found here.

But I will plan to extend the functionality to display the list of countries without strictly tying it to localization codes.

Also, the rigid list of localizations is due to the combined data in CLDR, Punic, and machine translators.

timjeep commented 1 day ago

I get that your answers explains why Canada isn't on the list. However, the problem still remains that your function to get a list of country names is not complete. Perhaps I didn't read the documentation. I will find another package to meet my needs. thanks...Tim

andrey-helldar commented 1 day ago

@timjeep You can use Punic:

# install dependency
composer require punic/punic

# load data
vendor/bin/punic-data -l +en_CA

Using:

use Punic\Data;
use Punic\Territory;

$localeCodes = ['en', 'en_CA', 'en_BR', 'fr', 'de_DE'];
$locale      = 'fr';

$items = [];

foreach ($localeCodes as $localeCode) {
    $code = Data::getTerritory($localeCode);

    $items[$code] = [
        'code' => $code,
        'name' => Territory::getName($code, $locale),
    ];
}

return $items;

Result:

array:5 [
  "US" => array:2 [
    "code" => "US"
    "name" => "États-Unis"
  ]
  "CA" => array:2 [
    "code" => "CA"
    "name" => "Canada"
  ]
  "BR" => array:2 [
    "code" => "BR"
    "name" => "Brésil"
  ]
  "FR" => array:2 [
    "code" => "FR"
    "name" => "France"
  ]
  "DE" => array:2 [
    "code" => "DE"
    "name" => "Allemagne"
  ]
]