Astrotomic / laravel-translatable

A Laravel package for multilingual models
https://docs.astrotomic.info/laravel-translatable/
MIT License
1.25k stars 157 forks source link

What is the proper way of using fallback? #184

Closed pratamatama closed 4 years ago

pratamatama commented 4 years ago

I'm using EducationDegree::translatedIn($request->locale)->get(); to get translation data, but it does not returns the fallback translation.

In this case, $request->locale were set to US which is not available yet at a time. I'm expecting it to return the results translated in EN but instead, I got an empty array.

Gummibeer commented 4 years ago

Hey,

translatedIn() returns all models (EducationDegree) which have a translation in the given locale. https://docs.astrotomic.info/laravel-translatable/package/scopes#translatedin-string-usdlocale-null

So if an EducationDegree doesn't have an English translation it won't be part of the returned collection. If you want to retrieve all EducationDegree models and get the translated attributes in English or your fallback locale you will have to configure your fallback logic like you need it https://docs.astrotomic.info/laravel-translatable/package/fallback-locale and set the $request->locale as app locale app()->setLocale($request->locale) or in the package config config(['translatable.locale' => $request->locale]) otherwise you can change the logic in Locales::current() which is used to retrieve the current locale. https://github.com/Astrotomic/laravel-translatable/blob/f36ddecd16616a35476f38846bf41c9597b56cdb/src/Translatable/Locales.php#L46-L49

pratamatama commented 4 years ago

Clear enough.. Thanks for clarifying out! Closing the issue now.