语言国际化配置(uniapp为例,针对零基础小白)

44 阅读1分钟
  • 1.在项目里面安装vue-i18n,并在main.js里面引入注册
npm install vue-i18n --save

import VueI18 from "vue-i18n"
Vue.use(VueI18n)


  • 2.新建locale文件夹,建立不同语言的json文件,在该文件夹下的index.js文件里面统一引入并导出(以下是英文json)

{
	"index.name": "oliender mall",
	"index.home": "Home page",
	"index.searchplace": "Enter keywords to search for products",
	"index.ggName": "ANNOUNCEMENT",
	"index.moneyF": "$",
	"index.remaiN": "HOT COMMODITY",
	"index.sold": "sold",
	"index.other": "other",
	"index.more": "See more",
	"index.inventory": "inventory",
	"index.productType": "Product type",
	"index.gggm": "Just purchased",
	"index.zh": "comprehensive",
	"index.xl": "sales",
	"index.jg": "price",
	"index.number": "number",
	"index.addCart": "Add to cart",
	"index.purchase": "Send Order",
	"category.home": "Category page",
	"card.home": "Shopping Cart",
	"index.search": "search",
	"detail.sjhf": "Merchant Message",
	"detail.hpl": "Product review rate",
	"detail.pinglun": "REVIEW",
	"detail.share": "share",
	"detail.total": "TOTAL",
	"detail.xq": "Product Details",
	"detail.kefuName": "Please contact customer service before placing an order",
	"detail.kefuBtnName": "Contact customer service",
	"index.productDetail": "Product Details",
	"my.home": "my",
	"order.copySuccess": "Replicating Success",
	"order.plccopySuccess": "Are you sure you want to copy this text?",
	"order.hg": "REPURCHASE",
	"order.home": "MY ORDER",
	"order.confrim": "Confirm The Order",
	"order.message": "LEAVE A MESSAGE",
	"order.paySuccess": "Payment successful",
	"order.dpingjia": "To be evaluated",
	"order.ypingjia": "Evaluated",
	"order.desc": "Please leave a message telling us the requirements",
	"order.phone": "PAYMENT ACCOUNT NUMBER",
	"order.upload": "UPLOAD PROOF OF PAYMENT",
	"order.uploadText": "Add picture",
	"order.btn": "Send Order",
	"order.tbpaid": "To be paid",
	"card.kong": "The shopping cart is empty",
	"order.all": "All",
	"order.delivery": "Wait for delivery",
	"order.bereceived": "Goods to be received",
	"order.selAddress": "Please select a shipping address",
	"order.selPayName": "Please fill in the payment account",
	"order.selPayImg": "Please upload a payment screenshot",
	"order.detail": "Order Detail",
	"order.num": "Order number",
	"order.closeOrder": "cancellation of order",
	"order.closeOrderText": "Are you sure you want to cancel this order?",
	"order.txfhText": "Merchant has been reminded to ship on time",
	"order.goPJ": "To evaluate",
	"order.refund": "refund",
	"order.refunding": "refunding",
	"order.jjRefund": "Refuse Refund",
	"order.tkyy": "Reason for refund",
	"order.plctkyy": "Please enter the refund reason",
	"order.notkyy": "Refuse Refund Reason",
	"order.Refunded": "Refunded",
	"order.plaRefund": "Refund reason cannot be empty",
	"order.saveRefund": "Are you sure you want to cancel the order and refund it?",
	"order.NoRefund": "Refund has been rejected. If you need to refund again, please contact customer service!",
	"order.time": "Order time",
	"order.status": "Order Status",
	"order.kdName": "Express Picture",
	"order.qrsh": "Confirm receipt",
	"order.ckwl": "View logistics",
	"order.Canceled": "Canceled",
	"order.delete": "Delete Order",
	"order.wxts": "Warm Tips",
	"order.txfh": "Reminder shipment",
	"card.deleteGoods": "Are you sure you want to delete the product",
	"car.sellall": "Select All",
	"car.success": "Submitted successfully",
	"car.selGoods": "Please select a product",
	"car.maxGoods": "The maximum quantity of goods purchased has been reached",
	"card.jrcg": "Successfully added to shopping cart",
	"order.deleteOrder": "Are you sure you want to delete the order?",
	"oder.endReceived": "Are you sure you have received the goods?",
	"my.index": "my",
	"my.msg": "My News",
	"my.address": "My Address",
	"my.feedback": "Feedback",
	"feedback.success": "Feedback successful",
	"feedback.fail": "Feedback failed",
	"my.custom": "Customer Service",
	"my.set": "Setup Center",
	"login.xzgj": "Select a country",
	"login.fail": "Login failed",
	"login.dlsx": "Invalid user information requires re authorization to log in",
	"login.home": "Login",
	"login.name": "Welcome to login",
	"login.text": "Buy what you need",
	"login.phone": "Please enter your account number",
	"login.password": "Please enter password",
	"login.wjmm": "Forgot your password?",
	"login.register": "Register an account",
	"search.hot": "Hot Search",
	"search.history": "Search History",
	"address.default": "Default Address",
	"address.delete": "Delete",
	"order.deleteSuccess": "Successfully deleted",
	"order.zqkcx": "Please select the correct product specification first",
	"order.zqku": "Insufficient inventory, please purchase later",
	"order.kcbz": "Insufficient inventory, please choose another type",
	"order.jrcg": "Joined successfully",
	"address.noAddress": "There is currently no address information available. Please add the address information",
	"address.edit": "Update",
	"address.add": "Add Address",
	"address.home": "Address Management",
	"address.bianji": "Edit Address",
	"address.consignee": "consignee",
	"address.placonsignee": "Please enter the recipient",
	"address.phoneNum": "phone number",
	"address.SelectRegion": "regional information",
	"address.plaSelectRegion": "Please enter address information",
	"address.plcdelete": "Are you sure you want to delete this address information?",
	"address.fulladdress": "full address",
	"address.plafulladdress": "Please enter detailed address",
	"address.setDefault": "Set as default address",
	"message.user": "User Message",
	"message.order": "Order Message",
	"message.home": "Message",
	"feedback.opinios": "Questions and opinions",
	"feedback.plaopinios": "Please provide a detailed description of your question and feedback...",
	"feedback.contact": "contact information",
	"feedback.placontact": "Please enter your contact information",
	"feedback.submit": "Submit",
	"set.agreement": "User Agreement",
	"set.policy": "Privacy Policy",
	"set.about": "About us",
	"set.plaExit": "Exit Reminder",
	"set.Login": "Log out of login",
	"set.savelogin": "Are you sure you want to log out",
	"login.xieyi": "Please agree to the Agreement first",
	"register.home": "register",
	"login.forgot": "forgot password",
	"login.code": "Verification code",
	"login.countryCode": "Please enter the area code or country",
	"login.codeText": "Please enter the verification code",
	"login.fsyzm": "Obtain code",
	"login.yzmcg": "The verification code was successfully sent. Please check it carefully",
	"login.pwd": "Password",
	"login.dxsb": "SMS sending failed",
	"login.czmm": "Password reset successful",
	"login.minPwdText": "Password needs to be greater than 6 digits",
	"login.ty": "agree with",
	"pinglun.haoping": "good",
	"pinglun.goods": "general",
	"pinglun.cha": "Bad",
	"pinglun.fxzn": "Share your user experience",
	"pinglun.plafxzn": "Writing your feelings can help more friends~",
	"pinglun.dj": "Service rating",
	"chat.zxdd": "The order you are consulting about",
	"chat.knzx": "You may want to inquire about this order",
	"chat.xsq": "Hours ago",
	"chat.fzq": "Minutes ago",
	"chat.gg": "just",
	"chat.message": "Please enter the chat content",
	"chat.wljc": "Poor network, please try again later",
	"chat.cxlj": "Connecting or already connected, please do not connect again",
	"chat.ljz": "connecting...",
	"more.more": "Pull up to display more",
	"more.loading": "loading...",
	"more.nomore": "There's no more data left",
	"login.updateName": "Modify nickname",
	"login.oldName": "Original nickname",
	"login.newName": "New nickname",
	"login.plaNewName": "Please set a new nickname",
	"login.tszf": "Contains special characters, please re-enter",

	"pinglun.fcc": "Very dissatisfied",
	"pinglun.bmy": "Dissatisfied",
	"pinglun.ybmy": "general satisfaction",
	"pinglun.my": "satisfied",
	"pinglun.fcmy": "Very satisfied",
	"pinglun.plcMessage": "Comment information cannot be empty",
	"pinglun.plcFFMessage": "The comment contains illegal keywords. Please re-enter",
	"pinglun.pfwk": "Rating cannot be empty",
	"pinglun.plcg": "Comment successful"
}
  • 3.在main.js里面引入每个json文件并初始换语言(一般这里先从本地取,取不到在设置一个默认语言)
import {zh en} from"./locale/index.js"

const message={
   zh,
   en
}

const i18n=new VueI18n({
  locale:"zh",
  message
})

const app=new Vue({
  i18n,
  ...App
})

app.$mount()

  • 4.在页面中使用
<view>{{$t('index.ggName')}}</view>
  • 5.通过$i18n.locale来动态切换语言
this.$i18n.locale = 'en'
  • 6.存储用户语言,防止每次打开不会重置
const language = uni.getStorageSync('language')
uni.setStorageSync('language','en')

const i18n = new VueI18n({ locale: uni.getStorageSync('language') || 'zh'