See rates for the top Laravel developers in Latin America

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 Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

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

Bacancy
Tinkerwell logo

Tinkerwell

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

Tinkerwell
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
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
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

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
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Laravel 12.44 Adds HTTP Client afterResponse() Callbacks image

Laravel 12.44 Adds HTTP Client afterResponse() Callbacks

Read article
Handle Nested Data Structures in PHP with the Data Block Package image

Handle Nested Data Structures in PHP with the Data Block Package

Read article
Detect and Clean Up Unchanged Vendor Files with Laravel Vendor Cleanup image

Detect and Clean Up Unchanged Vendor Files with Laravel Vendor Cleanup

Read article
Seamless PropelAuth Integration in Laravel with Earhart image

Seamless PropelAuth Integration in Laravel with Earhart

Read article
Laravel API Route image

Laravel API Route

Read article
Laravel News 2025 Recap image

Laravel News 2025 Recap

Read article