Spatie handles translations in Laravel applications primarily through two packages: laravel-translatable and laravel-translation-loader. Each serves a different purpose in managing multilingual content.
1. Spatie Laravel Translatable
This package is designed to make Eloquent models translatable without the need for additional database tables. It stores translations as JSON within a single database column.
How it works:
- You add the `HasTranslations` trait to your Eloquent model.- Define which attributes are translatable by setting a public `$translatable` array on the model.
- The translatable attributes are stored as JSON in the database column.
- You can set and get translations for different locales using methods like `setTranslation()`, `getTranslation()`, and `getTranslations()`.
Example:
php
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
class NewsItem extends Model
{
use HasTranslations;
public $translatable = ['name'];
}
You can then set translations like this:
php
$newsItem = new NewsItem;
$newsItem->setTranslation('name', 'en', 'Name in English')
->setTranslation('name', 'nl', 'Naam in het Nederlands')
->save();
Getting the translation respects the current app locale:
php
app()->setLocale('nl');
echo $newsItem->name; // Outputs: Naam in het Nederlands
It also supports nested JSON keys for translations, for example:
php
$newsItem->setTranslation('meta->description', 'en', 'Description in English')
->setTranslation('meta->description', 'nl', 'Beschrijving in het Nederlands')
->save();
You can retrieve these nested translations similarly[2][3][7].
Important features:
- No extra tables needed; translations are stored as JSON in the same table.- Automatically returns the translation for the current locale.
- Ability to disable fallback to default locale when a translation is missing by passing a third parameter `false` to `getTranslation()`.
2. Spatie Laravel Translation Loader
This package enables storing translations in the database rather than language files, allowing dynamic management of translations.
How it works:
- You install the package and replace Laravelâs default translation service provider with Spatieâs.- Run migrations to create a `language_lines` table where translations are stored.
- Translations are stored with a group, key, and an array of locale-text pairs.
- You can create translations programmatically by creating `LanguageLine` model instances.
- Laravelâs `__()` helper function is used to fetch translations, which now also checks the database.
Example:
php
use Spatie\TranslationLoader\LanguageLine;
LanguageLine::create([
'group' => 'validation',
'key' => 'required',
'text' => [
'en' => 'This is a required field',
'nl' => 'Dit is een verplicht veld',
],
]);
Then fetching translations:
php
__('validation.required'); // returns text based on current locale
Features:
- Supports overriding default Laravel language files with database entries.- Supports JSON translation lines by using group `'*'`.
- Extensible with custom translation loaders for other sources like YAML or CSV files[4][5].
Summary
- Spatie Laravel Translatable is ideal for translating model attributes using JSON columns directly in the modelâs database table, simplifying multilingual content management without extra tables.
- Spatie Laravel Translation Loader allows storing and managing translation strings dynamically in the database, integrating with Laravelâs translation system and overriding language files if needed.
Both packages complement each other and provide flexible, powerful solutions for handling translations in Laravel applications.
Citations:
[1] https://spatie.be/docs/laravel-translatable/v6/installation-setup
[2] https://github.com/spatie/laravel-translatable
[3] https://spatie.be/docs/laravel-translatable/v6/introduction
[4] https://github.com/spatie/laravel-translation-loader
[5] https://packagist.org/packages/spatie/laravel-translation-loader
[6] https://filamentphp.com/plugins/filament-spatie-translatable
[7] https://laraveldaily.com/lesson/multi-language-laravel/spatie-laravel-translatable
[8] https://github.com/filamentphp/filament/discussions/7364
[9] https://stackoverflow.com/questions/65252615/spatie-laravel-translatable-shows-the-default-locale-when-i-try-to-retrieve-th