How to localize a Laravel application

Written by Paweł Mysior on 10.07.2018

Localizing a fresh installation of a Laravel application is a simple task.

All you need to do is copy and translate values from the four files in resources/lang/en directory to the directory with your language. Then you would need to translate the texts in the default auth scaffolding views and the reset password notification texts. (If you're interested in a detailed step by step instruction, read below.)

Doing all of that, especially if you're spinning a new Laravel application that needs localization every week can be cumbersome.

Introducing laravel-localize

That's why I've created a package that installs a global laravel-localize command that will install all the language files for you automatically. Install it by running:

composer global require pawelmysior/laravel-localize

then cd into your Laravel application and run:

laravel-localize pl

That's it. All of the language files have been downloaded from the awesome caouecs/Laravel-lang repository. You can find the list of available languages here.

The only thing that you need to do is change the value of the locale key in config/app.php.

I hope that this simple command will help you save some time! Go check it out at https://github.com/pawelmysior/laravel-localize.

Manual step by step localization

If you wanted to translate your application to, for example the beautiful Polish language, you would have to start by changing the locale key in config/app.php to pl. Then you would create a pl directory in resources/lang and copy to it and translate the values in the following files from the resources/lang/en directory:

  • auth.php
  • pagination.php
  • passwords.php
  • validation.php

If your application is not using the authentication system that comes with Laravel, you're done. Good job!

If you run php artisan make:auth however, read on.

You will also need to translate the default auth scaffolding views and ResetPasswordNotification. But, if you're using a recent version of Laravel, you're in luck. Since Laravel 5.6.8 all the strings in the views that come from the default auth scaffolding use the __() helper function. Here's an example from the resources/views/auth/register.blade.php file:

<button type="submit" class="btn btn-primary">
    {{ __('Register') }}
</button>

This lets you define, for example a resources/lang/pl.json file:

{
  "Register": "Zarejestruj się"
}

And the text in the button will be translated.

Since Laravel 5.6.25 the default ResetPasswordNotification that sends the password reset link to the user also uses translation from the json file. Here's a fragment from the Illuminate\Auth\Notifications\ResetPassword class:

->line(Lang::getFromJson('You are receiving this email because we received a password reset request for your account.'))

The Lang facade and its getFromJson methods are used here. To keep going with the previous example, to translate this line in the password reset mail, you would need to add it to the resources/lang/pl.json file:

{
  "Register": "Zarejestruj się",
  "You are receiving this email because we received a password reset request for your account.": "Otrzymujesz ten e-mail, ponieważ otrzymaliśmy prośbę o zresetowanie hasła dla Twojego konta."
}

Here's a list of all the keys that you'll need to fully translate the default auth views and password reset mail:

{
  "Confirm Password": "",
  "E-Mail Address": "",
  "Forgot Your Password?": "",
  "If you did not request a password reset, no further action is required.": "",
  "Login": "",
  "Logout": "",
  "Name": "",
  "Password": "",
  "Register": "",
  "Remember Me": "",
  "Reset Password": "",
  "Reset Password Notification": "",
  "Send Password Reset Link": "",
  "You are receiving this email because we received a password reset request for your account.": ""
}
Copyright © Paweł Mysior, contact me via email, follow me on twitter

My website uses cookies. If you do not wish to accept cookies, please change your settings in the browser or leave the website. By using the website without changing the settings for cookies in your browser, you state that you accept cookies.