i18n icon indicating copy to clipboard operation
i18n copied to clipboard

Lazy loading for locales list

Open tasiek opened this issue 5 years ago • 12 comments

What problem does this feature solve?

In some cases, there's a need for a dynamic locales list (i.e. localizations created by editor on CMS). Currently, locales only allows a static array.

What does the proposed changes look like?

To allow dynamic locales listing, locales should allow functions as well, including those returning a Promise.

This feature request is available on Nuxt community (#c217)

tasiek avatar Mar 21 '19 23:03 tasiek

What status for this request?

zyrianov-v-fd avatar May 27 '19 10:05 zyrianov-v-fd

i need this also or is there a way to manipulate via module

export default function lang () {
  /**
   locales: [
      {
        code: 'en',
        iso: 'en-US',
        name: 'English',
        file: 'en.json'
      },
      {
        code: 'de',
        iso: 'de-DE',
        name: 'Deutsch',
        file: 'de.json'
      }
    ],
  */
  this.options.i18n.locales = [
    {
      code: 'en',
      iso: 'en-US',
      name: 'English',
      file: 'en.json'
    },
    {
      code: 'de',
      iso: 'de-DE',
      name: 'Deutsch',
      file: 'de.json'
    }
  ]
}

i tried but i it seems not working

cannap avatar Jun 21 '19 14:06 cannap

found a solution

nuxt.config.js defaults is just a normal nuxt config obj

export default async () => {
  const url = `${process.env.BASE_URL}/translation/languages`
  const { data: response } = await axios.get(url)
  const locales = response.data.map(lang => {
    return {
      ...lang,
      file: `${lang.code}.json`
    }
  })

  defaults.i18n.locales = locales

  return defaults
}

//e i forgot that every languages needs a file which returns content or a promise :/

cannap avatar Jun 25 '19 08:06 cannap

This is also necessary for pages. I am using Wordpress as a headless CMS and I made an endpoint that returns the sites structure. This would help me add and remove pages from CMS.

Currently I can work with the solution provided by cannap, but when the pages and locales options would allow functions this plugin would be perfect ❤️.

sven-ra avatar Aug 13 '19 13:08 sven-ra

@cannap Thank you very mutch to share your information about this topic and also your answer. Really i think this is my answer too, But i don't know where can i use this in my code, i mean which part of my code i have to put it?

fahamidev avatar Sep 15 '19 08:09 fahamidev

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 14 '19 08:11 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 13 '20 14:01 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 24 '20 22:04 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 24 '20 10:06 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 03 '20 14:10 stale[bot]

This is taking the locales on the build time. Is it also possible to load the locales on runtime? Eg, when the site is loaded?

export default async () => {
  const url = `${process.env.BASE_URL}/translation/languages`
  const { data: response } = await axios.get(url)
  const locales = response.data.map(lang => {
    return {
      ...lang,
      file: `${lang.code}.json`
    }
  })

  defaults.i18n.locales = locales

  return defaults
}

klodoma avatar Aug 29 '22 09:08 klodoma

No, the module generates routes based on specified locales and it can only do that at build time.

rchl avatar Aug 29 '22 09:08 rchl