Replies: 9 comments
-
If I got you, I think you can add this |
Beta Was this translation helpful? Give feedback.
-
@AbdElrahmaN31, I mean that if one of my records is not localized I got empty rather than getting the default value. So if we can have something like |
Beta Was this translation helpful? Give feedback.
-
Chiming in here to agree that there should be some fallback if the key is not translated yet. For example, we have a If we pull This would be very useful so that we don't need to run a script every time we add a new translatable field. |
Beta Was this translation helpful? Give feedback.
-
I agree on this one, would be really handy! |
Beta Was this translation helpful? Give feedback.
-
At first I thought that this option is something new but I just tested it. My test had this entry in DB:
And So, user can switch between English and Dutch and it will load appropriate translated strings. Then as a test I added another locale, Isn't this exactly what you're asking for? |
Beta Was this translation helpful? Give feedback.
-
My use-case would be if we are adding translations to an existing string field.
Would want Once translated, this field looks like:
Both use-cases above should return So not about non-existing locale but about non-existing translations entirely, AKA when the JSON translations are not present it should return the value itself. So if I am trying to read
|
Beta Was this translation helpful? Give feedback.
-
I was having the same issue and as a solution, I have overwritten its existing trait. & create a function where I store fallback lang value same as the current lang. value.
I understand this might not be a perfect way to do this Also It might return the wrong translation but it fixes the issue of returning blank values. |
Beta Was this translation helpful? Give feedback.
-
I found one simple solution to achieve this. I have overwritten trait's
So, the final array of translations is expanded with the default attribute value, without overwriting the value of the existing translation. |
Beta Was this translation helpful? Give feedback.
-
Addressed these shortcomings this way. @khwabscript script was not enough for some cases. :) use Spatie\Translatable\HasTranslations;
trait HasTranslationsDefaultFallback
{
use HasTranslations;
public function getTranslations(string $key = null, array $allowedLocales = null): array
{
if ($key !== null) {
$this->guardAgainstNonTranslatableAttribute($key);
return array_filter(
json_decode($this->getAttributes()[$key] ?? '' ?: '{}',
true) ?: [config('app.fallback_locale') => $this->getAttributes()[$key] ?? null],
fn($value, $locale) => $this->filterTranslations($value, $locale, $allowedLocales),
ARRAY_FILTER_USE_BOTH,
);
}
return array_reduce($this->getTranslatableAttributes(), function ($result, $item) use ($allowedLocales) {
$result[$item] = $this->getTranslations($item, $allowedLocales);
return $result;
});
}
} |
Beta Was this translation helpful? Give feedback.
-
Hello there,
I'am wondering if some of my data can't be localized such as company names, acronym or even not translated yet.
How can I get the default value if transaction not found, should I add the two values into json localization.
What about adding property to enable/disable fallback so we can refer to default value if fallback is turned off.
For example:
fallback_enabled => true|false
Beta Was this translation helpful? Give feedback.
All reactions