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
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
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
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 →
Non-backed Enums in Database Queries and a withSchedule() bootstrap method in Laravel 11.1 image

Non-backed Enums in Database Queries and a withSchedule() bootstrap method in Laravel 11.1

Read article
Laravel Pint --bail Flag image

Laravel Pint --bail Flag

Read article
Laravel Herd for Windows is now released! image

Laravel Herd for Windows is now released!

Read article
The Laravel Worldwide Meetup is Today image

The Laravel Worldwide Meetup is Today

Read article
Cache Routes with Cloudflare in Laravel image

Cache Routes with Cloudflare in Laravel

Read article
Learn how to manage timezones in your Laravel Apps image

Learn how to manage timezones in your Laravel Apps

Read article