nuxt-modules / i18n

I18n module for Nuxt
https://i18n.nuxtjs.org
MIT License
1.76k stars 484 forks source link

When override locales from Layer, I get an error: When using the langDir option the locales must be a list of objects. How to override Layer configuration from project layer? #2978

Open lna1989 opened 5 months ago

lna1989 commented 5 months ago

Environment


Reproduction

https://stackblitz.com/edit/nuxt-starter-cchusa?file=nuxt.config.ts

Describe the bug

I need to remove unnecessary languages that are present in the Layer. To override, Nuxt tells us to use the arrow function. But I get an error: When using the langDir option the locales must be a list of objects if switch:

locales: [
      {
        code: 'en',
        iso: 'en-US',
        name: 'English',
        file: 'en_US.json',
      },
    ],

to

  locales: () => {
      return [
        {
          code: 'en',
          iso: 'en-US',
          name: 'English',
          file: 'en_US.json',
        },
      ];
    },

Additional context

No response

Logs

[6:05:55 PM]  ERROR  Cannot start nuxt:  [@nuxtjs/i18n]: In project layer (/home/projects/nuxt-starter-7ixgvf/nuxt.config) - When using the langDir option the locales must be a list of objects.

  at checkLayerOptions (node_modules/@nuxtjs/i18n/dist/module.mjs:681:13)
  at setup (node_modules/@nuxtjs/i18n/dist/module.mjs:1771:5)
  at normalizedModule (node_modules/@nuxtjs/i18n/node_modules/@nuxt/kit/dist/index.mjs:2212:37)
  at async Module.installModule (node_modules/@nuxt/kit/dist/index.mjs:2499:95)
  at async initNuxt (node_modules/nuxt/dist/index.mjs:4040:7)
  at async NuxtDevServer._load (node_modules/nuxi/dist/chunks/dev2.mjs:267:5)
  at async NuxtDevServer.load (node_modules/nuxi/dist/chunks/dev2.mjs:199:7)
  at async NuxtDevServer.init (node_modules/nuxi/dist/chunks/dev2.mjs:194:5)
  at async Object.run (node_modules/nuxi/dist/chunks/dev-child.mjs:115:5)
  at async runCommand$1 (node_modules/nuxi/dist/shared/nuxi.a9f3bca7.mjs:1678:16)
kazupon commented 5 months ago

@BobbieGoede Can you give some advice for layers if you have any ideas? 🙏

BobbieGoede commented 5 months ago

Hmm I can only find docs about overwriting with a function for app.config.ts here, I'll look into figuring out a way to allow overwriting locales with a function.

lna1989 commented 5 months ago

@kazupon @BobbieGoede Oh, I'm sorry, it looks like I really messed up the rewrite functionality in app.config.ts. But in any case, we must have some method not of extending, but of overwriting the configuration described in nuxt.config.ts for i18n.

Under the hood, the functionality uses https://github.com/unjs/c12 And the same approach is used there through specifying the mergeStrategy function, as far as I understand, just like in app.config.ts - https://github.com/unjs/c12/issues/145