laravel-lang/locales
Locale data package for Laravel Lang. Provides up-to-date locale definitions you can use across your Laravel apps, with documentation for installation and contribution guidelines. MIT licensed.
Installation:
composer require laravel-lang/locales
Publish the config (optional):
php artisan vendor:publish --provider="LaravelLang\Locales\LocalesServiceProvider" --tag="locales-config"
First Use Case: Fetch the current locale’s metadata (e.g., for a language selector dropdown):
use LaravelLang\Locales\Facades\Locales;
$currentLocale = Locales::getCurrent(); // Returns LocaleData object
echo $currentLocale->name; // e.g., "English"
echo $currentLocale->native; // e.g., "English" (localized name)
echo $currentLocale->direction; // "ltr" or "rtl"
Key Facade Methods:
Locales::get($locale): Get a LocaleData object for a specific locale (e.g., Locales::get('fr_CA')).Locales::available(): List all supported locales (as LocaleData collection).Locales::installed(): List installed locales (checks app.php config).Locales::info($locale): Detailed metadata (e.g., country, currency, scripts).Blade Integration:
@php
$locale = LaravelLang\Locales\Facades\Locales::get('en_CA');
@endphp
<select>
<option value="{{ $locale->code }}">{{ $locale->name }} ({{ $locale->country->name }})</option>
</select>
Dynamic Locale Selection:
Locales::getCurrent() in middleware to set user-specific locales:
public function handle($request, Closure $next) {
$locale = Locales::get($request->input('locale', app()->getLocale()));
app()->setLocale($locale->code);
return $next($request);
}
Region-Specific Features:
$canadianLocales = Locales::available()
->where('country.code', 'CA')
->values('code'); // ['en_CA', 'fr_CA']
Form Validation:
use LaravelLang\Locales\Facades\Locales;
$validator = Validator::make($request->all(), [
'locale' => ['required', function ($attribute, $value, $fail) {
if (!Locales::available()->contains('code', $value)) {
$fail('Invalid locale.');
}
}],
]);
RTL/LTR Detection:
$direction = Locales::get(app()->getLocale())->direction;
view()->share('isRtl', $direction === 'rtl');
Multi-Tenant Localization:
tenants table and fetch dynamically:
$tenantLocale = Locales::get($tenant->locale);
$tenantCurrency = $tenantLocale->currency->code;
Laravel Validation:
Extend Illuminate\Validation\Rules\In to validate against installed locales:
use LaravelLang\Locales\Facades\Locales;
$rule = new In(Locales::installed()->pluck('code')->toArray());
Blade Directives: Create a custom directive for locale-aware content:
Blade::directive('locale', function ($locale) {
$localeData = Locales::get($locale);
return "<?php echo \$localeData->name; ?>";
});
Usage:
@locale('fr_CA') <!-- Outputs "French (Canada)" -->
API Responses: Attach locale metadata to API responses:
return response()->json([
'data' => $data,
'locale' => Locales::get(app()->getLocale())->toArray(),
]);
Testing: Mock locales in tests:
Locales::shouldReceive('get')
->with('en_CA')
->andReturn(new LocaleData(['code' => 'en_CA', 'name' => 'Test Locale']));
Configuration:
Override defaults in config/locales.php:
'with_countries' => true,
'with_currencies' => true,
'with_protects' => false, // Disable protected locales (e.g., 'en_US')
Null Locale Handling:
Locales::get():
$locale = $request->input('locale', app()->getLocale());
if (!Locales::available()->contains('code', $locale)) {
abort(400, 'Invalid locale');
}
Locales::get($locale ?? app()->getLocale()) to avoid null errors (see v2.9.2).Performance:
Locales::available() in a service provider:
public function boot() {
$this->app->singleton('locales.available', function () {
return Locales::available();
});
}
with_countries/with_currencies if unused to reduce memory usage.Locale Code Mismatches:
app()->setLocale('en') vs. Locales::get('en_US'):
Locales::get(app()->getLocale()) to resolve the current locale’s full metadata.en → en_US).Protected Locales:
Locales::installed($withProtects = false) excludes protected locales (e.g., en_US) by default.$withProtects = true if you need to include them.Deprecations:
Locales::getDefault() is an alias for Locales::getCurrent() (v2.2.0+).Locale class renamed to LocaleData (v1.4.0+).Locale Not Found:
config/app.php under locales.Locales::available():
dd(Locales::available()->pluck('code'));
Country/Currency Missing:
with_countries/with_currencies are enabled in config.Locales::raw()->get($locale).Direction Issues:
Locales::get('ar') (RTL) and Locales::get('en') (LTR):
dd(Locales::get('ar')->direction); // Should return 'rtl'
Custom Locale Data:
LocaleData for app-specific fields:
class ExtendedLocaleData extends \LaravelLang\Locales\LocaleData {
public function getTimezone() {
return $this->customData['timezone'] ?? 'UTC';
}
}
Locales::macro('get', function ($locale) {
return new ExtendedLocaleData(Locales::raw()->get($locale));
});
Dynamic Locale Loading:
Locales::macro('getFromDb', function ($code) {
$dbLocale = DB::table('locales')->where('code', $code)->first();
return new LocaleData($dbLocale ?? []);
});
Event Listeners:
event(new LocaleChanged($oldLocale, $newLocale));
Testing Utilities:
if (!function_exists('mock_locale')) {
function mock_locale($code, array $data = []) {
Locales::shouldReceive('get')
->with($code)
->andReturn(new LocaleData(array_merge($data, ['code' => $code])));
}
}
use LaravelLang\Locales\Facades\Locales;
$sortedLocales = Locales::available()
->sortBy(function ($locale) {
return Locales::get($locale->
How can I help you explore Laravel packages today?