laravel-lang/native-country-names
Provides native-language country names for Laravel apps. Install via composer and use localized datasets to display countries in their own languages. Maintained by Laravel Lang; MIT licensed.
composer require laravel-lang/native-country-names
php artisan vendor:publish --provider="LaravelLang\CountryNames\CountryNamesServiceProvider"
use LaravelLang\CountryNames\Country;
$countryName = Country::name('US'); // Returns 'United States' (en) or localized equivalent
Country facade for quick integration.// In Blade templates
<select name="country">
@foreach (Country::all() as $code => $name)
<option value="{{ $code }}">{{ $name }}</option>
@endforeach
</select>
Country::all() to generate localized dropdowns.// In controllers/APIs
return response()->json([
'countries' => Country::all($request->locale)
]);
app()->getLocale() or request) to Country::all() or Country::name().Accept-Language header).$name = Country::name('US', 'fr', 'en'); // 'États-Unis' (fr) or 'United States' (en fallback)
// In console commands
$countries = Country::all('es');
foreach ($countries as $code => $name) {
$this->info("{$code}: {$name}");
}
use LaravelLang\CountryNames\Tests\TestCase;
public function testCountryNames()
{
$this->assertEquals('Deutschland', Country::name('DE', 'de'));
$this->assertEquals('Germany', Country::name('DE', 'en'));
}
TestCase for locale-specific assertions.Override the default dataset by publishing the config and updating the data_path:
// config/country-names.php
'data_path' => database_path('countries.json'),
Cache country data for performance:
$countries = Cache::remember('countries_'.$locale, now()->addHours(1), function() use ($locale) {
return Country::all($locale);
});
Combine with other Laravel Lang packages (e.g., laravel-lang/iso-3166) for richer country data:
use LaravelLang\ISO3166\ISO3166;
$country = ISO3166::where('alpha2', 'US')->first();
$name = Country::name($country->alpha2);
Locale Mismatches
Country::name('US') defaults to the app’s locale. If the app locale isn’t set, it may return unexpected results.Country::name('US', 'fr'); // Force French
Data Updates
update:data Artisan command (if available in future versions).Case Sensitivity
US vs. us) are case-insensitive, but some integrations may enforce uppercase.$code = strtoupper($userInput);
Missing Locales
try {
$name = Country::name('XK', 'sq'); // Kosovo (Albanian)
} catch (\InvalidArgumentException $e) {
$name = Country::name('XK', 'en'); // Fallback to English
}
Performance with Large Datasets
Country::all()) may impact performance in memory-constrained environments.$countries = collect(Country::all())->take(50);
dd(Country::getSupportedLocales());
dd(Country::getData('es'));
US, not USA).Add Custom Countries Extend the dataset by publishing the config and merging custom data:
// config/country-names.php
'custom_data' => [
'XX' => [
'en' => 'Custom Land',
'es' => 'Tierra Personalizada',
],
],
Override Facade Methods
Bind a custom class to the Country facade in AppServiceProvider:
use LaravelLang\CountryNames\Facades\Country;
Country::swap(new \App\Services\CustomCountryService());
Localization Hooks
Use Laravel’s locale.booted event to dynamically set country names:
Event::listen('locale.booted', function ($locale) {
app('translator')->setLocale($locale);
// Custom logic for locale-specific country names
});
Testing Edge Cases Write tests for:
config/app.php). Override it per request if needed:
app()->setLocale('fr');
vendor/laravel-lang/native-country-names/data/. Avoid modifying this directory directly; use the config override instead.<x-jet-label for="country" value="{{ __('Country') }}" />
<x-jet-input id="country" type="text" list="countries" />
<datalist id="countries">
@foreach (Country::all() as $code => $name)
<option value="{{ $name }} ({{ $code }})">
@endforeach
</datalist>
Route::get('/api/countries', function (Request $request) {
return Country::all($request->locale);
});
LocaleMiddleware to test multiple locales in a single request:
$response = $this->withHeaders(['Accept-Language' => 'fr'])
->get('/countries');
How can I help you explore Laravel packages today?