Multilingualism in Laravel 11: How to create a multilingual application in Laravel 11 using i18n and localization?

Updated: Feb 24, 2025

Multilingualism in Laravel 11: How to create a multilingual application in Laravel 11 using i18n and localization?

To create a multilingual application in Laravel 11 using i18n and localization, follow the steps below:

  1. Install Laravel 11: First, make sure you have Laravel installed on your system. If not, follow the official Laravel installation guide to install it.

  2. Create a new language directory: Create a new directory for your language files in the resources/lang directory. For example, if you want to add support for Spanish, create a new directory named es.

  3. Create language files: Inside the new language directory, create new files for each file you want to translate. For example, if you want to translate the welcome message in the messages.php file, create a new file named messages.php inside the es directory.

  4. Add translation keys: In each language file, add the same translation keys as in the original language file. For example, in the messages.php file inside the es directory, add the following code:

    return [
        'welcome' => 'Bienvenido a nuestra aplicaciĆ³n!',
    ];
    
  5. Update config/app.php: In the config/app.php file, add the new language directory to the locales array:

    'locales' => [
        'en' => 'English',
        'es' => 'Spanish',
    ],
    
    'translation' => [
        'files' => [
            base_path('resources/lang'),
        ],
    ],
    
  6. Update app/Providers/AppServiceProvider.php: In the AppServiceProvider.php file, add the following code to the boot() method to set the default language:

    use Illuminate\Support\Facades\App;
    
    public function boot()
    {
        App::setLocale('en'); // Set the default language to English
    }
    
  7. Create routes for language switching: Create routes for language switching in your routes/web.php file:

    Route::get('/change-language/{locale}', 'LanguageController@changeLanguage')->name('changeLanguage');
    
  8. Create LanguageController: Create a new controller named LanguageController and add the following code to it:

    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\App;
    
    class LanguageController extends Controller
    {
        public function changeLanguage(Request $request)
        {
            App::setLocale($request->locale);
            return redirect()->back();
        }
    }
    
  9. Create views for language switching: Create views for language switching in your resources/views directory. For example, create a new file named lang.blade.php and add the following code to it:

    <ul class="nav navbar-nav">
        <li class="nav-item">
            <a class="nav-link {{ App::getLocale() === 'en' ? 'active' : '' }}" href="{{ route('changeLanguage', ['locale' => 'en']) }}">English</a>
        </li>
        <li class="nav-item">
            <a class="nav-link {{ App::getLocale() === 'es' ? 'active' : '' }}" href="{{ route('changeLanguage', ['locale' => 'es']) }}">EspaƱol</a>
        </li>
    </ul>
    
  10. Test your application: Start your Laravel application and test the language switching functionality by visiting the /change-language/es or /change-language/en URLs. Your application should now support multiple languages using i18n and localization.