sokil/php-isocodes
PHP library for ISO code datasets with localized names: countries (ISO 3166-1/2/3), currencies (ISO 4217), languages (ISO 639-3) and scripts (ISO 15924). Supports Gettext or Symfony Translation drivers, with locale configuration.
Install the package (choose based on needs):
composer require sokil/php-isocodes-db-i18n # With database + translations (recommended)
# OR
composer require sokil/php-isocodes-db-only # With database only (no translations)
Basic usage (for countries):
use Sokil\IsoCodes\IsoCodesFactory;
$isoCodes = new IsoCodesFactory();
$country = $isoCodes->getCountries()->getByAlpha2('US');
echo $country->getName(); // "United States of America"
Localization setup (if using translations):
// For Gettext (default)
putenv('LANGUAGE=en_US.UTF-8');
setlocale(LC_ALL, 'en_US.UTF-8');
// OR for Symfony Translation Driver
$driver = new SymfonyTranslationDriver();
$driver->setLocale('fr_FR');
$isoCodes = new IsoCodesFactory(null, $driver);
Display localized country names in a Laravel view:
// In a controller
$countries = collect($isoCodes->getCountries())
->mapWithKeys(fn($country) => [$country->getAlpha2() => $country->getLocalName()]);
return view('countries.index', ['countries' => $countries]);
Country Selection Dropdown
$countries = $isoCodes->getCountries()->getAll();
$options = collect($countries)->pluck('name', 'alpha2');
Subdivision Lookup (e.g., US States)
$subdivisions = $isoCodes->getSubdivisions()->getByCountryAlpha2('US');
$states = $subdivisions->filter(fn($s) => $s->getType() === 'State');
Currency Conversion Helper
$currency = $isoCodes->getCurrencies()->getByLetterCode('EUR');
$formatted = $currency->getLocalName() . ' ' . number_format($amount, 2);
Language Detection Middleware
public function handle($request, Closure $next) {
$lang = $request->header('Accept-Language') ?? 'en';
$driver = new SymfonyTranslationDriver();
$driver->setLocale($lang);
app()->singleton(IsoCodesFactory::class, fn() => new IsoCodesFactory(null, $driver));
return $next($request);
}
Service Provider Binding:
public function register() {
$this->app->singleton(IsoCodesFactory::class, function() {
$driver = new SymfonyTranslationDriver();
$driver->setLocale(config('app.locale'));
return new IsoCodesFactory(null, $driver);
});
}
Eloquent Model Traits:
trait HasCountryCodes {
public function getCountryNameAttribute() {
return $this->country_code
? app(IsoCodesFactory::class)->getCountries()->getByAlpha2($this->country_code)?->getLocalName()
: null;
}
}
API Response Formatting:
return response()->json([
'country' => [
'code' => $country->getAlpha2(),
'name' => $country->getName(),
'local_name' => $country->getLocalName(),
'flag' => $country->getFlag(),
]
]);
Caching Strategy:
$cacheKey = 'iso_countries_' . config('app.locale');
return Cache::remember($cacheKey, now()->addHours(1), function() {
return collect($isoCodes->getCountries());
});
Locale Configuration:
setlocale() before using translations will return empty strings.locale-gen uk_UA.utf8 on Ubuntu).Memory Usage:
$isoCodes->getSubdivisions()->setMemoryOptimization(true);
Flag Handling:
base64_decode($country->getFlag());
Translation Driver Conflicts:
Database Updates:
sokil/php-isocodes (without bundled DB), remember to run:
./bin/update_iso_codes_db.sh all /path/to/storage
Verify Locale:
echo setlocale(LC_ALL, 0); // Check current locale
Check Available Locales:
locale -a
Memory Profiling:
$memoryBefore = memory_get_usage();
$countries = $isoCodes->getCountries();
$memoryAfter = memory_get_usage();
echo "Memory used: " . ($memoryAfter - $memoryBefore) / 1024 / 1024 . "MB";
Common Errors:
Undefined locale → Install missing locale (e.g., sudo apt-get install locales-all).No such file or directory → Verify database path in IsoCodesFactory.Custom Translation Driver:
class LaravelTranslationDriver implements TranslationDriverInterface {
public function translate($message, $locale) {
return trans("iso_codes.$message", [], null, $locale);
}
}
Database Filtering:
$activeCountries = collect($isoCodes->getCountries())
->reject(fn($c) => $c->getWithdrawalDate() !== null);
Hybrid Caching:
$cache = Cache::rememberForever('iso_countries', function() {
return collect($isoCodes->getCountries())
->mapToGroups(fn($country) => [$country->getContinent() => $country]);
});
Event Listeners for Updates:
// In a service provider
$this->app->booted(function() {
if (config('iso_codes.auto_update')) {
Artisan::call('iso:update');
}
});
Publish Config:
php artisan vendor:publish --provider="Sokil\IsoCodes\IsoCodesServiceProvider"
Artisan Command for Updates:
// app/Console/Commands/UpdateIsoCodes.php
public function handle() {
$this->call('vendor:publish', ['--provider' => 'Sokil\IsoCodes\IsoCodesServiceProvider']);
$this->info('ISO codes updated!');
}
Blade Directives:
// In a service provider
Blade::directive('isoCountry', function($code) {
return "<?php echo app('Sokil\\IsoCodes\\IsoCodesFactory')->getCountries()->getByAlpha2($code)?->getLocalName(); ?>";
});
Usage in Blade:
@isoCountry('US') <!-- Outputs localized country name -->
How can I help you explore Laravel packages today?