Fallback locale query - which locale was returned from fallback?

I started refactoring my code with the new fallback feature, but I miss something:
how do I know which fallback locale was returned by the query?

Our urls looks like this: /en/post-title-in-english
I can perfectly fetch the right fallback slug with the fallbackLocales: [en, it, de] query, but struggle with the “/en/” part for the url.

Is there a better way other than grab this information out of “_allFieldLocales” with a custom fallback solution in the code?

/**
 * Retrieve fallback's value
 * @param  {Array}  allFieldLocales     _allSlugLocales from model
 * @param  {string} activeLocale        Current active locale
 * @return {string}                     Available value by active locale if is undefined or empty so return others availables as a fallback
 */
export const getLocaleValue = (
  allFieldLocales: MultiLocaleField[],
  activeLocale: string
): MultiLocaleField => {
  // Get each value by locale en, it and de
  const en = allFieldLocales?.filter((field) => field?.locale === "en")[0]
  const it = allFieldLocales?.filter((field) => field?.locale === "it")[0]
  const de = allFieldLocales?.filter((field) => field?.locale === "de")[0]

  // Assign values and define fallbacks locales
  const values = [
    {
      en: en || it || de,
      it: it || en || de,
      de: de || en || it
    }
  ]

  return values[0][activeLocale]
}

Thanks for your suggestion!

1 Like

Hello @thomas.iacopino

For now that seems like the best way to get that information.
But i do agree with you that returning what locale was selected as a fallback in the response would be a great addition, and would simplify this process quite alot.

Could you open a feature request for it here: ✋ Feature requests - DatoCMS community
This way we can track its progress a little bit better :slight_smile:

Thank you @m.finamor !
The feature request can be found here Output _currentLocale for fallback locales queries

1 Like