Spatie manages translations in Laravel applications by integrating with Laravel's localization system and providing flexible ways to store and retrieve translations based on the app's current locale.
How Spatie Handles Translations When the App Locale Changes
1. Translation Storage and Retrieval
- Spatie offers a package called laravel-translation-loader which allows storing translations in a database instead of just language files. This package replaces Laravel's default translation service provider with Spatie's provider, enabling translations to be fetched dynamically from the database[1].
- Translations are stored in a table (usually `language_lines`) where each entry contains a group (like "validation"), a key (like "required"), and a JSON object holding translations for multiple locales (e.g., `'en' => 'This is a required field', 'nl' => 'Dit is een verplicht veld'`)[1].
- When you call Laravelâs `__()` function with a translation key, Spatieâs loader fetches the translation for the current locale set in the application. For example, if the app locale is `'nl'`, `__('validation.required')` returns the Dutch translation from the database[1].
2. Locale Switching
- The app locale can be changed at runtime using Laravelâs `app()->setLocale('locale_code')` method.
- Once the locale is changed, all subsequent translation retrievals via `__()` or other translation functions automatically return the text in the newly set locale.
- If a translation is missing for the current locale in the database, Laravelâs fallback locale mechanism can return the translation from the default locale, unless explicitly disabled[1][8].
3. Model-Level Translations with JSON Storage
- Another Spatie package, laravel-translatable, is designed for Eloquent models to make attributes translatable.
- Translations are stored as JSON in a single database column per translatable attribute, without needing extra tables[3].
- When the app locale changes, accessing a translatable attribute on the model returns the value for the current locale automatically. For example, `$newsItem->name` will return the translation for the current locale set in the app[4].
- You can explicitly get or set translations for specific locales using methods like `getTranslation(attribute, locale)` and `setTranslation(attribute, locale, value)`[4].
4. Fallback and Missing Translations
- Spatieâs translatable package supports fallback locales so if a translation is missing for the current locale, it can return the translation from another locale (usually the default)[8].
- This behavior can be controlled by passing a boolean flag to `getTranslation` to disable fallback and get an empty string if the translation is missing[8].
Summary
When the app locale changes, Spatieâs translation packages automatically serve the appropriate translation from either database-stored language lines or JSON-stored model attributes, leveraging Laravelâs locale setting. This seamless integration ensures that translation retrieval functions return the correct localized string based on the current app locale, with fallback mechanisms available if a translation is missing.
This approach allows dynamic locale switching at runtime and supports storing translations in flexible ways, either in database tables or JSON columns, depending on the package used.
References: [1][3][4][8]
Citations:
[1] https://github.com/spatie/laravel-translation-loader
[2] https://spatie.be/docs/laravel-translatable/v6/introduction
[3] https://github.com/spatie/laravel-translatable
[4] https://spatie.be/docs/laravel-translatable/v6/basic-usage/getting-and-settings-translations
[5] https://filamentphp.com/plugins/filament-spatie-translatable
[6] https://github.com/filamentphp/filament/discussions/6913
[7] https://laraveldaily.com/lesson/multi-language-laravel/spatie-laravel-translation-loader
[8] https://stackoverflow.com/questions/65252615/spatie-laravel-translatable-shows-the-default-locale-when-i-try-to-retrieve-th