0

I am working on a multi-lingual project. Which has 3 locales en-SG, en-VN, and vi-VN. Among them, en-SG is a default locale.

Is this possible to query the docs in a way so that when there is no doc for any locale(especially en-VN) the site shows the default locale doc?

I have achieved something similar with this query.

*[_type == "cardPage" && (__i18n_lang == $locale || __i18n_lang == $defaultLocale ) && !(_id in path('drafts.**'))]

But it's not working properly when the $locale doc available, it's querying the $locale doc and $defaultLocale doc at the same time. And showing the $defaultLocale doc instead of the actual $locale doc on some pages.

I am using nextjs for front end and sanity-plugin-intl-input plugin for localization.

1 Answers1

0

You can add your default document to your result and return the first match.

[(
  *[ _type == "something" && the_one_you_want ] +
  *[ _type == "something" && fallback_or_default ]
)[0]]

There are different flavors of concatenation. Here is another with Spread syntax.

[
  ...*[ _type == "something" && the_one_you_want ],
  ...*[ _type == "something" && fallback_or_default ]
][0]
cfm
  • 156
  • 1
  • 2
  • 12