Locale urls on nuxt

Hi, we are using prismic with nuxt to build landing pages on our site. So we need to translate the pages urls to make links.

Is there any way to use the localePath from the i18n library to get one page uid in other language?

Thanks

Hey Fausto, welcome to the Forum!

Have you tried working with switchLocalePath ? Quoting Nuxt: – It returns a link to the current page in another language. I found this piece of information in this page.

I'm guessing you're using the UIDs to build the URLs, right? Are these UIDs all the same between languages or not?

Thanks Paulina!

The problem is that the UIDs are different for each language, for example, on english it might be "hello", in spanish "hola" and so on.

So when i need to make a link to that page, i need to use something like localePath("hello") and that should return /hello when the site is in english and /hola when the site is in spanish.

Thanks!

Ok I see, I think accessing the alternate languages of each document will help you build this routes. Have you seen our multi language example that uses Nuxt.js?

Check it out and see how we handle this dynamic routes:

Here's a simple example of how you can retrieve the alternate_languages from a query:

  async asyncData({ $prismic, error }) {
    try {
      // Query to get post content
      const result = await $prismic.api.getByUID('page', params.uid)

      return {
        doc: result.data.body,
        altLangs: result.alternate_languages
      };
    } catch (e) {
      // Returns error page
      error({ statusCode: 404, message: "Page not found" });
    }
  }

Then, you just need to pass it to the link resolver:
<nuxt-link :to="$prismic.linkResolver(altLang)">

1 Like

This issue has been closed due to inactivity. Flag to reopen.