0

I'd like to redirect from /my-account to /hr/moj-racun using nuxt i18n (because Croatian is the default language, and the mapping between my-account and moj-racun exists).

Instead it redirects to /hr/my-account.

The page folder inside my nuxt app folder is named 'my-account', and this is my current setup:
(I've set the alwaysRedirect, but it takes the entered url and prepends the language prefix, but it does not translate it by the given mapping)

[
  'nuxt-i18n',
  {
    locales: [
      {
        name: 'Hrvatski',
        code: 'hr',
        iso: 'hr-HR',
        file: 'hr.js'
      },
      {
        name: 'English',
        code: 'en',
        iso: 'en-US',
        file: 'en.js'
      }
    ],
    langDir: 'locales/',
    strategy: 'prefix',
    defaultLocale: 'hr',
    lazy: true,
    detectBrowserLanguage: false
  }
]

Edit, this is my-account page specific nuxt-i18n setup:

export default {
      nuxtI18n: {
        paths: {
          en: '/my-account',
          hr: '/moj-racun'
        }
      }
    }

If I go to the page like http://example.com/my-account it gets redirected to http://example.com/**hr**/my-account, without translation.

steakoverflow
  • 1,206
  • 2
  • 15
  • 24
  • What about removing the `index` from your routes? – Bruno Martins Jul 20 '20 at 16:06
  • Sorry, the global page setup wasn't used anyway, I had the local nuxt-i18n setup for all pages. Please check out the edit. – steakoverflow Jul 21 '20 at 08:06
  • Is the last piece of code written in a `/pages/my-account.vue` file? – Bruno Martins Jul 21 '20 at 08:23
  • It is in /pages/my-account/index.vue, and it is working. This call gets me there(to Croatian URL): this.$router.push(this.localePath({ name: 'my-account' })) So the mapping must be working, just no way to redirect from global URL slug to local. This is a problem, because I sometimes get redirects from language agnostic referrers, but would like the user to land in his language or in the default language. – steakoverflow Jul 21 '20 at 10:36
  • The mapping may be working for nuxt, but I'm not sure that @nuxt/i18n expects the pages to be defined like that. You can create a `/pages/my-account-test.vue` and see if it works better. – Bruno Martins Jul 21 '20 at 12:19
  • I tried, but it still redirects from /my-account to /hr/my-account, no translation of the page slug. – steakoverflow Jul 22 '20 at 05:42

1 Answers1

0

That is because your default locale is set to hr, If you want to redirect and switch language and translate data use this tag:

<nuxt-link
  v-for="locale in availableLocales"
  :key="locale.code"
  :to="switchLocalePath(locale.code)">{{ locale.name }}</nuxt-link>

you can visit this page: https://i18n.nuxtjs.org/lang-switcher

behnoush s
  • 31
  • 5