Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do you handle nested translations in Spatie


How do you handle nested translations in Spatie


In Spatie's Laravel Translatable package, handling nested translations is supported by using "dot" or "arrow" notation to access and set translations inside JSON columns. This allows you to work with nested keys within a single translatable attribute, stored as JSON.

How to Handle Nested Translations in Spatie Laravel Translatable

1. Set Up the Model:**
- Use the `HasTranslations` trait in your Eloquent model.
- Declare a public `$translatable` array with the attributes you want to translate, including nested keys using the `->` notation.

php
   use Illuminate\Database\Eloquent\Model;
   use Spatie\Translatable\HasTranslations;

   class NewsItem extends Model
   {
       use HasTranslations;

       public array $translatable = ['name', 'meta->description'];
   }
   

2. Setting Nested Translations:**
- Use `setTranslation()` with the nested key path using `->` to specify the nested attribute.
- Example for setting translations for `meta->description` in different locales:

php
   $newsItem = new NewsItem;

   $newsItem->setTranslation('meta->description', 'en', 'Description in English')
            ->setTranslation('meta->description', 'nl', 'Beschrijving in het Nederlands')
            ->save();
   

3. Getting Nested Translations:**
- Access nested translations using the same key path.
- When you access the attribute directly, it returns the translation for the current app locale.

php
   $description = $newsItem->meta->description; // Returns 'Description in English' if app locale is 'en'
   
   $descriptionInDutch = $newsItem->getTranslation('meta->description', 'nl'); // Returns 'Beschrijving in het Nederlands'
   

4. Working with Nested Keys:**
- The package stores translations as JSON in the database, allowing nested structures.
- You can define multiple levels of nesting by using the `->` notation in the key, e.g., `'admin.cruds.users.fields.firstName'` can be represented as `'admin->cruds->users->fields->firstName'` when setting or getting translations.

5. Fallbacks and Missing Translations:**
- Spatie Translatable supports fallback locales if a translation is missing.
- You can configure fallback behavior globally or per model and even provide a callback to handle missing keys.

Summary

Spatie Laravel Translatable handles nested translations by allowing you to specify nested keys using the `->` notation in the translatable attributes. This lets you store and retrieve deeply nested translation strings within a JSON column on your model, without needing separate tables. The trait `HasTranslations` manages this seamlessly, enabling you to set and get translations for nested keys just like flat attributes[2][5][6][1].

This approach is particularly useful for complex translation structures like form fields or metadata descriptions, keeping everything within a single JSON column while maintaining easy access to nested translation values.

Citations:
[1] https://github.com/spatie/laravel-translation-loader/discussions/167
[2] https://github.com/spatie/laravel-translatable
[3] https://spatie.be/docs/laravel-data/v4/as-a-data-transfer-object/nesting
[4] https://spatie.be/docs/laravel-permission/v6/advanced-usage/extending
[5] https://spatie.be/docs/laravel-translatable/v6/installation-setup
[6] https://spatie.be/docs/laravel-translatable/v6/basic-usage/handling-missing-translations
[7] https://stackoverflow.com/questions/54480013/laravel-and-package-spatie-laravel-translatable-make-search-query-for-special
[8] https://www.reddit.com/r/laravel/comments/1j3613p/pros_and_cons_by_using_spatietranslatable/
[9] https://laraveldaily.com/lesson/multi-language-laravel/php-json-trans-underscores
[10] https://stackoverflow.com/questions/74401880/how-to-nest-same-data-type-with-spatie-laravel-data