Search code examples
laravellocalizationlaravel-translatable

spatie / laravel-translatable shows the default locale when I try to retrieve the translation that does not exist yet


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!


Solution

  • 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)