I18n-module: Lazy loading for locales list

Created on 22 Mar 2019  路  10Comments  路  Source: nuxt-community/i18n-module

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)
feature-request pinned 馃搶

Most helpful comment

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 :/

All 10 comments

What status for this request?

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

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 :/

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 鉂わ笍.

@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?

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.

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.

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.

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.

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.

Was this page helpful?
0 / 5 - 0 ratings