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!