NuxtN
Nuxt9mo ago
13 replies
bauti

Dynamic baseUrl in i18n config in nuxt.config.ts

Hello! i am trying to use a dynamic baseUrl, passing a function, but it throws that it is not serializable, could someone help me to fix it?
This is my code:
i18n: {
    vueI18n: 'i18n.config.ts',
    strategy: 'prefix',
    baseUrl: (context) => {
      if (import.meta.server) {
        const { req, beforeNuxtRender } = context;
        beforeNuxtRender(({ nuxtState }) => {
          nuxtState.host = req.headers.host;
        });
      }
      return (
        'https://'
        + (import.meta.server ? context.req.headers.host : context.nuxtState.host)
      );
    },
    defaultLocale: 'es',
    detectBrowserLanguage: {
      useCookie: true,
      cookieKey: 'i18n_redirected',
      redirectOn: 'root'
    },
    locales: [
      {
        code: 'en',
        file: 'en.json',
        language: 'en-US',
        name: 'English'
      },
      {
        code: 'es',
        file: 'es.json',
        language: 'es-AR',
        name: 'Español'
      },
      {
        code: 'pt',
        file: 'pt.json',
        language: 'pt-PT',
        name: 'Português'
      }
    ],
    lazy: true
  },

I tried with this:
baseUrl: (context) => { return 'https://test.com'; },
and didnt work too
Was this page helpful?