laravel-lang/native-country-names
Laravel package providing country names in their native languages for localized UIs and forms. Part of the Laravel Lang ecosystem, install via Composer and use alongside your app’s localization setup. MIT licensed.
Installation:
composer require laravel-lang/native-country-names
Publish the config (optional):
php artisan vendor:publish --provider="LaravelLang\NativeCountryNames\ServiceProvider"
First Use Case: Display a country name in the current app locale:
use LaravelLang\NativeCountryNames\Facades\Country;
// In a Blade template or controller
$countryName = Country::name('US'); // Returns "United States" (en) or localized equivalent
Where to Look First:
Country (e.g., Country::name(), Country::all()).config/native-country-names.php (custom locales, paths).Locale-Aware Country Names:
// Auto-detects app locale (config/app.php)
Country::name('GB'); // "United Kingdom" (en) or "Royaume-Uni" (fr)
// Force a specific locale
Country::setLocale('de');
Country::name('GB'); // "Vereinigtes Königreich"
Dropdowns/Forms:
<select name="country">
@foreach (Country::all() as $code => $name)
<option value="{{ $code }}">{{ $name }}</option>
@endforeach
</select>
Country::all() if used frequently (e.g., in a global dropdown).API Responses:
return Country::all(); // Returns array like:
// ['US' => 'United States', 'GB' => 'United Kingdom', ...]
Dynamic Localization:
$userLocale = $user->locale ?? app()->getLocale();
Country::setLocale($userLocale);
$countryName = Country::name($request->country_code);
Rule::in() for country code validation:
use LaravelLang\NativeCountryNames\Rules\Country;
$request->validate([
'country' => ['required', new Country],
]);
Blade::directive('country', function ($code) {
return "<?php echo LaravelLang\NativeCountryNames\Facades\Country::name({$code}); ?>";
});
Usage:
<p>Selected: @country('CA')</p>
US) and fetch names dynamically:
$user->country_name = Country::name($user->country_code);
$this->app->shouldReceive('getLocale')->andReturn('es');
$this->assertEquals('España', Country::name('ES'));
Locale Fallback:
config/app.php) is set correctly.Caching:
Country::all() loads the entire dataset. Cache it in a high-traffic app:
$countries = Cache::remember('countries', now()->addHours(1), function () {
return Country::all();
});
Country Code Sensitivity:
US ≠ us). Use strtoupper() if accepting user input:
$code = strtoupper($request->country);
Data Updates:
vendor/laravel-lang/native-country-names/data/ (not recommended for production).config/native-country-names.php for supported locales. Add custom locales by extending the package (see below).Country::all() with tideways/xhprof if slow. Optimize with caching or lazy-loading.'overrides' => [
'CZ' => 'Česko', // Force "Czechia" in supported locales
],
Add Custom Locales:
Country facade or create a decorator:
use LaravelLang\NativeCountryNames\Facades\Country as BaseCountry;
class ExtendedCountry extends BaseCountry {
public static function name($code, $locale = null) {
$locale = $locale ?: app()->getLocale();
$customNames = [
'XK' => ['kos' => 'Kosovë'], // Albanian name for Kosovo
];
return $customNames[$code][$locale] ?? parent::name($code, $locale);
}
}
app()->bind('LaravelLang\NativeCountryNames\Facades\Country', function () {
return new ExtendedCountry();
});
Override Data Source:
config/native-country-names.php:
'data_path' => database_path('country-names.json'),
Add Country-Specific Logic:
Country::name('US'); // "United States"
Country::name('US', 'fr'); // "États-Unis"
Country::name('US', 'custom'); // "Custom Name" (via middleware)
app() helper for locale access:
$locale = app()->getLocale();
Country::setLocale() to test specific locales:
Country::setLocale('ja');
$this->assertEquals('アメリカ合衆国', Country::name('US'));
$name = Country::name($code, $locale);
$name = $name ?? trans('countries.'.$code, [], $locale); // Fallback to custom translations
How can I help you explore Laravel packages today?