Localization

Localizations or i18n is treated as a first class citizen in GraphCMS.

We create localized fields for your data model instead of creating duplicate entries, which allows for more flexible query and mutation support.

Querying Locales

Locales can be queried four ways

The Project Default

{
messages {
subject
}
}

Defining a Fallback

{
messages(locales: [de_ch, de, en]) {
subject
locale
}
}

Locales will be served in the order they are requested, left to right (Swiss German, German, English). If nothing is found, the default locale will be returned.

Querying all the Locales

{
messages {
localizations {
subject
locale
}
}
}

This will return all the locales where values are present.

The Client Default

const fetch = require('isomorphic-unfetch');
fetch(YOUR_GRAPHCMS_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'gcms-locales': 'en',
},
body: JSON.stringify({ query: '{ posts { title } }' }),
})
.then(res => res.json())
.then(res => console.log(res.data));

By passing a locale header, you can query content in context of the client's preferred locale. It's possible to pass in a string array DE_CH, DE, EN as the value for fallback preference.

Using as a Filter

Fallback as a Filter

{
messages(locales: [de]) {
subject
locale
}
}

The fallback behavior will only return entries where the locale has a value (including null).

Mutating Locales

Updating Locales

mutation {
updateMessage(
where: { id: "123xyz" }
data: {
localizations: { update: { locale: en, data: { subject: "Good Bye" } } }
}
) {
id
}
}

Creating Locales

mutation {
updateMessage(
where: { id: "123xyz" }
data: {
localizations: { create: { locale: fr, data: { subject: "Au revoir" } } }
}
) {
id
}
}

Upserting Locales

mutation {
createMessage(
where: { id: "123xyz" }
data: {
localizations: {
upsert: {
locale: es
create: { subject: "¡Hola!" }
update: { subject: "¡Buenos Días!" }
}
}
}
) {
id
}
}

Deleting Locales

mutation {
deleteMessage(
where: { id: "123xyz" }
data: { localizations: { delete: { locale: fr } } }
) {
id
}
}

Note that you can't delete the default locale.

This site uses cookies to provide you with a better user experience. For more information, refer to our Privacy Policy