 vue-i18n是个非常实用的多国语言组件!当你的网站要配置多种语言的界面时,用它是首选。用起来倒也容易,翻下手册,看几个实用的案例,就基本上八九不离十啰。可以说,上手的成本挺小的。 [参考手册](https://kazupon.github.io/vue-i18n/zh/) ```js npm install vue-i18n --save //main.js import VueI18n from 'vue-i18n' Vue.use(VueI18n) const DEFAULT_LANG = navigator.language // console.log(888, 'DEFAULT_LANG', DEFAULT_LANG) //888 "DEFAULT_LANG" "zh-CN" "DEFAULT_LANG" "en" const LOCALE_KEY = 'localeLanguage' const i18n = new VueI18n({ locale:'zh-CN', messages: { 'zh-CN': require('./assets/lang/zh_CN'), 'en': require('./assets/lang/EN') }, fallbackLocale: 'zh-CN' }) const setup = lang => { if (lang === undefined){ lang = window.localStorage.getItem(LOCALE_KEY) if (i18n.messages[lang] === undefined){ lang = DEFAULT_LANG } } window.localStorage.setItem(LOCALE_KEY, lang) Object.keys(i18n.messages).forEach(lang => { document.body.classList.remove(`lang-${lang}`) }) document.body.classList.add(`lang-${lang}`) document.body.setAttribute('lang', lang) Vue.config.lang = lang i18n.locale = lang } setup() //切换 setzhlang(){ let LOCALE_KEY = 'localeLanguage' let lang = 'zh-CN' localStorage.setItem(LOCALE_KEY, lang) this.$i18n.locale = 'zh-CN' }, setenlang(){ let LOCALE_KEY = 'localeLanguage' let lang = 'en' localStorage.setItem(LOCALE_KEY, lang) this.$i18n.locale = 'en' } //html {{ $t('message.orderNumber') }} {{ $t('message.paymentMethod') }} ``` 在`./assets/lang/zh_CN`这个文件夹下配置中文或是其它的语言包就要可以,然后配上前端的语言切换就可以了。还是相当易用的。
author | lemooljiang |
---|---|
permlink | vue-i18n-75 |
category | hive-105017 |
json_metadata | {"tags":["cn","network-institute","vue","vue-i18n","language"],"image":["https://steemjiang.com:8081/ipfs/QmPPSHAkjjZuzuJVNpL3sQ4dnHrCRL8FXcH5F5tfQcnHde"],"links":["https://kazupon.github.io/vue-i18n/zh/"],"app":"hiveblog/0.1","format":"markdown"} |
created | 2020-12-22 14:16:00 |
last_update | 2020-12-22 14:16:00 |
depth | 0 |
children | 0 |
last_payout | 2020-12-29 14:16:00 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 1.610 HBD |
curator_payout_value | 1.502 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,601 |
author_reputation | 442,111,264,000,381 |
root_title | "多国语言组件vue-i18n / 网络研习社#75" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 101,049,024 |
net_rshares | 17,426,654,788,960 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
lemooljiang | 0 | 41,122,629,275 | 100% | ||
ace108 | 0 | 760,371,126,218 | 26% | ||
rivalhw | 0 | 2,096,346,034,103 | 100% | ||
nextgen622 | 0 | 1,635,796,030,024 | 100% | ||
sweetsssj | 0 | 11,528,733,074,678 | 33% | ||
dapeng | 0 | 43,197,568,754 | 100% | ||
laodr | 0 | 3,603,403,565 | 100% | ||
htliao | 0 | 13,045,069,992 | 35% | ||
alphacore | 0 | 61,063,370,034 | 2.51% | ||
catwomanteresa | 0 | 174,730,015,743 | 50% | ||
liangfengyouren | 0 | 3,388,222,539 | 50% | ||
stevenmosoes | 0 | 546,528,664 | 50% | ||
aafeng | 0 | 179,707,535,461 | 28% | ||
cn-reader | 0 | 15,840,834,550 | 50% | ||
tvb | 0 | 38,981,892,687 | 50% | ||
karja | 0 | 224,872,219,051 | 10% | ||
kimzwarch | 0 | 11,238,656,103 | 4% | ||
yellowbird | 0 | 54,016,001,241 | 100% | ||
dancingapple | 0 | 12,102,109,742 | 50% | ||
blc | 0 | 4,016,321,190 | 100% | ||
gtpjfoodbank | 0 | 30,161,067,597 | 90% | ||
lebin | 0 | 38,862,148,892 | 100% | ||
fun2learn | 0 | 1,397,471,056 | 3% | ||
kymio | 0 | 523,414,373 | 3.75% | ||
competeapp | 0 | 1,095,594,071 | 100% | ||
archisteem | 0 | 1,223,814,295 | 7.5% | ||
dses | 0 | 35,693,375,201 | 70% | ||
moneybaby | 0 | 820,574,703 | 2.5% | ||
julian2013 | 0 | 53,245,579,883 | 50% | ||
fusion.lover | 0 | 26,573,300,500 | 100% | ||
mia-cc | 0 | 12,868,807,609 | 100% | ||
dailyke20 | 0 | 40,850,296,236 | 100% | ||
omnivori | 0 | 1,905,164,530 | 90% | ||
btscn | 0 | 203,823,836,449 | 100% | ||
dbfoodbank | 0 | 3,074,243,835 | 66.5% | ||
weddinggift | 0 | 4,268,309,257 | 100% | ||
hivecur2 | 0 | 28,124,401,445 | 5% | ||
intacto | 0 | 39,424,745,414 | 3% |