Courses

Multi-Language Laravel 11: All You Need to Know

Translation Process: Dev to Translator

Typically, the actual translating process is performed by external non-dev people, right?

But the developer's role here is to prepare all the structure well and to give clear instructions to the translator. In this and a few upcoming lessons, let's discuss how can we perform our part of the process.


Translating JSON Files

In our application we'll most likely have 1 JSON file per language:

lang/en.json

{
"Dashboard": "Dashboard",
"Log in": "Log in",
"Register": "Register",
"You're logged in!": "You're logged in!",
 
// ...
}

So, you send this file to the translator, with these instructions:

  1. Translate the right side of the : to the target language.
  2. Make sure the key on the left side of the : is not changed.
  3. Make sure that the file structure is not changed.

I like this approach for site-wide translations, because it's easy to understand, and it's easy to translate. You instantly see the whole context of the translation.


Translating PHP Files

Our application currently has these files (we've ignored default files):

lang/en/auth.php

return [
'failed' => 'These credentials do not match our records.',
'password' => 'The provided password is incorrect.',
'throttle' => 'Too many login attempts. Please try again in :seconds seconds.',
'register' => 'Registration',
 
// ...
];

lang/en/general.php

return [
'dashboard' => 'Dashboard',
'youAreLoggedIn' => 'You are logged in!',
'cancel' => 'Cancel',
'saved' => 'Saved.',
'save' => 'Save',
'confirm' => 'Confirm',
];

As you can see, there are two separate files right now. Both of them have to be sent to our translator, with these instructions:

  1. Translate the text that's on the right side of the => sign.
  2. Keep the keys on the left side of the => sign the same.
  3. Make sure to keep the same structure of the file.
  4. Add \ before ' sign if it's in the text.

Imagine giving this set of instructions to a person who doesn't know anything about programming. It's not easy, right?

It's even harder if you have a lot of files (for example validation files) and a lot of text to translate.

You also may have an issue where the translator wouldn't understand the meaning or intention of the key, as it's not quite clear what the context is. You might have more specific keys but that doesn't help much.


Package to Translate Core Laravel Files

You might have noticed that we didn't talk about the core Laravel translation files, such as lang/en/validation.php. That's because you don't usually have to translate them yourself.

There's an awesome community-built translation repository: Laravel-Lang/lang

They have a full package that adds some commands to your project:

  1. Install the package: composer require laravel-lang/common --dev
  2. Run php artisan lang:add es to install the language files for es (Spanish).
  3. This will add the files to your project:

That's it! You now have the Spanish language files installed in your project. You can extend this by adding any additional keys that you might have in your project.

You can also copy files manually if you wish:

  1. Find the language you need, for example, es: https://github.com/Laravel-Lang/lang/tree/main/locales/es
  2. Open the json.json file and copy its contents of it.
  3. Create a new file in your project: lang/es.json
  4. Paste the contents of the json.json file into your new file.

That's it. You should be good to go.


Bonus "Trick": Ask AI to Translate Text

Heard of ChatGPT yet?

So yeah, you may not need an external translator. If given a correct set of rules, AI can perform the translation for us!

While it's definitely not perfect, it's a good example of how AI could help you translate text one day!

Notice: Each ChatGPT run gave us a different translation for some text, so you'd have to manually fix them or have someone review them.

avatar

What is a pain in a butt for me personaly is that our business asks for lang changes on a daily basis, because they either made a mistake or decided to change something. This leads to changing the code and running deployment on multiple environments dev ,staging, prd.

Anyone has some kind of a tip for this situation?

avatar

Hey, this sounds like a perfect fit for:

As these would allow you to fully manage your translations in the database. This means that you don't have to do any deployments to your server, just load the cached translations. I have done this multiple times in multiple projects and it's working quite well for exactly your scenario!