Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dj75's avatar
Level 1

Fallback_locale and .json translation files, doesn't work like .php files

Laravel version 8.6.4 Php 8.0.10

Have an app that I've just switched from using resources/lang/en/messages.php type files to using a single resources/lang/en.json file.

When I use .json files for translations the fallback_locale set in app.php is not being respected, I'm getting the key of the strings instead of the fallback locale translation string.

Using pseudocode to describe the issue....

lang/en/messages.php
	'hello' => 'hi there!'
setLocale('ru')
__('messages.hello') >>> returns 'hi there!'

This is what you'd expect, the fallback translation because no lang/ru/messages.php exists.

lang/en.json
	"messages.hello" : "hi there!"
setLocale('ru')
__('messages.hello') >>> returns 'messages.hello'  

You would expect 'hi there!' as this is the fallback translation language.

ANyone have any ideas? Is this a bug?

0 likes
2 replies
dj75's avatar
Level 1

Okay, so I get it now. I think.

It works fine if you define your main (default) language translation in .php files. Then only use .json files for the other languages, the fallback to .php works. You just have to remember to define the json differently to the php, for example:

validation.php
	'multiple_of' => 'The :attribute must be a multiple of :value.',

ru.json
	"validation.multiple_of" : "Russian transaltion here",
ChrLu's avatar

I was running into the same problem using Laravel 9.17 and wondered why it doesn't work with .json files? The language files are fine, because when setting the locale to 'en' the translation from en.json gets displayed. But when setting the locale to 'de' and fallback_locale to 'en' and the translation key 'my.key' does not exist in de.json, the output will be 'my.key'.

When you need this feature, please leave a comment in: https://github.com/laravel/framework/issues/41565

Please or to participate in this conversation.