Localization

Localization 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).

Querying all the Locales

{
messages {
subject # default locale
localizations {
subject #locale specific
locale
}
}
}

You will want to pass argument includeCurrent: true to include the current locale inside the localizations array.

Querying all the Locales (including the current locale)

{
messages {
localizations(includeCurrent: true) {
subject
locale
}
}
}

This will return all the locales where values are present. If nothing is found, the default locale will be returned.

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.


Were you expecting something more?

We are constantly contributing to our documentation, but if you spot something we're missing, let us know and we'll be sure to add it.

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