1

The problem is that I want a title with 3 languages (English, Italian, German). The user can add 3 titles on a form for each language which only the Italian is required. After that the titles are saved in db like this using spatie / laravel-translatable:

{"it":"Titolo in italiano.","en":"English title."}

The idea is that user can edit this titles but there is a problem. On the form when trying to retrieve titles for English, Italian, German, when one of the titles for this languages is missing and I try to show it using:

{{ $item->getTranslation('title_lang', 'de') }}

It retrieves the default title as per default locale which is 'it' in this case. So in case that Germa title is missing it shows the Italian one.

I wanted to show the input which would contain German title, otherwise to be empty in case of no title in German.

I don't know how can I can check on blade using @if statement or some other logic. I tried desperately this but without success(no sense but I give it a try):

@if(isset($product->getTranslation('description_locale', 'de')))
    {{ $product->getTranslation('description_locale', 'de') }}
@endif

I looked at the docs but did not found something which can help me.

Has anyone any idea how to achieve this?

Thank you!

Marinario Agalliu
  • 989
  • 10
  • 25
  • 1
    can you try to `$useFallbackLocale=false` param for not getting default locale string. https://github.com/spatie/laravel-translatable/blob/master/src/HasTranslations.php#L49 As: `$product->getTranslation('description_locale', 'de', false)` – Enver Arslan Dec 11 '20 at 14:17
  • 1
    Bro you're right! Thank you so much! I was blocked and nothing come to my mind. You can write this as an answer and I can accept it! – Marinario Agalliu Dec 11 '20 at 14:20
  • 1
    fyi: `@if(isset({{ $product->getTranslation('description_locale', 'de') }}))` should not contain `{{` and `}}` since you already are using Blade – brombeer Dec 11 '20 at 14:21
  • @brombeer yeah that is so stupid, I was just confusing and did that, I know that `@if` should not conatin `{{}}` but I was just confused. Thanks anyway. I tried this without `{{}}` also and did not worked. Im editing the question but I already got the answer. – Marinario Agalliu Dec 11 '20 at 14:23
  • That might be because of the `isset()`, `@if($product->getTranslation('description_locale', 'de'))` should work, not tested though – brombeer Dec 11 '20 at 14:27

1 Answers1

4

Can you try to $useFallbackLocale=false param for not getting default locale string.

You can check method usage from source code.

As: $product->getTranslation('description_locale', 'de', false)

Enver Arslan
  • 709
  • 3
  • 13