laravel-lang/lang
Community-maintained localization files for Laravel. Install via Composer to add and update translations for Laravel’s core messages across many locales, with curated language packs and ongoing updates from the Laravel Lang project.
Installation
composer require laravel-lang/lang
Publish translations (if needed):
php artisan vendor:publish --provider="LaravelLang\LangProvider" --tag=lang
Configure config/app.php
Add the provider to the providers array:
LaravelLang\LangProvider::class,
Set Default Locale
Update config/app.php:
'locale' => 'es', // Example: Spanish
Verify Language Availability
Check resources/lang/ for newly added language files (e.g., es.json, fr.php).
Replace hardcoded strings in Blade views with translations:
{{ __('messages.welcome') }} <!-- Uses es.json if locale is 'es' -->
Or in PHP:
__('validation.required', ['attribute' => 'Name']);
Multi-Language Support
public function handle($request, Closure $next)
{
app()->setLocale($request->header('Accept-Language') ?? config('app.locale'));
return $next($request);
}
Lang::get() for runtime locale retrieval:
$locale = app()->getLocale();
Validation Messages
Leverage built-in validation translations (e.g., validation.required):
$validator = Validator::make($data, $rules);
$validator->setCustomMessages([
'required' => __('custom.required', ['attribute' => 'Email']),
]);
Jetstream/Fortify Localization
Override auth/validation strings in resources/lang/{locale}/auth.php or validation.php.
Dynamic Language Switching Add a language selector in Blade:
<select name="language">
@foreach (config('lang.supported') as $lang)
<option value="{{ $lang }}" {{ app()->getLocale() === $lang ? 'selected' : '' }}>
{{ __('languages.' . $lang) }}
</option>
@endforeach
</select>
Custom Language Files
Extend existing translations by copying files from vendor/laravel-lang/lang/resources/lang/{locale} to resources/lang/{locale}.
Fallback Mechanism
Configure fallback locales in config/app.php:
'fallback_locale' => 'en',
Testing Mock translations in tests:
$this->app->setLocale('es');
$this->withoutExceptionHandling();
Missing Translations
sr_Latn_ME, zh_CN) have gaps. Check status.md for specifics.resources/lang/{locale}/messages.php.Locale-Specific Files
de_AT.php (German-Austria) may conflict with de.php. Ensure correct naming conventions.Caching Issues
php artisan view:clear
php artisan config:clear
Jetstream/Fortify Overrides
resources/lang/{locale}/auth.php may not auto-update. Re-publish translations if needed:
php artisan vendor:publish --tag=lang --force
dd(app()->getLocale());
php artisan lang:list (if available) or manually verify resources/lang/{locale}.Add New Languages
Custom Translation Logic
Extend the LangServiceProvider:
public function register()
{
$this->app->singleton('translator', function ($app) {
$loader = new ArrayLoader();
$loader->loadPaths([...]);
return new Translator($loader, $app['locale']);
});
}
Dynamic Language Loading Load translations on-demand:
$translator->load('custom', $pathToJson);
trans() for Context
Pass arrays for dynamic replacements:
trans('messages.greet', ['name' => 'John']);
route('home', [], false, app()->getLocale());
php artisan lang:compile
How can I help you explore Laravel packages today?