How to query multi-languages blog post?

I can read in “General Concepts - Localization” page that Dato supports “multi-language blogs, where some articles can be written only in English, other only in Italian and others in both languages.”.

It’s exactly our use case here!

However, as I’m quite new to GraphQL I don’t know how to query the list of articles in 2 cases mentionned above:

  • How to query the list of articles only available in English?
  • How to query the list of articles available in English and in another locale?

Can someone give me a hint on this? Thanks!

Hello @yann,

very sorry for the delay here.

You can fetch objects of one locale like this:

{
  allArticles(locale: it) {
    id
    ...
  }
}

Otherwise you can fetch all the locales of a field in this way:

{
  allArticles {
    _allFieldLocales {
      locale
      value
    }
  }
}

Hope this helps!

I ended up with the following solution with 2 queries: one for the default locale to display the articles only available in this default locale, another for the alternative locale which in my case must display articles in this alt locale mixed with the articles available in the default locale.

For the default locale:

allArticles(locale: $locale, orderBy: publishedAt_DESC, filter: {heroTitle: {exists: "true"}}) {
  heroTitle
}

For the alternative locale:

allArticles(locale: $locale, orderBy: publishedAt_DESC) {
  heroTitle
  _allHeroTitleLocales {
    value
    locale
  }
}

Then I verify if heroTitle in translated in the alternative locale or not in the _allHeroTitleLocales object.

Hope it can help others.

1 Like