content icon indicating copy to clipboard operation
content copied to clipboard

Support i18n fallback language map

Open existe-deja opened this issue 3 years ago • 2 comments

Is your feature request related to a problem? Please describe.

My website follow this content folder structure

content/
  en/
    terms.md
    article-1.md
  en-us/
    article-1.md

But I en-us will contain only few pages for specific overrides. IE: I won't translate terms page twice or copy paste md files.

Describe the solution you'd like

I would like to be able to specify a map of decision to fallback content

Describe alternatives you've considered

for now I wait for an error and I refetch a fallback content

  async asyncData ({ app, $content }) {
    let page = null
    try {
      page = await $content(app.i18n.locale, 'terms')
        .fetch()
        .catch(async () => {
          return await $content(app.i18n.fallbackLocale[app.i18n.locale][0], 'terms')
            .fetch()
        })
    } catch (e) {
      console.log(e)
    }

    return {
      page
    }
  },

existe-deja avatar Mar 21 '22 12:03 existe-deja

Hello :)

I added label for v1 but I think this will be easily implementable via @nuxt/content v2!

I also add the v2 label so we can track compatibility with upcoming v2 of https://github.com/nuxt-community/i18n-module

Here is an example of the query you could implement in @nuxt/content v2:

Content structure:

content/
  en/
    terms.md
    article-1.md
  en-us/
    article-1.md

Code:

export default defineNuxtConfig({
   content: {
      defaultLocale: 'en',
      locales: ['en-us', 'en']
   }
})
// This would be a global useState representing current user locale
const locale = useState('global-locale', () => 'en')

// This is the query to add in your pages
const { data: content } = await useAsyncData('page-content', () => {
	return queryContent().locale(locale.value).find()
})

The fallback should be done with defaultLocale.

Tahul avatar May 09 '22 12:05 Tahul

I think I'm missing something

https://stackblitz.com/edit/github-wcjbwb?file=pages%2Findex.vue,nuxt.config.ts,content%2Fen%2Fabout.md

The fallback behavior isn't working and if I try to refetch after a catch I get a Nuxt error on server side but I get the expected result on client side.

existe-deja avatar Oct 10 '23 15:10 existe-deja