如何使用 Vue-i18n 创建一个更改语言的按钮

How to create a button which changes languages with Vue-i18n

我有一个导航栏,需要用两个不同的按钮在语言之间切换。我使用 Vue-CLI 和 Vue-I18N 作为模板语法,但我无法在它们之间更改语言。文档中的解决方案对我帮助不大。我的 Header.vue 、 main.js 和 App.vue 在下面。我在等你的答案。谢谢

Header.vue

<template>
    <div>
        <b-navbar
            toggleable="lg"
            type="light"
            variant="light"
        >
            <b-navbar-brand href="#">{{ $t('johnDoe') }}</b-navbar-brand>

            <b-navbar-toggle target="nav-collapse"></b-navbar-toggle>

            <b-collapse
                id="nav-collapse"
                is-nav
            >
                <b-navbar-nav>
                    <b-nav-item href="#">{{ $t('home') }}</b-nav-item>
                    <b-nav-item href="#">{{ $t('about') }}</b-nav-item>
                    <b-nav-item href="#">{{ $t('projects') }}</b-nav-item>
                    <b-nav-item href="#">{{ $t('education') }}</b-nav-item>
                    <b-nav-item href="#">{{ $t('contact') }}</b-nav-item>
                </b-navbar-nav>

                <!-- Right aligned nav items -->
                <b-navbar-nav class="ml-auto">
                    <b-nav-form>
                        <b-button
                            size="sm"
                            class="my-2 my-sm-0 btn-info"
                            type="submit"
                            @click="i18n.locale = 'en'"
                        >{{ $t('english') }}</b-button> &nbsp;
                        <b-button
                            size="sm"
                            class="my-2 my-sm-0"
                            type="submit"
                            @click="i18n.locale = 'tr'"
                        >{{ $t('turkish') }}</b-button>
                    </b-nav-form>
                </b-navbar-nav>
            </b-collapse>
        </b-navbar>
    </div>
</template>

<script>
export default {
  name: 'HelloI18n'
}
</script>

<i18n>
{
  "en": {
    "johnDoe": "John Doe",
    "home": "Home",
    "about": "About Me",
    "projects": "Projects",
    "education": "Education",
    "contact": "Contact",
    "english": "English",
    "turkish": "Turkish"
  },
  "tr": {
    "johnDoe": "John Doe",
    "home": "Anasayfa",
    "about": "Hakkımda",
    "projects": "Projelerim",
    "education": "Eğitim",
    "contact": "İletişim",
    "english": "İngilizce",
    "turkish": "Türkçe"
  }
}
</i18n>

<style scoped>
</style>

App.vue

<template>
  <div id="app">
    <Header></Header>
  </div>
</template>

<script>
import Header from "./components/Header.vue";

export default {
  name: "app",
  components: {
    Header,
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
</style>

main.js

import '@babel/polyfill'
import 'mutationobserver-shim'
import Vue from "vue";
import './plugins/bootstrap-vue'
import App from "./App.vue";
import i18n from './i18n'

Vue.config.productionTip = false;

new Vue({
  i18n,
  render: h => h(App)
}).$mount("#app");

按钮@click 事件中有错字。 i18n 可以从带有“$i18n”的模板访问,没有 "i18n" 所以,你的按钮代码必须是:

<b-button
  size="sm"
  class="my-2 my-sm-0 btn-info"
  type="submit"
  @click="$i18n.locale = 'en'"
>{{ $t('english') }}</b-button>