Handling missing translations | laravel-translatable-go | Spatie       

 SPATIE  

laravel-translatable-go
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-translatable-go](https://spatie.be/docs/laravel-translatable-go/v1)  Basic-usage  Handling missing translations

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-translatable-go/v1) 

  Handling missing translations    
- [ Introduction ](https://spatie.be/docs/laravel-translatable-go/v1/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-translatable-go/v1/requirements)
- [ Upgrading ](https://spatie.be/docs/laravel-translatable-go/v1/upgrading)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-translatable-go/v1/installation-setup)
- [ Testing ](https://spatie.be/docs/laravel-translatable-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-translatable-go/v1/releasing)
- [ Questions and issues ](https://spatie.be/docs/laravel-translatable-go/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-translatable-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-translatable-go/v1/support-us)
- [ About us ](https://spatie.be/docs/laravel-translatable-go/v1/about-us)

Basic usage
-----------

- [ Getting and setting translations ](https://spatie.be/docs/laravel-translatable-go/v1/basic-usage/getting-and-settings-translations)
- [ Removing translations ](https://spatie.be/docs/laravel-translatable-go/v1/basic-usage/removing-translations)
- [ Replacing translations ](https://spatie.be/docs/laravel-translatable-go/v1/basic-usage/replacing-translations)
- [ Querying translations ](https://spatie.be/docs/laravel-translatable-go/v1/basic-usage/querying-translations)
- [ Validating translations ](https://spatie.be/docs/laravel-translatable-go/v1/basic-usage/validating-translations)
- [ Handling missing translations ](https://spatie.be/docs/laravel-translatable-go/v1/basic-usage/handling-missing-translations)

Advanced usage
--------------

- [ Available events ](https://spatie.be/docs/laravel-translatable-go/v1/advanced-usage/available-events)
- [ Customize the toArray method ](https://spatie.be/docs/laravel-translatable-go/v1/advanced-usage/customize-the-toarray-method)
- [ Usage with factories ](https://spatie.be/docs/laravel-translatable-go/v1/advanced-usage/usage-with-factories)

 Handling missing translations
=============================

Sometimes your model doesn't have a requested translation. Using fallback functionality, you can decide what should happen. Configure it through the package's `New` callback or a view-specific `Config`.

### Falling back to a specific locale

```
translatable.New(func(cfg *translatable.Config) {
    cfg.FallbackLocale = "fr"
})
```

Registration uses `TRANSLATABLE_FALLBACK_LOCALE`, then `APP_FALLBACK_LOCALE`, then `en` by default. An explicitly configured empty fallback disables the configured locale. `GetTranslation(ctx, key, locale, false)` never falls back.

### Fallback locale per model

Define `TranslationFallbackLocale() string` on your model to override the package fallback. Only that selected fallback is tried. If it has no value, the package fallback is not tried afterward.

### Falling back to any locale

Set `cfg.FallbackAny = true` to return another translation when both the requested locale and selected fallback are missing. Go maps have no insertion order, so the lexically first allowed locale is selected. This differs deliberately from PHP's first-inserted locale.

### Customize fallbacks

Set a callback to log a missing translation or contact another service:

```
cfg.MissingKey = func(ctx context.Context, missing translatable.MissingTranslation) (*string, error) {
    slog.WarnContext(ctx, "Missing model translation",
        "key", missing.Key,
        "locale", missing.Locale,
        "fallback_locale", missing.FallbackLocale,
    )
    return nil, nil
}
```

The callback receives the actual custom model, key, requested locale, selected locale and fallback value. It runs only when another locale was selected, matching the PHP implementation. Return a non-nil string pointer to replace the displayed value. Returning nil or an error preserves the fallback. Callback panics are not caught.

### Disabling fallbacks on a per model basis

Implement `UseTranslationFallback() bool` returning false to disable fallback for `GetAttribute`. An explicit `GetTranslation` call still honors its boolean argument.

### Null and empty values

Null and empty strings are filtered by default. Set `AllowNull` or `AllowEmptyString` to retain them. An allowed null or empty requested value prevents fallback. Zero and false are valid values. `HasTranslation` never counts null as present.

A missing translation returns an empty string, or nil when null is allowed or the underlying base column is nil. Filtered values disappear when a setter rewrites that locale map, matching PHP's read-filter-then-write behavior.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
