Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle anchors correctly #16

Closed
TheAlexLichter opened this issue Feb 10, 2018 · 3 comments
Closed

Handle anchors correctly #16

TheAlexLichter opened this issue Feb 10, 2018 · 3 comments

Comments

@TheAlexLichter
Copy link
Contributor

TheAlexLichter commented Feb 10, 2018

I want to use nuxt-i18n to "translate" anchor links (though it might sound weird).

Example config:

 locales: [
    {
      code: 'en',
      iso: 'en-US',
      name: 'English'
    },
    {
      code: 'de',
      iso: 'de-DE',
      name: 'Deutsch'
    }
  ],
  defaultLocale: 'en',
    work: {
      de: '/referenzen'
    },
    '#contact-us': {
      de: '#schreib-uns'
    },

If I iterate over all links and use the localePath function..

  <nuxt-link
          v-for="link in links"
          :key="link"
          :to="localePath(link)">
          {{ $t(`links.link`) }}

the anchor tag isn't resolved correctly. I guess that the problem lies in the export of base routes while ignoring other routes that aren't included in the base routes of Nuxt

This question is available on Nuxt.js community (#c6)
@paulgv
Copy link
Collaborator

paulgv commented Feb 10, 2018

Hi @manniL !
To translate anchor links, you could simply use vue-i18n to generate localized ID's for your DOM.
Here's how I'd do it:

<template>
  <section>
    <a :href="`#${$t('contactus_anchor')}`">Contact link</a>
    <!-- More stuff here -->
    <div :id="$t('contactus_anchor')">
      Contact content
    </div>
  </section>
</template>

And in your translations files:

// en
module.exports = {
  contactus_anchor: 'contact-us'
}

// de
module.exports = {
  contactus_anchor: 'schreib-uns'
}

@TheAlexLichter
Copy link
Contributor Author

Thanks for the fast answer!
I pursued that approach, though it feels a little "hacky". But it works great ☺️

@israelmuca
Copy link

I know this is closed already, but if someone wants another workaround for this problem, I'm using a button with @click="viewGallery"

viewGallery:

viewGallery() {
      let route = this.localePath("story")+"/#gallery"
      this.$router.push(route)
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants