Laravel 5.4: JSON Based Language Files

Published on by

Laravel 5.4: JSON Based Language Files image

One of the most wanted requests we receive at Laravel is introducing better support for multilingual web applications, there are already packages out there that add some nice functionality to Laravel for better handling of multilingual projects requirements, but one of the painful issues when building such applications is managing translation keys.

In previous versions of Laravel, you could insert translated lines using the trans() or trans_choice() helper functions:

trans('auth.verification_number_instructions')

And then you have to include translations for that key in every language your project supports, so for English, you’ll need to have a resources/lang/en/auth.php file that looks like this:

<?php
 
return [
'auth.verification_number_instructions' => 'Please enter your 4-digit verification number:'
];

For small projects the number of translation keys are limited so it’s not that hard to manage it; however, for large projects coming up with translation keys that are easy to understand and remember is a serious pain, and for that reason, Laravel 5.4 is shipped with a new translation helper function:

__("Please enter your 4-digit verification number:")

This new function will look for a resources/lang/en.json file, decode it, and bring the corresponding translation value for the line provided based on the application’s current language. The JSON file looks like this:

{"Please enter your 4-digit verification number:": "men fadlak adkhel raqam al tareef"}

This new feature will allow developers to use plain language lines while writing the application, and defer the need to manage translations to a later stage.

As for why we use JSON files, the decision is based on the fact that JSON is easy to read by humans and also computer software, we believe that having translations stored in JSON opens the door for package developers to create better tools for handling application translations.

Passing parameters to the translator

Using the __() method you’ll be able to pass parameters to the translator just like we used to do in previous versions of laravel:

__(
"Hello :name, you have :unread messages",
['name' => $user->name, 'unread' => $notifications->count]
)

The new thing here is that parameter replacement will happen even if the language line wasn’t found, that means you don’t even have to build a translation file for your applications main language. So in the above example even if there’s no en.json file, the output of the method will be something like:

Hello Mohamed, you have 23 messages.

Translation lines in Blade

In version 5.4, Laravel introduces a new enhancement to the @trans blade directive, you’ll be able to do the following.

@trans(['name' => $user->name, 'unread' => $notifications->count])
Hello :name, you have :unread messages.
@endtrans

We believe that this syntax ensures better readability for long translation lines.

Mohamed Said photo

Web Developer and Laravel Core Contributor

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Forge
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
Bacancy - Staff Augmentation logo

Bacancy - Staff Augmentation

Leave your web app development hustles to the leading IT Staff Augmentation Service Providers. Choose from an extensive pool of 1050+ developers and give yourself the sigh of success you deserve with Bacancy. Get In Touch Today!

Bacancy - Staff Augmentation
DocuWriter.ai logo

DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

DocuWriter.ai
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
October CMS v3.6 Ships Today, Full of New Features image

October CMS v3.6 Ships Today, Full of New Features

Read article
Laracon EU Videos are now out image

Laracon EU Videos are now out

Read article
Use Google's Gemini AI in Laravel image

Use Google's Gemini AI in Laravel

Read article
PhpStorm is getting a brand new terminal image

PhpStorm is getting a brand new terminal

Read article
Six Essential Plugins for Visual Studio Code image

Six Essential Plugins for Visual Studio Code

Read article
Modularize Your Laravel Application With the Modular Package image

Modularize Your Laravel Application With the Modular Package

Read article