0

Using NuxtJS, how to output the user's chosen locale in the axios headers? So that when requesting to the backend, the 'Accept-Language' header with the selected locale is transmitted.

nuxt.config.js

...
  // Modules: https://go.nuxtjs.dev/config-modules
  modules: [
    // https://go.nuxtjs.dev/axios
    '@nuxtjs/axios',
    // https://go.nuxtjs.dev/pwa
    '@nuxtjs/pwa',
    '@nuxtjs/i18n',
  ],

  i18n: {
    nuxtI18nHead: true,
    strategy: 'prefix_except_default',
    defaultLocale: "en",    
    locales: [
      {
        code: 'de',
        ico: 'de-DE',
        file: 'de-DE.json'
      },
      {
        code: 'ru',
        ico: 'ru-RU',
        file: 'ru-RU.json'
      },
      {
        code: 'en',
        ico: 'en-US',
        file: 'en-US.json'
      },
    ],
    detectBrowserLanguage: {
      useCookie: true,
      cookieKey: "i18n_redirected",
      alwaysRedirect: false,
      fallbackLocale: "en",
      redirectOn: 'root',
    }
  },

  // Axios module configuration: https://go.nuxtjs.dev/config-axios
  axios: {
    baseURL: 'http://127.0.0.1:8000',
    headers: {
      common: {
        'Accept-Language': 'en' /// Получить тут выбранную локаль
      },      
    }
  },

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • I doubt you can do that during build time. Meanwhile, you could update the axios instance on the fly and send the info still. – kissu Nov 25 '22 at 08:33

0 Answers0