When using Laravel's i18n feature, I need to create a file for each locale and for each translation 'group'. For example, a messages.php file would return an array of generic messages, and a validation.php would return an array of validation messages, and both those files would exist for each locale I have set. This means I have a lang/en/messages.php
as well as a lang/it/messages.php
, each with the correct translated string for each specified key.
Is it possible to have a "generic" translation file that doesn't require to specify a group when calling Lang::get()
? What I would like to do is to be able to call Lang::get("MyString")
instead of Lang::get("group.MyString")
.
In my case I need to modify Illuminate/Foundation/helpers.php
The solution is to take a domain by default (in this case, messages.php) and then add to the phrase ($id) the domain if there is not dot.
function trans($id = null, $parameters = [], $domain = 'messages', $locale = null)
{
if (is_null($id)) {
return app('translator');
}
if (strpos($id, '.') === false) {
$id = "messages." . $id;
}
return app('translator')->trans($id, $parameters, $domain, $locale);
}