laravel-lang/config
Laravel Lang: Config provides configuration resources for the Laravel Lang ecosystem, making it easier to integrate and manage language-related settings in Laravel apps. Maintained by the Laravel Lang community and distributed via Composer under MIT.
Installation:
composer require laravel-lang/config
Publish the config file:
php artisan vendor:publish --provider="LaravelLang\Config\LaravelLangConfigServiceProvider" --tag="config"
This generates config/lang.php in your project.
First Use Case:
Configure default locale and fallback locales in config/lang.php:
return [
'default_locale' => 'en',
'fallback_locale' => 'en',
'supported_locales' => ['en', 'es', 'fr'],
'paths' => [
resource_path('lang'),
],
];
Now, Laravel-Lang packages (e.g., laravel-lang/lang) will automatically use these settings.
config/lang.php – Centralized configuration for all Laravel-Lang packages.LaravelLang\Config\LaravelLangConfigServiceProvider – Registers and bootstraps the package.LaravelLangConfig – Access helper methods (e.g., LaravelLangConfig::initialize()).Centralized Configuration:
Use config/lang.php to define:
['en', 'es', 'fr']).loader => 'json').Example:
return [
'default_locale' => env('APP_LOCALE', 'en'),
'fallback_locale' => 'en',
'supported_locales' => ['en', 'es', 'fr', 'de_AT'],
'paths' => [
resource_path('lang'),
base_path('custom/translations'),
],
'loader' => 'json', // or 'php', 'yaml'
];
Environment-Specific Overrides:
Override settings per environment using .env or conditional logic in config/lang.php:
'default_locale' => env('APP_LOCALE', config('app.locale')),
Integration with Laravel-Lang Packages: The package works seamlessly with other Laravel-Lang tools like:
laravel-lang/lang: Uses config/lang.php for locale paths and loaders.laravel-lang/routes: Configure route localization metadata (e.g., meta, directory).laravel-lang/publisher: Publishes translation files using settings from config/lang.php.Example for routes:
'routes' => [
'meta' => [
'prefix' => 'api',
'middleware' => ['api'],
],
'directory' => 'api',
],
Dynamic Initialization:
Use the LaravelLangConfig facade to initialize or reset configurations dynamically:
use LaravelLang\Config\Facades\LaravelLangConfig;
LaravelLangConfig::initialize(); // Re-initialize with current config
Publishing Custom Configs: Extend or override the default config by publishing additional files:
php artisan vendor:publish --provider="LaravelLang\Config\LaravelLangConfigServiceProvider" --tag="config"
Then modify the published config/lang.php to suit your needs.
New Project Setup:
config/lang.php.laravel-lang/lang) to automatically inherit settings.Multi-Language Project:
config/lang.php.resources/lang/es/).App facade or middleware to set the locale dynamically:
use Illuminate\Support\Facades\App;
App::setLocale('es');
Route Localization:
config/lang.php under the routes key.LaravelLang\Routes package to generate localized routes:
Route::lang('es')->get('/saludo', function () {
return 'Hola';
});
CI/CD Pipeline:
config/lang.php in version control.APP_LOCALE) to override settings per environment.Combine with Laravel-Lang/Lang:
The package works out-of-the-box with laravel-lang/lang. Ensure your translation files are in the correct paths (e.g., resources/lang/{locale}/).
Custom Loaders:
If you need a custom loader (e.g., for .po files), extend the package or use Laravel’s built-in loader system alongside this package.
Middleware for Locale Switching: Create middleware to switch locales based on user preferences or headers:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\App;
class SetLocale
{
public function handle($request, Closure $next)
{
if ($locale = $request->header('Accept-Language')) {
App::setLocale($locale);
}
return $next($request);
}
}
Testing:
Override the config in tests using config() or Mockery:
$this->app->singleton('config', function ($app) {
return new \Illuminate\Config\Repository([
'lang' => [
'default_locale' => 'test',
'fallback_locale' => 'en',
],
]);
});
Localization in Blade:
Use the @lang directive or trans() helper:
<h1>@lang('welcome.title')</h1>
Or:
echo trans('welcome.title');
Unsupported Laravel Versions:
Config File Overrides:
--force or manually merge changes:
php artisan vendor:publish --provider="LaravelLang\Config\LaravelLangConfigServiceProvider" --tag="config" --force
Locale Paths:
paths array in config/lang.php points to valid directories. Invalid paths will cause errors when loading translations.'paths' => [
resource_path('lang'),
base_path('custom/translations'),
],
Fallback Locale Logic:
fallback_locale setting must be one of the supported_locales. Otherwise, fallback logic may fail silently or throw errors.'fallback_locale' => 'en', // Must be in 'supported_locales'
'supported_locales' => ['en', 'es', 'fr'],
Loader Conflicts:
json and php), ensure translation files are consistent in format. Mismatches may cause parsing errors.'loader' => 'json', // or 'php', 'yaml'
Route Configuration:
meta and directory keys for routes were renamed from map in v2.2.0. Update your config if you’re upgrading:
// Old (v1.x)
'map' => [...]
// New (v2.x)
'meta' => [...]
IDE Helper Files:
vendor/laravel-lang/config/helpers. If these cause issues, disable them by setting VENDOR_PATH in your .env:
VENDOR_PATH=
Missing Translation Files:
config/lang.php.chmod -R 755 resources/lang).Locale Not Switching:
App::setLocale()).es vs. ES).How can I help you explore Laravel packages today?