|
| 1 | +# Fallback locale |
| 2 | + |
| 3 | +## App wide |
| 4 | + |
| 5 | +If you want to fallback to a default translation when a translation has not been found, enable this in the configuration using the `use_fallback` key. And to select the default locale, use the `fallback_locale` key. |
| 6 | + |
| 7 | +```php |
| 8 | +[ |
| 9 | + // ... |
| 10 | + 'use_fallback' => true, |
| 11 | + 'fallback_locale' => 'en', |
| 12 | + // ... |
| 13 | +] |
| 14 | +``` |
| 15 | + |
| 16 | +## per Model |
| 17 | + |
| 18 | +You can also define per-model the default for "if fallback should be used", by setting the `$useTranslationFallback`property: |
| 19 | + |
| 20 | +```php |
| 21 | +class Post extends Model |
| 22 | +{ |
| 23 | + public $useTranslationFallback = true; |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +## for Properties |
| 28 | + |
| 29 | +Even though we try having all models nicely translated, some fields might left empty. What's the result? You end up with missing translations for those fields! |
| 30 | + |
| 31 | +The property fallback feature is here to help. When enabled, translatable will return the value of the fallback language for those empty properties. |
| 32 | + |
| 33 | +The feature is enabled by default on new installations. If your config file was setup before v7.1, make sure to add the following line to enable the feature: |
| 34 | + |
| 35 | +```php |
| 36 | +'use_property_fallback' => true, |
| 37 | +``` |
| 38 | + |
| 39 | +Of course the fallback locales must be enabled to use this feature. |
| 40 | + |
| 41 | +If the property fallback is enabled in the configuration, then translatable will return the translation of the fallback locale for the fields where the translation is empty. |
| 42 | + |
| 43 | +### **customize empty translation property detection** |
| 44 | + |
| 45 | +This package is made to translate strings, but in general it's also able to translate numbers, bools or whatever you want to. By default a simple `empty()` call is used to detect if the translation value is empty or not. If you want to customize this or use different logic per property you can override `isEmptyTranslatableAttribute()` in your main model. |
| 46 | + |
| 47 | +```php |
| 48 | +protected function isEmptyTranslatableAttribute(string $key, $value): bool |
| 49 | +{ |
| 50 | + switch($key) { |
| 51 | + case 'name': |
| 52 | + return empty($value); |
| 53 | + case 'price': |
| 54 | + return !is_number($value); |
| 55 | + default: |
| 56 | + return is_null($value); |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## **Country based fallback** |
| 62 | + |
| 63 | +Since version v5.3 it is possible to use country based locales. For example, you can have the following locales: |
| 64 | + |
| 65 | +* English: `en` |
| 66 | +* Spanish: `es` |
| 67 | +* Mexican Spanish: `es-MX` |
| 68 | +* Colombian Spanish: `es-CO` |
| 69 | + |
| 70 | +To configuration for these locales looks like this: |
| 71 | + |
| 72 | +```php |
| 73 | + 'locales' => [ |
| 74 | + 'en', |
| 75 | + 'es' => [ |
| 76 | + 'MX', |
| 77 | + 'CO', |
| 78 | + ], |
| 79 | + ]; |
| 80 | +``` |
| 81 | + |
| 82 | +We can also configure the "glue" between the language and country. If for instance we prefer the format `es_MX` instead of `es-MX`, the configuration should look like this: |
| 83 | + |
| 84 | +```php |
| 85 | +'locale_separator' => '_', |
| 86 | +``` |
| 87 | + |
| 88 | +What applies for the fallback of the locales using the `en-MX` format? |
| 89 | + |
| 90 | +Let's say our fallback locale is `en`. Now, when we try to fetch from the database the translation for the locale `es-MX`but it doesn't exist, we won't get as fallback the translation for `en`. Translatable will use as a fallback `es` \(the first part of `es-MX`\) and only if nothing is found, the translation for `en` is returned. |
| 91 | + |
0 commit comments