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.

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

LaraJobs

The official Laravel job board

LaraJobs
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
Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate logo

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate

Build your SaaS application in hours. Out-of-the-box multi-tenancy and seamless Stripe integration. Supports subscriptions and one-time purchases, allowing you to focus on building and creating without repetitive setup tasks.

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate
Rector logo

Rector

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

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
PHP 8.4 is released with Property Hooks, Class Instantiation without extra parenthesis, and more image

PHP 8.4 is released with Property Hooks, Class Instantiation without extra parenthesis, and more

Read article
Asymmetric Property Visibility in PHP 8.4 image

Asymmetric Property Visibility in PHP 8.4

Read article
Access Laravel Pulse Data as a JSON API image

Access Laravel Pulse Data as a JSON API

Read article
Laravel Forge adds Statamic Integration image

Laravel Forge adds Statamic Integration

Read article
Transform Data into Type-safe DTOs with this PHP Package image

Transform Data into Type-safe DTOs with this PHP Package

Read article
PHPxWorld - The resurgence of PHP meet-ups with Chris Morrell image

PHPxWorld - The resurgence of PHP meet-ups with Chris Morrell

Read article