【问题标题】:Language Switcher doesn't apply the new locale语言切换器不应用新的语言环境
【发布时间】:2019-07-25 21:42:35
【问题描述】:

我正在使用 Nuxt 开始一个新网站,我目前正在将其设置为多语言(法语和英语)

我跟着this tutorial设置了翻译和语言切换器,得到了以下信息:

nuxt.config.js(相关部分)

    ['nuxt-i18n', {
      detectBrowserLanguage: {
        useCookie: true,
        alwaysRedirect: true
      },
      strategy: 'prefix_except_default',
      defaultLocale: 'fr',
      parsePages: false,
      seo: true,
      pages: {
        about: {
          en: '/about-us',
          fr: '/a-propos'
        },
        portfolio: {
          en: '/projects',
          fr: '/portfolio'
        }
      },
      locales: [
        {
          code: 'en',
          iso: 'en-US',
          name: 'English',
          file: 'en-US.js'
        },
        {
          code: 'fr',
          iso: 'fr-FR',
          name: 'Français',
          file: 'fr-FR.js'
        }
      ],
      lazy: true,
      langDir: 'lang/'
    }]

navbar.vue

  <nav class="header">
    <div class="logo">
      <nuxt-link :to="localePath('index')">
        <img src="https://bulma.io/images/bulma-logo.png" alt="Bulma: a modern CSS framework based on Flexbox" width="112" height="28">
      </nuxt-link>
    </div>
    <div class="links">
      <nuxt-link :to="localePath('about')">
        {{ $t('navbar.about') }}
      </nuxt-link>
      <nuxt-link :to="localePath('blog')">
        Blog
      </nuxt-link>
      <nuxt-link :to="localePath('portfolio')">
        Portfolio
      </nuxt-link>
      <nuxt-link :to="localePath('contact')">
        Contact
      </nuxt-link>
      <span>|</span>
      <nuxt-link v-if="$i18n.locale === 'fr'" :to="switchLocalePath('en')">
        English
      </nuxt-link>
      <nuxt-link v-else :to="switchLocalePath('fr')">
        Français
      </nuxt-link>

      {{ $i18n.locale }}
    </div>
  </nav>

这是我的目录结构(如果有帮助的话)

layouts/
  front.vue
  navbar.vue

pages/
  index.vue
  about.vue
  blog.vue
  portfolio.vue
  contact.vue

navbar.vue 文件在 front.vue 内部调用,这是我的布局。

问题如下:

  • 在任何页面上,当我尝试单击 languageSwitcher 链接时,我会被重定向到其英文版本(ie/a-propos 变为 /en/about-us),但其他链接会将我带回来到法语版。
  • {{ $i18n.locale }} 一直显示fr
  • 我尝试了以下代码块:
<template>
  ...
  <nuxt-link
    v-for="locale in availableLocales"
    key="locale.code"
    :to="switchLocalePath(locale.code)"
  >
    {{ locale.name }}
  </nuxt-link>
  ...
</template

<script>
export default {
  computed: {
    availableLocales () {
      return this.$i18n.locales.filter(i => i.code !== this.$i18n.locale)
    }
  }
}
</script>

它只显示英文,而它应该同时显示英文和法文。

我做错了什么,或者我错过了什么?

【问题讨论】:

    标签: javascript vue.js vuejs2 nuxt.js


    【解决方案1】:

    这对我有用,使用 Nuxt-$i18n 的 vue-select 和 availableLocales,希望它有所帮助:

    <template>
      <div>
        <client-only>
         <b-navbar>
           <div class="container">
          <b-navbar-brand
            class="navbar-brand-custom mr-2"
            :to="localePath('index')"
            :title="title">
    
          <div class="d-flex flex-row order-2 order-lg-3 ml-auto text-capitalize">
            <b-navbar-nav class="d-flex flex-row">
              <!-- Language menu -->
              <v-select
                v-model="selected"
                v-for="(lang, index) in availableLocales"
                :key="index"
                :value="lang.code"
                :searchable="false"
                @input="onChange"
                class="vue-select-custom"
              ></v-select> 
              <!-- Navbar menu -->
              <b-nav-item-dropdown
                id="menu-links"
                right
                no-caret
                role="manu"
                class="ml-0"
                menu-class="animated fadeIn text-right menu-links rounded-0"
              >
                <span class="dropdown-menu-arrow"></span>
    
                <template
                  v-slot:button-content
                  style="height:42px"
                >
              </b-nav-item-dropdown>
            </b-navbar-nav>
          </div>
        </div>
      </b-navbar>
      </client-only>
    

    <script>
      export default {
       data() {
         return {
           selected: this.$i18n.locale
         }
       },
       computed: {
        availableLocales() {
         return this.$i18n.locales.filter(i => i.code !== this.$i18n.locale);
       },
     },
     .....
     methods: {
      onChange(locale) {
        var language = locale.toLocaleLowerCase();
        this.$i18n.setLocaleCookie(language)
        this.$router.replace(this.switchLocalePath(language));
      },
      ....
     }
    }
    

    【讨论】:

    • 我一直在寻找这个答案,但我有一个问题,his.$router.replace(this.switchLocalePath(language));仅更改 URL 但不刷新内容。我应该刷新页面吗?
    • mmh 通常不需要刷新,你是否使用带有文件的语言文件夹,内容需要在文件中明确定义,否则你必须有条件地加载数据...
    【解决方案2】:

    这是由于alwaysRedirect: true,设置为false

    【讨论】:

    • 你应该添加一些解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 2018-08-09
    • 2019-06-25
    • 1970-01-01
    相关资源
    最近更新 更多