Localization
Foir supports multi-language content out of the box. Configure locales for your project, translate content fields in the editor, and ask for a language on each query.
Overview
Localization in Foir works by attaching per-locale translations to translatable fields on your records. Locales are BCP-47 language codes. When you request a locale, Foir works down an ordered list of places a field’s value could come from and serves the first one that has it.
Key concepts
- Locale: a BCP-47 language code (for example
en-US,fr-FR,de-DE) representing a language and region. - Source locale: the locale marked as the project’s default. It is the language you author in, and the editor labels it “Source”. Omit the
localeargument on a query and you get these authored values. - Translatable fields:
textandprosefields, unless a field opts out withconfig.translatable: false. Other field types (numbers, dates, booleans, references, media) share a single value across all locales.
How a requested locale is resolved
Ask for a locale and Foir works down this list, field by field. The first entry that holds a value for the field is the one you are served:
- The locale you asked for. Casing does not matter, so a request for
en-gbfinds theen-GByou configured. - The fallback locale you set on it, then that locale’s own fallback, for as many hops as you have configured.
- The closest matches among your project’s other locales, within the same language and writing system, closest first.
- Your project’s default locale.
- The value the content was authored in.
Because the list is walked per field rather than per record, a partly translated record can come back with its title from one entry and its body from another, in the same response. An ordinary read does not say which entry supplied which value. Ask and it will: see checking what you were served.
Worked examples
Take a project whose default locale is en-GB. It also publishes fr-FR, an fr-CA set to fall back to en-GB, an es-ES set to fall back to de-DE, and a de-DE that has been deactivated.
| Requested | Served | Why |
|---|---|---|
fr-CA | en-GB | You set that fallback deliberately, so it outranks the closer language match to fr-FR. |
fr | fr-FR | The project does not publish bare fr, so it works through the French locales it does publish, closest match first. |
ja-JP | en-GB | The project publishes no Japanese at all, so the list runs out and ends at the default. |
es-ES | en-GB | The route runs through de-DE, but a deactivated locale is only ever passed through, never served. |
Each row describes a field with no value of its own in the requested locale. A field that does have one is served it, every time.
Where several locales qualify as a closest match, all of them are tried before the default rather than only the nearest. On this project a request for fr reaches fr-FR and then fr-CA, so a field translated only into Canadian French still answers it.
Two behaviours are worth stating because they tend to surprise people:
- A locale you have not configured still resolves. An ordinary read treats it as any other request and ends at your default rather than at nothing, so a mistyped code shows up as content in an unexpected language rather than as a failure. On a project that has no default locale the list ends one entry earlier, at the value the content was authored in. Locale reporting is what turns it back into something you can catch.
- The closest-match step stays inside one written language. A reader who asks for
pt-PTis never handedpt-BRcopy by it, and the same holds the other way round. Set a fallback locale on one of them if that is what you want.
Leaving the locale out
Omit the locale argument and every translatable field comes back with the value it was authored in. That is not the same as naming a locale you have not configured: with nothing requested there is nothing to resolve, so the list above is never walked.
Locale properties
Each locale has:
| Property | Description |
|---|---|
locale | BCP-47 code (e.g., en-US) |
displayName | Human-readable name (e.g., “English (United States)”) |
nativeName | Name in the native script (e.g., “English (US)”) |
isDefault | Whether this is the project’s default (source) locale |
isActive | Whether the locale is served. Deactivate one and a fallback edge pointing at it is still passed through to whatever comes next, but never returned. The closest-match and default steps skip it outright, so it is only routes you configured by hand that carry on through it. |
isRtl | Whether the language is right-to-left |
fallbackLocale | Your override: the locale to serve instead when this one has no value. It is tried before the closest language match, and the locale it names may carry a fallback of its own. It has to name a locale this project already configures, so a code with a typo in it is refused when you save rather than sitting there resolving nowhere. Leave it unset to let the closest match decide. |
In the console
Configuring locales
- Go to Settings > Locales
- Click New Locale
- Select or enter the BCP-47 code (e.g.,
fr-FR) - Set the display name
- Mark one locale as the default
The form also offers a fallback locale. Fill it in when you want requests for this locale to be served a particular other one, and leave it empty to let the closest language match decide. It has to name a locale you have already added, so add the target first; a code the project does not carry is refused when you save. Deactivating a locale on the same form stops it being served. A route that falls back through it carries on to whatever comes next.
Translating content
- Open any record in the editor
- Use the locale switcher at the top of the editor to switch between languages
- Translatable fields show separate inputs for each locale
- Save when translations are complete
Non-translatable fields (numbers, dates, booleans, references) share the same value across all locales. A text or prose field that has opted out of translation is edited as one value too: on one of the record’s own fields, the editor edits that value whichever locale is on screen, and says beside it that a change applies to all of them.
Via the CLI
List locales
foir locales listDeactivated locales are hidden. Add --include-inactive to see them too, which you need when you are checking a fallback route that runs through one:
foir locales list --include-inactiveGet a locale
By ID:
foir locales get loc_abc123By code:
foir locales get fr-FRGet the default locale
foir locales defaultCreate a locale
foir locales create --data '{
"locale": "fr-FR",
"displayName": "French (France)",
"nativeName": "Fran\u00e7ais (France)",
"isRtl": false
}'Create a locale that falls back to another
fallbackLocale is the locale to serve when this one has no value for a field. Here, a request for fr-CA is served en-GB rather than fr-FR, even though fr-FR is the closer language match:
foir locales create --data '{
"locale": "fr-CA",
"displayName": "French (Canada)",
"nativeName": "Fran\u00e7ais (Canada)",
"fallbackLocale": "en-GB"
}'en-GB has to exist before this runs. A fallback naming a locale the project does not configure is refused as an invalid argument, and so is one pointing at itself or closing a loop. That refusal is what stops a typo becoming an edge that resolves nowhere: the chain would skip it in silence and hand the request to the closest language match instead, which is the opposite of what you configured it for.
Update a locale
Send fallbackLocale to change where a locale falls back to, and isActive to stop or resume serving it:
foir locales update loc_abc123 --data '{
"displayName": "French (France)",
"nativeName": "Fran\u00e7ais (France)",
"fallbackLocale": "en-GB",
"isActive": true
}'Delete a locale
foir locales delete loc_abc123A locale another locale falls back to cannot be deleted, and neither can the one currently serving as the project’s default. Re-point or clear the fallbacks first, or promote another locale to default. Both refusals exist for the same reason the fallback field is checked on the way in: every chain that ran through the deleted locale would otherwise start resolving somewhere else, across every record at once, with nothing in the response to say why.
Via the API
Requesting localized content
Pass the locale argument on the per-model query. Translatable fields come back resolved for that locale directly on the typed fields, each one taking the first entry of the resolution list that holds a value:
query {
page(naturalKey: "about", locale: "fr-FR") {
_id
title
body
}
}The same read through the data client, where locale is an ordinary argument too:
const page = await foir.page.findUnique({ naturalKey: 'about', locale: 'fr-FR' });Resolution is unchanged: the list is walked server-side and the resolved values arrive on the typed fields, because the client prints the query above and sends it to the same executor.
Batch locale requests
Fetch multiple records in the same locale with the plural connection, filtering by natural key:
query {
pages(
where: { naturalKey: { in: ["about", "contact", "faq"] } }
locale: "de-DE"
) {
edges {
node {
_naturalKey
title
}
}
}
}The same batch through the data client:
const pages = await foir.page.findMany({
where: { naturalKey: { in: ['about', 'contact', 'faq'] } },
locale: 'de-DE',
});
for (const { node } of pages.edges) {
console.log(node._naturalKey, node.title);
}findMany returns the same Relay connection the GraphQL query does, with edges, pageInfo and totalCount, rather than a flattened array, so pagination stays reachable.
Checking what you were served
A read says nothing about how it resolved unless you ask. Two switches do the asking, and neither changes the content that comes back.
No SDK equivalent: both switches answer under the GraphQL response’s
extensions, and the data client hands back the record on its own, so it has nothing to return them on. Read them over/graphql.
localeStrict reports the gaps. Turn it on and extensions.localeStrict lists the field paths that got nothing from the list and came back as authored, the ones nobody has translated into any language included. A field declared translatable: false is never a gap. Ask for a locale the project does not configure and that comes back as an error beside the data, which is what makes a mistyped code fail a CI run rather than quietly serve the wrong language. That error needs a configured set for the request to be outside of, so a project that has configured no locales at all raises it for nothing:
query {
page(naturalKey: "about", locale: "fr-FR", contexts: { localeStrict: true }) {
_id
title
body
}
}resolutionTrace reports the whole walk. Turn it on and extensions.resolution carries, for each field, the entries that were tried in order, which one answered, and why the others did not. It is far larger than the content it explains and it needs a key holding the targeting:debug scope, so treat it as a debugging tool rather than something to leave on. Without that scope it is simply absent:
query {
page(naturalKey: "about", locale: "fr-FR", contexts: { resolutionTrace: true }) {
_id
title
}
}One request’s trace is capped. Past a fixed number of candidate lists the records still to come carry truncated in place of theirs, and the report as a whole carries it too, so a trace that stopped early says so rather than reading as though the candidates ran out. The per-field locale summary is outside the cap and still counts every field, which is why localeStrict and the coverage figures stay complete on a response whose lists were cut.
Each switch also has a request header, X-Foir-Locale-Strict and X-Foir-Resolution-Trace, for turning one on from a shell or a CI job without editing the query.
Writing translations
The public per-model write surface is locale-agnostic: createPage / updatePage accept a single set of field values, and there is no translations input or locale argument on mutations. Author per-locale content in the console editor with the locale switcher (see Translating content above). The public API’s role in localization is read-time resolution via the locale argument.
Translations can also be written without the editor. From the CLI, foir records set-field-locale writes one locale’s translation of one field, or several fields and locales as one change. Its flags are in the generated command reference. A connected agent can do the same. Several written together are one change to the record, which on a versioned model is one version.
Reading locale configuration
Add, edit and deactivate locales in the console under Settings > Locales, or from the CLI with the commands shown above. foir locales list --include-inactive --json is the quickest way to read a project’s whole locale configuration back, fallbacks included. The flag is part of the answer rather than an extra: without it the deactivated locales are filtered out, and those are exactly the ones a fallback route can run through without ever being served.
User language preference
A signed-in user carries a preferredLocale on their own profile: a
canonical BCP47 tag naming the language they read. null means they have never
chosen one, and the project default applies.
The user sets it themselves:
const me = await foir.ns.auth.currentUser();
await foir.ns.users.updateUser({ input: { id: me.id, preferredLocale: 'fr-CA' } });updateUser is the one update of a user. A signed-in user who names
their own id and changes only their language is asked for no permission, and no
role has to grant it: a preference the person holding it cannot set is a
preference in name only. Anything else on that field asks users:write, which
no user’s role may hold, so a user cannot change another user, or
their own email or status, through it.
updateUser is on the schema of a key that holds users:write. On every
other key, a publishable key among them, the user sets their own with
foir.ns.auth.updateMyProfile({ preferredLocale }). It changes only the
signed-in user’s own row, and also takes their firstName, lastName and
avatarUrl.
Pass an empty string to clear the choice and fall back to the project default. The tag has to name a locale your project publishes; anything else is refused with the published list in the error.
Storing the preference does not by itself change what a request is served. A
storefront reads it and passes it as the locale argument, the same as any
other locale choice.
Localization vs variants
Localization and variants work together but serve different purposes:
| Feature | Purpose | Example |
|---|---|---|
| Localization | Translate the same content into different languages | English to French translation |
| Variants | Show different content to different audiences | Mobile users see a simplified layout |
A single variant can have translations in multiple locales. For example, your “VIP” variant can have both English and French translations.
Best practices
- Translate every field you serve in a locale. An untranslated field is filled from somewhere further down the list rather than coming back empty, which is easy to miss on a page that is otherwise fully translated.
- Use standard BCP-47 codes (e.g.,
en-US, notenglishorglobal). A code Foir cannot read as a language takes no part in the closest-match step, so nothing reaches it but a request that spells it character for character, casing and all, or a fallback you point at it by hand. - Set a fallback locale when the closest language match is not the answer you want. A regional locale that should be served your main market’s copy rather than its own language’s is the usual case.
- Keep translatable copy (
textandprosefields) in dedicated fields so it can be localized per locale; values like alt text are part of the media value and shared across all locales. - Mark a field that holds a link, an anchor, a slug or a key as not translatable, with
config.translatable: false. Translation tools then leave it alone, and the editor offers no translation of it. - Start with your primary market locales and expand as needed. You can add locales at any time.
- Test right-to-left rendering if you support Arabic, Hebrew, or other RTL languages.