adbrosaci / vue-lang-router

Vue language routing with (optional) localized URLs.
MIT License
66 stars 8 forks source link

How to do not show language index in url for default language #22

Open Lundark opened 2 years ago

Lundark commented 2 years ago

Hi @radek-altof, this plugin saved my day! I'm trying to figure out how to set the plugin not to show the default language in the url, this option would be handy for SEO.

// routerOptions log
// this is my default language
{
    "_value": "/it/"
}

When i try to catch the value of currentRoute it obviously '/' path and i can't do any redirect condition. Is there a way to remove language index from url?

Referenced issue #14 i figured out the path in the url is necessary, but if i make a normal a tag for default language, can i remove safely that index since the language has been set correctly?

Lundark commented 2 years ago

I solved it for now!

here src/plugin/index.js

i replace with:

-- export { createLangRouter, i18n };
++ export { createLangRouter, i18n, setLanguage };

in my component i make condition to catch my default language:

<language-switcher v-slot="{ links }" class="language-switch" >
  <div v-for="link in links" :key="link.langIndex">
    <router-link :to="link.url" v-if="link.langIndex != 'it'">
        &nbsp;&nbsp;
        <img :src="`/images/flags/${link.langIndex}.webp`" alt="" width="20" />
    </router-link>
    <a v-else :href="link.url.replace(/\/it\//g,'/')" @click="setLanguage('it')">
      &nbsp;&nbsp;
      <img :src="`/images/flags/${link.langIndex}.webp`" alt="" width="20" />
    </a>
  </div>
</language-switcher>
...
import { setLanguage } from '../plugin';
...

This way it works, but i don't know how it works with history. In my case i don't use history.