Languages
Choose the response language with the locale parameter, and build language-prefixed links and switchers
Set the locale URL parameter to choose the language of a FrontStage API response. Without it, the response uses the default language of the site.
curl -s -G https://www.example.org/graphql \
--data-urlencode 'query={ pages(limit: 3) { docs { title path } } }' \
--data-urlencode 'locale=es'{
"data": {
"pages": {
"docs": [
{ "title": "Inicio", "path": "/es" },
{ "title": "Página no encontrada", "path": "/es/404" },
{ "title": "Acerca de", "path": "/es/about" }
]
}
}
}A field that has no translation returns the value in the default language.
Find the languages of a site
{
site {
defaultLocale
locale
locales {
code
isDefault
}
}
}site.locale is the language of the current response. A locale value that the site does not offer returns HTTP status 400 with the plain-text body Invalid locale parameter, not a GraphQL response.
Paths include the language
Every path field returns a site-relative URL in the language of the response:
- In the default language, a path has no prefix:
/events/holst-the-planets. - In another language, a path starts with the language code:
/es/events/holst-the-planets.
Use path for links. Do not build paths from slugs.
Build a language switcher
Every content type has alternates: the same record in each language the site offers, including the current one.
query Alternates($slug: String!) {
event(slug: $slug) {
title
locale
alternates {
locale
path
}
}
}{
"data": {
"event": {
"title": "Holst: The Planets",
"locale": "en",
"alternates": [
{ "locale": "en", "path": "/events/holst-the-planets" },
{ "locale": "es", "path": "/es/events/holst-the-planets" }
]
}
}
}Use the same list for <link rel="alternate" hreflang="..."> tags.
Route a language-prefixed URL
route(path:) takes the path with its language prefix. Set locale to the same language. To find the language from a URL, use splitLocalePath from the client:
import { createFrontstageClient, splitLocalePath } from "@baskerhq/frontstage-client";
const basker = createFrontstageClient({ endpoint: "https://www.example.org/graphql" });
const { locale } = splitLocalePath("/es/events/holst-the-planets", ["en", "es"], "en");
// locale is "es"
const { route } = await basker.query(
`query Route($path: String!) { route(path: $path) { __typename } }`,
{ path: "/es/events/holst-the-planets" },
{ locale },
);A prefixed path without the matching locale returns RouteNotFound. See Routing.
page(path:) is different: it takes the path without the language prefix, for example /about, and uses locale for the language.
Generate pages for every language
routes lists the public URLs in the language of the request. Send one routes query for each language in site.locales. See Build a site with Astro.
Related
- Routing
- Caching:
localeis part of the cache key. - Content model