Translatable fields
Last updated:
On this page
With javaabu/translatable v3 installed, a field can take one input per language:
TextInput::make('name')->label(__('Name'))->required()->translatable();
Textarea::make('description')->translatable(layout: 'stacked');
Wysiwyg::make('body')->translatable(fn (?Post $record) => $record?->is_public);
When translatable is installed, enabled, and the form's model translates the field (Translatable::supports($model, 'name')), the field becomes one input per active locale, named {field}_{locale}: name_en, name_dv. Otherwise (the package isn't installed, translations are off, or the model or field isn't translatable) it's a plain field, so ->translatable() is always safe.
| Inputs | One per active locale, the model's primary locale first, each with its language's direction, lang, and the Thaana keyboard for Dhivehi. Labels are "Name (ދިވެހި)" |
| Layout | tabs (the tab with an error opens) or stacked |
| State | name_en, name_dv from Translatable::values(); name holds the primary locale's value, so $get('name') works |
| Validation | Your rules through Translatable::rules(): the primary locale as written, the others with required swapped for nullable. translatable(require_all: true) requires every locale |
| Saving | validate() returns name_en and name_dv, which translatable models accept in fill(), create() and update() |
| Conditions | visibleWhen('name', …) watches the primary locale's input |
| Drivers | Each input has name (bracket notation, meta[subtitle_dv]) and statePath (meta.subtitle_dv) |
Both drivers render the same inputs: Blade with javaabu/forms' tabs and inputs, React with @javaabu/forms. The JSON node has a translatable key with the inputs (see JSON format). To use translatable's own React component, register it as the translatable type:
<StencilProvider components={{ translatable: MyTranslatableField }}>
Records that aren't translatable models
Some records keep per-locale values themselves, like javaabu/meta's meta values. Such a record (or a form class) implements Javaabu\Stencil\Contracts\ResolvesTranslatableFields, and stencil asks it which fields are translatable instead of Translatable::supports():
use Javaabu\Stencil\Contracts\ResolvesTranslatableFields;
class Post extends Model implements ResolvesTranslatableFields
{
public function isTranslatableField(string $state_path): bool
{
return MetaFields::isTranslatable(static::class, Str::after($state_path, 'meta.'));
}
}
The field then gets one input per active locale, as above, for its full state path: Group::make()->statePath('meta') with TextInput::make('subtitle')->translatable() gives meta.subtitle_en and meta.subtitle_dv in the state, rules and validated data (input names meta[subtitle_en], meta[subtitle_dv]). Values are read with data_get($record, 'meta.subtitle_dv'), and the rules are expanded by stencil the same way: the primary locale as written, the others nullable.
isDeclaredTranslatable() tells whether ->translatable() was called, whether or not the field ends up with per-locale inputs.
Installing
Stencil only suggests translatable. Install it in your app to use translatable fields:
composer require javaabu/translatable:^3.0@dev
See translatable's docs for making models translatable.