Convert translate message calls to a single json language file

David Carr

Packages Laravel Framework Tutorials

Laravel supports using the __() call to setup locale for your pages ie in a blade file you may have a title called Dashboard. To make it translatable you would use:

{{ __('Dashboard') }}

This will default to the given text if the config/app.php file's locale is set to en, if its set to another language Laravel will look for a the lang.json file in the resourses folder, if one is not found then the given message will be used. 

You have to manually create the language files yourself which can be time consuming.

This package: https://github.com/vemcogroup/laravel-translation provides the ability to automate the lang file process!

Install it with composer:

composer require vemcogroup/laravel-translation

Publish the config:

php artisan vendor:publish --provider="Vemcogroup\Translation\TranslationServiceProvider"

This generated a config file to config/translation I'll stick with the defaults for this post.

Now all you need to do is run:

php artisan translation:scan

This will scan your .php and .vue files (you can add others in the config) looking for matching functions the defaut is to look for __() functions. All matches are added to resources/lang/en.json

This is very useful as can write your text in english using the __() syntax and generate a single language file for any translations needed, perfect!

Fathom Analytics $10 discount on your first invoice using this link

David Carr - Laravel Developer

Hi, I’m David Carr

A Senior Developer at Vivedia
I love to use the TALL stack (Tailwind CSS, Alpine.js, Laravel, and Laravel Livewire)

I enjoy writing tutorials and working on Open Source packages.

I also write books. I'm writing a new book Laravel Testing Cookbook, This book focuses on testing coving both PestPHP and PHPUnit.

Sponsor me on GitHub

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Laravel Testing Cookbook

Help support the blog so that I can continue creating new content!

Fathom Analytics $10 discount on your first invoice using this link

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2006 - 2023 DC Blog. All code MIT license. All rights reserved.