Get fallback locale from locale chain #1787
Unanswered
frederikheld
asked this question in
Q&A
Replies: 1 comment
-
I figured it out: import { useI18n } from 'vue-i18n'
import { fallbackWithLocaleChain, createCoreContext } from '@intlify/core-base'
const { locale, fallbackLocale } = useI18n()
// function start
const fallbackChain = fallbackWithLocaleChain(
createCoreContext({}),
fallbackLocale.value as string,
locale.value
)
for (let i = 0; i < fallbackChain.length; i++) {
if (value[fallbackChain[i]]) {
return value[fallbackChain[i]]
}
}
return undefined
// function end If there's a simpler (maybe built-in) way to do this, please let me know. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have data from the database that is structured like this:
I wrote a recursive function that looks for
i18n
keys and returns the data reduced to the current locale.The above object for the locale
'de-DE'
would be reduced toNow I would like to add support for fallback locales, so if a data key in the object is
i18n['de']
instead ofi18n['de-DE']
, it should fall back to that locale and ultimately to the default locale configured in the plugin options (which isen-US
).Unfortunately I can't figure out how to get that configuration out of the
i18n
library. I found thefallbackWithLocaleChain
function in the API docs but I can't figure out how to import it or how to set it up with aCoreContext
properly.Can anyone give me a hint how to approach that?
EDIT:
After digging through the source code, I figured out that I have to import it from a different package like this:
import { fallbackWithLocaleChain } from '@intlify/core-base'
. So the first problem is solved. But how do I use this function?Beta Was this translation helpful? Give feedback.
All reactions