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

Ligonsker's avatar

How can I access nested translation values in localization file?

I have a translation file called translations.php:

return [
    'key1' => 'value1',
    'key2' => 'value2',
]

But what if I want to organize it by "subjects":

return [
    'subject1' => ['key1' => 'value1',  'key2' =>'value2'],
    'subject2' => ['key3' => 'value3',  'key4' =>'value4']
]

Is it possible? Or for that I should use the translation strings approach, something like:

// translations.json
{
    "subject1.key1": "value1",
	"subject1.key2": "value2",
	"subject2.key3": "value3",
	"subject2.key4": "value4",
}

Thanks

0 likes
5 replies
Ligonsker's avatar

@Sinnbeck Thank you, apparently I had an error from another piece of code. Now I am able to access the translation like so:

trans('translations.subject1.key1');

Is it the correct way or there's a better way?

Sinnbeck's avatar

@Ligonsker Personally I split them out into several files. Like subject1.php. This makes it faster for me to find the file that holds all translations for a specific part of the app

1 like
Ligonsker's avatar

@Sinnbeck Yes I also have separate files, but this case it's just the translation for all the table columns in the DB to human readble columns and in another language (I couldn't think of a better way to translate table columns)

So in this case there are many tables so I thought to refer to this entire file as "table column translations" and inside it will contain the individual tables as the subjects

Please or to participate in this conversation.