Reputation: 767
I am trying to develop a small package in Laravel
, but I have the following problem. From my package I need to get the translations that are present in the languages directory:
Example:
myapp/resources/lang
When I make a call as follows to get the translations of a file:
private function getTranslationsFromFile()
{
return Lang::get('auth');
}
It does not return the translations in that file. It only returns auth
. In the Laravel languages directory there is that file, but inside the directory en
.
So I have defined before getting the translations the default 'locale'
so that I can load them from there in this way:
private function getTranslationsFromFile()
{
app()->setLocale('en');
return Lang::get('auth');
}
this way it works fine, but my question is the following, is there no way to load the translations regardless of whether or not the 'locale'
is set? I mean, is it not possible to get the translations just by giving it a file path?
Example:
private function getTranslationsFromFile()
{
return Lang::get('en/auth');
}
Thank you very much in advance.
Upvotes: 0
Views: 625
Reputation: 5358
You can certainly do that by leveraging the third and fourth parameter of Lang::get()
:
This is the method's signature:
get(string $key, array $replace = [], string|null $locale = null, bool $fallback = true)
The third parameter specifies the locale you'd wish to get the translation string for:
Lang::get('auth.failed', [], 'en')
To set a fallback language in case that the translation is not available in the chosen locale, use the fourth parameter:
Lang::get('auth.failed', [], 'en', 'es')
Upvotes: 2