sokil/php-isocodes-db-i18n
Lightweight PHP library bundling ISO Codes database with i18n support. Look up ISO country, language, script and currency codes and get localized names via simple API. Useful for forms, dropdowns, validation and locale-aware apps.
Installation
composer require sokil/php-isocodes-db-i18n
Add the service provider to config/app.php:
'providers' => [
// ...
Sokil\IsoCodes\IsoCodesServiceProvider::class,
],
Publish Config & Database
php artisan vendor:publish --provider="Sokil\IsoCodes\IsoCodesServiceProvider" --tag="config"
php artisan vendor:publish --provider="Sokil\IsoCodes\IsoCodesServiceProvider" --tag="migrations"
Run migrations:
php artisan migrate
First Query
use Sokil\IsoCodes\Facades\IsoCodes;
// Get country by ISO code (e.g., 'US')
$country = IsoCodes::country('US');
echo $country->name; // "United States"
// Get country by name (English)
$country = IsoCodes::countryByName('United States');
Localization
Ensure your app’s locale is set (e.g., config/app.php):
'locale' => 'en', // or 'fr', 'es', etc.
Fetching Entities
// Countries
$countries = IsoCodes::countries()->limit(10)->get();
$country = IsoCodes::country('GB');
// Subdivisions (e.g., US states)
$states = IsoCodes::subdivisions('US')->get();
$state = IsoCodes::subdivision('US', 'CA'); // California
// Languages
$languages = IsoCodes::languages()->get();
$language = IsoCodes::language('en');
Localized Output Dynamically switch locales in runtime:
app()->setLocale('fr');
$country = IsoCodes::country('FR');
echo $country->name; // "France" (localized)
Integration with Eloquent
Attach ISO codes to models (e.g., User):
use Sokil\IsoCodes\Traits\HasIsoCodes;
class User extends Model
{
use HasIsoCodes;
}
// Usage:
$user = new User();
$user->setCountry('US'); // Stores 'country_code' in DB
$user->country->name; // Localized name
Validation Validate ISO codes in Form Requests:
use Sokil\IsoCodes\Rules\ValidCountryCode;
public function rules()
{
return [
'country_code' => ['required', new ValidCountryCode],
];
}
Caching
Enable caching for performance (add to config/isocodes.php):
'cache' => [
'enabled' => true,
'driver' => 'file', // or 'redis', 'database'
],
Clear cache when data updates:
php artisan cache:clear
Custom Queries Extend the query builder for custom filters:
$countries = IsoCodes::countries()
->where('official_name', 'like', '%United%')
->where('region', 'EU')
->get();
API Integration Fetch remote updates (if supported in future versions):
// Hypothetical: Sync with ISO online database
IsoCodes::syncWithIsoDatabase();
Localization Fallbacks
Configure fallback locales in config/isocodes.php:
'locales' => [
'fallback' => 'en',
'supported' => ['en', 'fr', 'es'],
],
Testing Mock ISO codes in tests:
$this->partialMock(IsoCodes::class, function ($mock) {
$mock->shouldReceive('country')
->with('US')
->andReturn((object) ['name' => 'Mock USA']);
});
Locale Mismatches
app()->setLocale('en'); // Fallback to English
Database Sync Conflicts
'migrations' => [
'run' => false, // Set to false in config/isocodes.php
],
Caching Stale Data
cache:forget strategy:
Cache::forget('isocodes_countries');
Case Sensitivity
'us' vs 'US').$country = IsoCodes::country(strtoupper($userInput));
Missing Subdivisions
try {
$subdivision = IsoCodes::subdivision('TT', '1');
} catch (\Sokil\IsoCodes\Exceptions\NotFoundException $e) {
// Fallback logic
}
Log Queries
Enable query logging in config/isocodes.php:
'debug' => [
'log_queries' => true,
],
Check storage/logs/laravel.log for raw SQL.
Check Data Integrity Verify DB records with:
php artisan tinker
>> Sokil\IsoCodes\Models\Country::count(); // Should match expected records
Validate Config
Ensure config/isocodes.php paths are correct:
'database' => [
'connection' => 'mysql', // Must match your .env
],
Custom Data Sources Override the default data source by binding a custom repository:
$this->app->bind(\Sokil\IsoCodes\Contracts\CountryRepository::class, function () {
return new CustomCountryRepository();
});
Add New Entity Types
Extend the package’s Entity class to support custom ISO entities (e.g., currencies):
class Currency extends \Sokil\IsoCodes\Models\Entity
{
protected $table = 'isocodes_currencies';
}
Localization Hooks Add custom translation logic via service provider:
public function boot()
{
IsoCodes::extend('country', function ($code) {
// Custom logic for country 'XX'
if ($code === 'XX') {
return (object) ['name' => __('custom.xx_name')];
}
});
}
Event Listeners Listen for ISO code updates (e.g., after migration):
event(new \Sokil\IsoCodes\Events\DataSynced());
How can I help you explore Laravel packages today?