Frontend lang switcher
# ❓questions
e
I translated my platform, but how to handle the frontend language switcher?
i
e
Ahaaaa thanks! I did not get to the particular frontend part it seems haha!
Thanks
I have one small question regarding this.. I created a singleton for my menu structure and I call it globally via my boot function in my AppserviceProvider, but it does not translate the menu items if for instance I choose my language to be Dutch.. It is still in English..
i
The boot function is called before the middleware responsible for setting the locale
e
Hm.. Then how do I set my navigation globally best?
e
Thanks, I call the menu items through a browser frm my pages model btw..
Hm, it still does not translate..
The Navigation singleton has a browser field where I can select the pages but I need the translated title of those pages..
The slug of the URL does translate
i
if the locale is set at the time of execution of the code retrieving that singleton browsers data, it would do it correctly, so you still have the same problem, which is not really a twill concern, more so about how Laravel middleware and request lifecycle works
If you access that data directly from your menu view, is it correct?
e
It is weird because it does translate the slug correctly... Just not the title..
registratie - Registration Before the - is the slug and after is the title
So it does translate correctl
y
It is the same if I call it in my controller of in my provider, it does not seem to be able to access the translations...
In other places it does not seem to get the translated version of my fields either in the frontend..
i
that means you haven't setup the middleware correctly or are querying the models in a way that isn't loading translations correctly. Does it work if you manually set the app locale before retrieving the model
e
Hm so weird.. On the page, the $page->title does not translate either.. Do not know why haha.. It works perfectly in the backend
It does show the correctly too
The title is also inside my $translatedAttributes object
It still only shows the English version
The translating works perfectly for the translatable content of my blocks btw
If I try to get database fields such as title, I cannot seem to get the translation..
@ifox
With the blocks it works perfectly..
i
I need to know exactly how you are retrieving the record and where in the codebase to help @elkex
e
Controller to retrieve the page content:
Copy code
public function index($slug){
        $page = $this->repository->forNestedSlug($slug);
        abort_unless($page, 404, 'page');
        $this->setMetadata($page);
        return view('pages.index', compact('page'));
    }
The model has the HasTranslation trait The repository has the HandleTranslations trait In frontend blade:
Copy code
$page->title
But this only returns the English version.. The translations work correctly in the backend CMS, so I think the configuration should be ok..
i
ok, and how is your routing configured for this controller action?
if you do app()->setLocale('en') or whatever other locale you're using before calling forNestedSlug(), do you get the correct title?
e
Tried that, unfortunately not
It does translate the blocks correctly though, that is the weirdest part, however these are not DB fields ofc..
Or not directly model fields
i
when you land on that controller is the app locale correctly set according to the url you're trying?
e
Yes, the switches correctly
Wit the translation package the slugs translate correctly, but the title, description etc; don't...
i
I mean, that's not expected. If you dd the $page in your controller, which translations relationship do you see loaded? None, 1, multiple?
e
Multiple

https://cdn.discordapp.com/attachments/1092457843690590228/1093135083147366410/Screenshot_2023-04-05_at_13.28.26.png

It always shows 2 relations: the English one & the selected language one
i
That means you have enabled translatable.use_fallback and use_property_fallback to be true, correct?
e
Yes
In both Twill config and translatable
i
now, when you're doing ->title, the package is using the loaded translations according to your app locale
so I don't get what's wrong really
e
Yes, that's what I would expect to happen too haha
THat's why I'm so confused
i
do you have the issue with a non nested model too?
e
Good question.. I don't think I have a non nested model at the moment, I'll create one
i
what are your twill.locale, app.locale, as well as fallback locales?
e
app.locale:
Copy code
'locale' => 'en',
'fallback_locale' => 'nl',
twill.locale:
Copy code
'locale' => 'en',
    'fallback_locale' => 'nl',
If I change the locale in app.translatable, the copy does change to NL so......
Could it have something to do with this..? In other projects it shows how many have been published, but in this project it doesn't for some reason..

https://cdn.discordapp.com/attachments/1092457843690590228/1093148984849211412/Screenshot_2023-04-05_at_14.23.25.png

Nvm, that's fixed hm..
Ait I fixed it.....
In the translatable the locale had to be set to null.. Wauw......
It all works perfectly now, the only thing that does not yet work the way it should, is that if you're on a page and you want to translate it to another language, the slug of that page in the other language is not correct..
This issue is fixed!