thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.8k stars 2.67k forks source link

[BUG] Localization problem #4227

Closed KhurshidYakubov closed 5 years ago

KhurshidYakubov commented 5 years ago

Version information

Steps To Reproduce

Steps to reproduce the behavior:

  1. Go to Admin Panel
  2. Add news in three languages
  3. Go to the Landing Page
  4. Change the Language
  5. See error

Expected behavior

I want to see translated texts which I add from admin panel when I change the language.

Screenshots

this is route: image

In the screenshot below, If you pay attention to @lang('home.news') it is getting translated texts from

resources/lang

but it can not get another texts from admin panel. How can I fix that ?

this is my view:

image

Additional context

Add any other context about the problem here.

Sebastiangperez commented 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

emptynick commented 5 years ago

Yes, our documentation has it.

fletch3555 commented 5 years ago

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

KhurshidYakubov commented 5 years ago

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

KhurshidYakubov commented 5 years ago

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
KhurshidYakubov commented 5 years ago

@Sebastiangperez this may help you too

Sebastiangperez commented 5 years ago

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.

Sebastiangperez commented 5 years ago

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.

github-actions[bot] commented 4 years ago

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.