Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How does Spatie manage translation validation


How does Spatie manage translation validation


Spatie manages translation validation primarily through Laravel's standard validation system, but with some specific approaches and tools tailored for translatable attributes.

How Spatie Handles Translation Validation

1. Validation of Translatable Attributes

Spatie's Laravel Translatable package stores translations in a single JSON column per translatable attribute. To validate these attributes, you typically validate the JSON structure or the individual translations within the JSON. However, the package itself does not provide built-in validation rules out of the box specifically for translations.

Developers usually handle validation by:

- Validating the translatable attribute as an array where each key corresponds to a locale and the value is the translation string.
- Using Laravel's array validation rules to ensure each locale's translation meets criteria (e.g., required, string, max length).

For example, if you have a translatable 'name' attribute, you might validate it like this in a FormRequest or validator:

php
$request->validate([
    'name.en' => 'required|string|max:255',
    'name.nl' => 'required|string|max:255',
]);

This approach ensures each translation is validated individually.

2. Uniqueness Validation for Translations

For validating uniqueness of translated attributes (e.g., ensuring a translated name is unique in a given locale), Spatie recommends using the separate package laravel-unique-translation. This package extends Laravel's validation rules to check uniqueness within translations stored as JSON.

This means you can validate that a translation for a specific locale is unique before saving or updating the database, which is not straightforward with standard Laravel unique validation rules due to the JSON storage format[1].

3. Handling Missing Translations and Fallbacks

While not strictly validation, Spatie provides mechanisms to handle missing translations gracefully through fallback locales or custom fallback callbacks. This ensures that if a translation is missing or invalid, the system can fall back to a default locale or execute custom logic (like logging or fetching from an external service)[9].

4. Translation Loader and Validation

Spatie also offers the laravel-translation-loader package that stores translations in the database and supports fetching them dynamically. While this package focuses on loading and storing translations rather than validating them, it complements the translation workflow by allowing flexible translation management[5][6].

Summary

- Spatie's main translatable package stores translations in JSON columns and expects developers to validate translations as arrays using Laravel's native validation rules.
- For uniqueness validation of translated attributes, Spatie suggests using the laravel-unique-translation package.
- Missing translation handling and fallback logic are supported to ensure robustness but are not part of validation per se.
- The laravel-translation-loader package supports database-backed translations but does not directly address validation.

This approach leverages Laravel's powerful validation system while addressing the challenges posed by storing translations as JSON in a single column.

References: [1][5][6][9]

Citations:
[1] https://spatie.be/docs/laravel-translatable/v6/basic-usage/validating-translations
[2] https://github.com/spatie/laravel-translatable/issues/112
[3] https://spatie.be/docs/laravel-translatable/v6/installation-setup
[4] https://github.com/spatie/laravel-validation-rules
[5] https://packagist.org/packages/spatie/laravel-translation-loader
[6] https://github.com/spatie/laravel-translation-loader
[7] https://laraveldaily.com/lesson/multi-language-laravel/spatie-laravel-translatable
[8] https://github.com/spatie/laravel-validation-rules/blob/master/README.md
[9] https://spatie.be/docs/laravel-translatable/v6/basic-usage/handling-missing-translations