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

wheeler's avatar

trans() helper is broken?

I spent a bit of time figuring out how to get translation working in Lumen. It seems some effort has gone into burying translation/localisation functionality in the docs over recent versions, even though it is still a cornerstone of validation feedback.

In terms of API design, i've decided I want to use accept-language headers to return localised language responses, so I definitely wanted validation to return usable language without re-implementing validation language on the client side through some kind of funky error code system.

To get translation working in Lumen, firstly, you have to copy vendor/laravel/lumen-framework/resources/lang/en/validation.php over to the local resources/lang/en.

Then you have to set the locale in AppServiceProvider

app('translator')->setLocale('en');

Finally, you have to access translations through the container:

trans('validation.string') // returns "validation.string" 
app('translator')->trans('validation.string') // returns "The :attribute must be a string."

I'm pretty sure the reason why trans() doesn't work is because of the $domain parameter:

// lumen-framework/src/helpers.php
function trans($id = null, $parameters = [], $domain = 'messages', $locale = null)
{
    if (is_null($id)) {
        return app('translator');
    }

    return app('translator')->trans($id, $parameters, $domain, $locale);
}

// Illuminate\Translation\Translator
public function trans($key, array $replace = [], $locale = null)
{
    return $this->get($key, $replace, $locale);
}

Note that the helper passes $domain but the Translator implementation does not have such a parameter, which leads to unexpected results.

Has anyone else come across this? Did I miss something in how to return localised language from a Lumen based API?

0 likes
2 replies
zachleigh's avatar
Level 47

I believe that $domain was removed in 5.4. What version of Lumen are you using?

Please or to participate in this conversation.