Getting useContent to return content in the visitor's current locale
my app already keeps the active language in a context, and useContent always comes back in the same language regardless. I want it to return the localized default that matches whatever locale the visitor is currently on.
How do I pass the active locale through the hook so the right localized default comes back?
2 answers
Pass the active locale as preferredLocale in the useContent options. Pull it from the context you already have and hand it straight in:
const {locale} = useLanguage();const content = useContent('home-hero', { preferredLocale: locale, fallback: {title: 'Welcome'},});If you want a baseline for when nothing sets it, defaultPreferredLocale on the provider sets a fallback locale at initialization. One thing to guard: an unresolved locale falls back to the default content, so a route where the language has not been resolved yet just gets the default rather than an error.
Worth knowing what actually produces the localized variants: per-locale default content is the localization add-on. So preferredLocale selects which locale you want, but you get distinct content per locale only once that is set up. Without it the hook still resolves, you just get the single default back for every locale.
got it, wiring the context locale into preferredLocale now and will look at the add-on for the per-locale defaults. thanks