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
Add the package to your composer.json under require-dev if using for development/testing.
Publish Language Files:
php artisan vendor:publish --provider="LaravelLang\Lang\LangServiceProvider" --tag=lang
This publishes all language files to resources/lang/vendor/laravel-lang.
First Use Case:
__('vendor.lang-key'); // Access translations via the 'vendor' namespace
Language Overrides:
resources/lang/{locale}/auth.php (or any other namespace) and copy the relevant keys from vendor/lang/{locale}/auth.php.// resources/lang/id/auth.php
return [
'failed' => 'Kredensial tidak valid. Silakan coba lagi.',
];
Dynamic Language Switching:
// app/Http/Middleware/SetLocale.php
public function handle($request, Closure $next) {
app()->setLocale($request->header('Accept-Language') ?? config('app.locale'));
return $next($request);
}
app/Http/Kernel.php.Integration with Laravel Breeze/Jetstream:
resources/lang/vendor/laravel-jetstream with the published translations from laravel-lang/lang.Validation Language:
vendor/lang/{locale}/validation.php to resources/lang/{locale}/validation.php and customizing them.Partial Language Loading:
config(['app.fallback_locale' => 'en']) to ensure English fallback if a translation is missing.$translator = app('translator');
$translator->load('id', ['auth' => require resource_path('lang/vendor/laravel-lang/id/auth.php')]);
Custom Language Packs:
LaravelLang\Lang\LangServiceProvider and overriding the boot() method to load additional files.Testing Localizations:
Lang::setLocale() in tests to switch languages:
use LaravelLang\Lang\Facades\Lang;
public function test_localization() {
Lang::setLocale('id');
$this->assertEquals('Kredensial tidak valid.', __('auth.failed'));
}
Missing Translations:
uz_Cyrl missing country names like "Fiji").Namespace Conflicts:
vendor namespace (__('vendor.lang-key')).config(['app.fallback_locales' => ['en', 'vendor']]) to ensure fallback works correctly.Caching Issues:
php artisan view:clear
php artisan config:clear
Jetstream/Breeze Overrides:
laravel-lang translations and ensure your custom files load after the vendor files.Check Loaded Languages:
dd(app('translator')->getLoader()->getLoadedLocales());
Verify your language is loaded.
Fallback Debugging:
config(['app.debug' => true]);
translator facade logs for missing keys.Partial Translations:
validation.encoding), override them in your project’s language files:
// resources/lang/vi/validation.php
'encoding' => 'Trường :attribute phải được mã hóa bằng :encoding.',
Custom Language Files:
LangServiceProvider:
// app/Providers/LangServiceProvider.php
namespace App\Providers;
use LaravelLang\Lang\LangServiceProvider as BaseLangServiceProvider;
class LangServiceProvider extends BaseLangServiceProvider {
protected $additionalLanguages = ['jv' => 'resources/lang/vendor/laravel-lang/jv'];
}
Dynamic Language Loading:
$translator->load('id', ['custom' => ['key' => 'Custom value']]);
Contributing Missing Translations:
Performance Optimization:
Lang::load() sparingly in production. Pre-load languages during bootstrapping:
// app/Providers/AppServiceProvider.php
public function boot() {
Lang::load('id');
Lang::load('vi');
}
How can I help you explore Laravel packages today?