Dynamic languages using spatie's package

I am saving a list of languages per entry in my articles table, so for example, $article->languages can be ['es','de'] and sometimes it can be ['fr','nl'].. etc. Now, i worked with spatie/laravel-translatable in the past with no problem at all, when getTranslatableLocales() has a fixed set of languages, but when trying to populate the locale switcher dynamically by $article->languages - it fallbacks to the default config once language is changed. Here's how im trying to implement it at the moment on ArticleResource.php -
public static function getTranslatableLocales(): array
{
$recordId = request()->route('record');
$defaultLanguages = config('languages');
if ($recordId) {
$article = Article::find($recordId);
if ($article && !empty($article->languages)) {
return $article->languages;
}
}
return $defaultLanguages;
}
public static function getTranslatableLocales(): array
{
$recordId = request()->route('record');
$defaultLanguages = config('languages');
if ($recordId) {
$article = Article::find($recordId);
if ($article && !empty($article->languages)) {
return $article->languages;
}
}
return $defaultLanguages;
}
I noticed that once i change the language in the locale switcher (a simple Actions\LocaleSwitcher::make() on getHeaderActions()) - the $recordId is emptied, hence it retrieves the $defaultLnaguages instead. I guess there's a more way to access the current record instead of request()->route('record')
1 Reply
prowler
prowlerOP7mo ago
* On the initial load - it populates the dynamic languages correctly. The problem is only happening once i select a different language. i was able to patch it like this -
if($request->input('components.0.snapshot') !== null)
$recordId = json_decode($request->input('components.0.snapshot'))->data->data[0]->id;
else
$recordId = request()->route('record');
if($request->input('components.0.snapshot') !== null)
$recordId = json_decode($request->input('components.0.snapshot'))->data->data[0]->id;
else
$recordId = request()->route('record');
not very elegant, but it works..
Want results from more Discord servers?
Add your server