Translatable Input
The <x-wirekit:: component edits one value in several languages: a product name,
a category, a shipping method's description. The primary language's field comes first. Under it,
every further language has a chip that says whether it is translated, and a field of its own. With a
few languages every field is on screen; with many, a chip opens its language, one at a time.
Usage
The first language of locales is the primary one unless primary names another. A translation that
is still missing shows the primary text as its placeholder, so the reader sees what to translate, and
its chip says "missing" until something is typed.
The value stands for one field, and it reads as one: it is a field.set with
as-field, so its legend is drawn as a field's label, and the primary language's field stands on the
height of an input beside it in the same row. With a single language in
locales, it is an ordinary field: the label in sight, no code beside it, and the same binding,
messages and help, so a form looks as it did before a second language came, and shows the codes from
the second one on. Without locales, it is that one field in the application's language.
Many Languages
With more further languages than inline (2 by default), the fields of the further languages close.
A chip opens its language and closes the one open before, and "Show all languages" opens every one of
them, in two columns from the sm breakpoint on.
A chip shows its language's code, a check for translated, a dash for missing and a warning sign for a field the server rejected. The state is in the chip's accessible name as well, "Italiano: missing", so it never rests on its color.
With Livewire
wire:model on the component binds each language to its key of one array, name.de, name.en,
with the modifiers you give it:
// app/Livewire/EditCategory.php
use Livewire\Component;
class EditCategory extends Component
{
// 1. One array per value, keyed by the language code
public array $name = ['de' => '', 'en' => '', 'fr' => ''];
public function save(): void
{
// 2. The primary language is required, a translation is not
$this->validate([
'name.de' => ['required', 'string', 'max:120'],
'name.*' => ['nullable', 'string', 'max:120'],
]);
}
}
{{-- resources/views/livewire/edit-category.blade.php --}}
<x-wirekit::translatable-input
label="Category"
:locales="['de' => 'Deutsch', 'en' => 'English', 'fr' => 'Français']"
wire:model.blur="name"
required
/>
An error the server files under one language, name.fr, stands under that language's field, its chip
shows the warning sign, and a closed field opens. An error filed under the value itself, name,
stands under the fields, as under any field, and every language's field says it is invalid and
points at it. required marks the primary language only: a translation stays optional.
Like every field, it takes a labelAfter slot for a mark after the legend, a badge that says the
value is not decided yet, for example, as described for
Input.
What belongs to a field goes onto each language's field, as on an input, where the attributes land
on the field. x-on:keydown. runs when the reader presses Enter in any
language, and Enter on a chip still only opens its language. wire:keydown, x-on:keydown,
@keydown and their keyup and keypress forms are handed on, and so are autocomplete,
autocapitalize, autocorrect, spellcheck, inputmode, enterkeyhint, maxlength,
minlength, pattern (one-line fields only, since a textarea takes none), tabindex,
aria-describedby, which every language's field reads after its own message, and form, which joins
every language to a form elsewhere on the page. An aria-label or aria-labelledby names the set
of languages, while each field keeps its language's name; with a single language it names the field.
A listener with
.window or .document stays on the component, where it runs once.
In a Plain Form
Without wire:model, name is the base of the fields' names, name[de], name[en], and value
fills them:
<form method="POST" action="/categories/12">
@csrf
@method('PUT')
<x-wirekit::translatable-input
label="Category"
name="name"
:locales="['de' => 'Deutsch', 'en' => 'English']"
:value="old('name', $category->name)"
/>
</form>
$category->name is the array of texts, as a JSON column cast to array stores it. Laravel reads
the request back as the same array, and an error under name.en reaches the English field.
old('name', …) brings back what the reader typed when the request fails validation; with
$category->name alone, every language would show the stored text again and the edits would be
gone.
Several Lines
multiline gives every language a Textarea, with rows lines:
Words for One Language
hints gives one language a sentence of its own, under its field, as a field's hint stands under it.
The field is described by it, and a code that is not in locales is left out:
Inside x-wk-unsaved, a language whose text the server does not have yet
takes the unsaved line on its field and a dot at its code, which stands where a field's label would.
Words beside the code would push its field out of line with the other languages, so the field's
description names them, as it does for every unsaved field.
Naming the Languages
Give locales as a map of code to name, in the words your readers know. A list of codes works too:
each language is then named in the reader's language through PHP's intl extension, en as
"Englisch" for a German reader, and by its code where intl is not installed. A code is letters,
digits, - and _ (pt-BR, zh_Hant); anything else is left out, with a warning in the log while
APP_DEBUG is on.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
label |
string|null |
null |
The value's name: the legend over its languages, and the label of its field with one language |
locales |
array |
[] |
The languages, a map of code to name or a list of codes. Without one, the application's language |
primary |
string|null |
null |
The primary language's code. Without it, the first of locales |
value |
array|null |
null |
The text per language, for a form without wire:model |
name |
string|null |
null |
The base of the fields' names, name[de] |
multiline |
bool |
false |
A textarea per language instead of an input |
rows |
int|string |
3 |
The lines of each textarea, or auto to grow with its text |
inline |
int |
2 |
How many further languages stand as fields without asking |
hint |
string|null |
null |
A sentence about the value, under its fields |
hints |
array |
[] |
A sentence for one language, under its field: a map of code to text. See Words for One Language |
help |
string|null |
null |
An explanation in a tooltip beside the legend |
error |
string|false|null |
null |
A message about the value as a whole. Without it, the error bag's message for name or the model. false shows none, for the value or any language, and leaves the $errors bag alone, see Turning an Error Off |
required |
bool |
false |
Marks and requires the primary language |
placeholder |
string|null |
null |
The primary field's placeholder |
disabled |
bool |
false |
Disables every language |
readonly |
bool |
false |
Makes every language read-only |
size |
string |
'md' |
sm, md or lg, as on Input |
scope |
string|null |
null |
Scoped personalization key |
Accessibility
- The value is a
fieldsetnamed by its legend, and each field is named by its language in full, so a screen reader announces "Product name, group" and then "English, edit text". - Each field carries
langwith its code, so a screen reader reads the text in that language's pronunciation (WCAG 3.1.2). - A chip is a button. Its name is the language and its state, "English: translated"; the code and the glyph on it are hidden from screen readers, and the colors follow the state without carrying it alone (WCAG 1.4.1).
- Where a chip opens its language, it carries
aria-expandedandaria-controls, and the focus moves to the field it opened. "Show all languages" carriesaria-expandedas well. - A field the server rejected opens and stays open, so its message is in reach.
- A value with a single language is a single field named by its label, without a group around it.
Keyboard Interaction
| Key | Action |
|---|---|
Tab |
Moves through the primary field, the chips, "Show all languages" and the open fields |
Enter / Space |
On a chip: opens its language and moves to its field, or closes it; with every field on screen, moves to its field. A key listener of yours runs on the fields only |
Design Tokens
| Token | Used for |
|---|---|
--color-wk-bg-muted |
The code beside each field |
--color-wk-text-muted |
A further language's code, and a chip before it is translated |
--color-wk-text |
The primary language's code |
--color-wk-border-strong |
A chip's edge |
--color-wk-bg-hover |
A chip under the pointer |
--color-wk-border-success |
The edge of a translated chip |
--color-wk-success-text |
The text of a translated chip |
--color-wk-border-error |
The edge of a chip whose field has an error |
--color-wk-danger-text |
The text of a chip whose field has an error |
--color-wk-bg-active |
The chip of the open language |
--gap-wk-sm |
The space between a code and its field, and between two fields |
--gap-wk-xs |
The space between the chips |
The fields are Input and Textarea, and the group is a field set, each with its own tokens.
Further Reading
- Field for the group's legend, hint and help
- WCAG 3.1.2: Language of Parts