Closed KhurshidYakubov closed 5 years ago
i have some similar issue, i couldn't find any reference on how we can change the language in the frontend. I tested using this :
Route::get('locale/{locale}',function($locale){
App::setLocale($locale);
app()->setLocale($locale);
Session::put('locale',$locale);
echo app()->getLocale();
return redirect()->route('home');
});
Because voyager still using the default value. Does anyone have a good documentation on how achive this? , i think i'm using 1.0. Thanks
Yes, our documentation has it.
Voyager simply uses the locale defined in your config/app.php
file, so you can make your front-end use the same. If you wish to change it by the url like both of your examples use, then just add a middleware or something to get that url parameter (or pull from session) and set the config value (it's in-memory at runtime, so it won't override the file and won't affect other users).
@Sebastiangperez as for your example, I'd just like to point out that your route should likely be a POST, not a GET, since GET requests shouldn't change state.
None of this is specific to Voyager
1. @emptynick I have read your documentation already I am tring to load tranlations(from Controller) like:
$news = News::all();
$news->load('translations');
and in front-end I am calling like(as I shown on my screenshot above ):
@foreach($news as $item)
<h5>{{$item->title}}</h5>
<p>{{$item->body}}</p>
@endforeach
2. @fletch3555 I have made a middleware and getting url.
The strange thing is that local translations which are coming from the folder resources/lang are working fine when change the language
Just changed to this, and everything is working fine. thank you all :)
@foreach($news as $item)
<h5>{{$item->getTranslatedAttribute('title', \App::getLocale(), 'ru')}}</h5>
<p>{{$item->getTranslatedAttribute('body', \App::getLocale(), 'ru')}}</p>
@endforeach
@Sebastiangperez this may help you too
this is very strange because this method checks if there is a locale pases as a second argument , if not uses app()->getLocale(), its on the trait Translatable.php - line 46 - Version 1.0 i will check using what they wrote.
Voyager simply uses the locale defined in your
config/app.php
file, so you can make your front-end use the same. If you wish to change it by the url like both of your examples use, then just add a middleware or something to get that url parameter (or pull from session) and set the config value (it's in-memory at runtime, so it won't override the file and won't affect other users).@Sebastiangperez as for your example, I'd just like to point out that your route should likely be a POST, not a GET, since GET requests shouldn't change state.
None of this is specific to Voyager
I will try using POST but i don't see the difference in this case.
This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.
Version information
Description
I have implemented localization with middleware. And my page easily changing languages. In Admin panel also I am able to add texts for three languages. But when i change the language, it is not translating the texts(it is staying by default in any cases).
Steps To Reproduce
Steps to reproduce the behavior:
Expected behavior
I want to see translated texts which I add from admin panel when I change the language.
Screenshots
this is route:
In the screenshot below, If you pay attention to
@lang('home.news')
it is getting translated texts frombut it can not get another texts from admin panel. How can I fix that ?
this is my view:
Additional context
Add any other context about the problem here.