dimsav / laravel-translatable

[Deprecated] A Laravel package for multilingual models
MIT License
1.95k stars 320 forks source link

How to get a list of items according to this logic #460

Closed Chalkin closed 6 years ago

Chalkin commented 6 years ago

Hey Guys,

I'm trying to figure out on how to get all Posts as the following:

Assumption: Every blog post does have a translation in the default language of en.

Posts: Post 1 (Has en, de, es) Post 2 (Has en, de) Post 3 (Has en) Post 4 (Has en, de)

  1. When the app locale is set to 'en' it lists all posts with language en (But it should not list any duplicates. A blog post should only be listed one time, not more often)

  2. When the app locale is set to 'de' it lists all the posts with translations to german AND if there is no german translation it falls back to en. (But it should not list any duplicates. A blog post should only be listed one time, not more often)

Example if app language is set to 'en': Post 1 in en Post 2 in en Post 3 in en Post 4 in en

Example if app language is set to 'de': Post 1 in de Post 2 in de Post 3 in en Post 4 in de

If app language is set to 'es' it would list: Post 1 in es Post 2 in en Post 3 in en Post 3 in en

Gummibeer commented 6 years ago

Just use the fallback locale and do Post::all() and loop.

Gummibeer commented 6 years ago

https://github.com/dimsav/laravel-translatable/blob/master/readme.md#fallback-locales

Chalkin commented 6 years ago

Awesome! Thanks. My fake data from my seeder got me confused that's why I thought it did not work.

Gummibeer commented 6 years ago

@dimsav this issue could be closed.