鍍金池/ 問答/網絡安全  HTML/ js中如何使用vue-i18n國際化獲取相應語言對應字符?

js中如何使用vue-i18n國際化獲取相應語言對應字符?

通過 vue-i18n 實現(xiàn)了多語言切換,在template里面已經實現(xiàn)了該功能,但是現(xiàn)在需要在 js 中 獲取對應語言的字符,網上找了好久還是沒找到,有人知道怎么弄么?

template 里面

{{ $t('message.exchangeCenter.exchangeDate') }}

js里面需要:需要把中文替換成多語言,求解

const exchangeTypes = [
    {id: 1, text: '游戲幣'},
    {id: 2, text: '積分'}
];

js中寫法:

const exchangeTypes = [
    {id: 1, text: this.$t('xxx')},
    {id: 2, text: this.$t('yyy')}
];

錯誤提示:
圖片描述

回答
編輯回答
有你在

怎么實現(xiàn)占位傳值呢 就是文本某個位置字符需要動態(tài)改變 比如:
message: {

  hello: '你好'{0}
}

就是可以實現(xiàn) 你好lucy \你好Rosi 這樣的
2017年3月16日 18:04
編輯回答
來守候

文檔都是擺設么
http://kazupon.github.io/vue-...

// If using a module system (e.g. via vue-cli), import Vue and VueI18n and then call Vue.use(VueI18n).
// import Vue from 'vue'
// import VueI18n from 'vue-i18n'
//
// Vue.use(VueI18n)

// Ready translated locale messages
const messages = {
  en: {
    message: {
      hello: 'hello world'
    }
  },
  ja: {
    message: {
      hello: 'こんにちは、世界'
    }
  }
}

// Create VueI18n instance with options
const i18n = new VueI18n({
  locale: 'ja', // set locale
  messages, // set locale messages
})

定義好你的字符以后,代碼中用
const exchangeTypes = [

{id: 1, text: this.$t('coin')},
{id: 2, text: this.$t('points')}

];

2018年6月9日 07:41
編輯回答
毀了心

謝謝,問題已經解決了,回來晚了,不好意思。主要問題是這個應該算擴展的 vue 對象,所以this指向的和我們寫vue代碼里面的this指向是一樣的,所以多語言對象只能寫到vue實例里面,而不能夠直接在js中通過 $t 的方式來獲取多語言字符。

2018年3月17日 19:54
編輯回答
墨小白

題主你好,我也遇到了同樣的問題,但是看了你的回答還不是很明白,題主最后怎么解決的呢?謝謝分享^-^

2017年2月2日 13:22