Mainstay GitHub
Content

One value, or one per language

There are two common shapes for content in more than one language. One gives each language a row of its own and groups the rows as translations of one entry. The other declares it per field: a field that opts in holds a value per language, and one that does not holds one value shared by all of them. Mainstay declares it per field.

The trouble with a row per language

When every language is its own row, every field is duplicated, the ones that must never differ included: a price, an author, a start date, a flag saying an entry is featured. Nothing in the schema can say that a field is not translated, so keeping them aligned is a convention, and a convention drifts quietly, separately in every language. Declared per field, it is a fact the schema holds.

Declared on the field

app/Content/Event.php
#[Route(['en' => '/events/{slug}', 'nl' => '/evenementen/{slug}'])]
class Event extends Entry
{
    #[Text(required: true, localized: true)]
    public string $slug;

    #[Date(required: true)]
    public CarbonImmutable $startsOn;

    #[Number(min: 0)]
    public ?int $priceInCents;
}

The slug is translated, so each language has an address of its own, and the route gives each language its own pattern where a segment is translated too. The date and the price are not, so they cannot differ between languages. An entry's title and content, which every entry has, are translated already.

Two tables, one join

The type's table holds what is shared, and the _locales table beside it what is translated, one row per entry and language under a unique key on the two:

Where each value of an event lives

Its date, in every language event.starts_on
Its price, in every language event.price_in_cents
Its title, per language event_locales.title
Its slug, per language event_locales.slug
Its text and blocks, per language event_locales.content

Every read joins the two on the language asked for, as an inner join: an entry with no row in a language is not there in that language, and is not served there. That is what having no fallback means. There is no fallback to the default language's value, and publishing and the trash act on every language at once.

Each language's path is a row in the lookup that routes requests, unique on the site, the language and the path, so two languages may each have an entry at /events/opening-night without either taking the other's.

Where each language is served

mainstay.locales in the config maps each language to where it is served: a path prefix, ['en' => '/', 'nl' => '/nl'], or a host per language. A language and where it is served are one fact, so they are written as one. A map a request could not be told apart by is refused: two languages at one base, or some with a host and some without. So is a path another language's prefix would answer instead, when it is written, since nobody could reach it.

What stays one value

Some things are shared by design. An entry's tags are rows in its taxonomy's pivot, which has no language column, so an entry has the same tags in every language, and each tag translates its own title. A block's fields are translated with the content they are in, whole: a field inside a block is never translated on its own. An image keeps one file and one focal point, with alt text in every language.

With one language configured, the admin draws no language switch and none of this shows, and the _locales tables are there for the day a second one is added.

Keep reading

All articles