首页 > 其他分享 >uniapp中使用vue-i18n实现多语言

uniapp中使用vue-i18n实现多语言

时间:2023-09-17 22:15:38浏览次数:35  
标签:uniapp vue zh language index locale i18n import

一 安装vue-i18n

npm i vue-i18n@6

二 添加相关语言配置

 如 en.json:

{
  "locale.auto": "System",
  "locale.en": "English",
  "locale.zh-hans": "简体中文",
  "locale.zh-hant": "繁体中文",
  "locale.ja": "日语",
  "index.title": "Hello i18n",
  "index.home": "Home",
  "index.component": "Component",
  "index.api": "API",
  "index.schema": "Schema",
  "index.demo": "uni-app globalization",
  "index.demo-description": "Include uni-framework, manifest.json, pages.json, tabbar, Page, Component, API, Schema",
  "index.detail": "Detail",
  "index.language": "Language",
  "index.language-info": "Settings",
  "index.system-language": "System language",
  "index.application-language": "Application language",
  "index.language-change-confirm": "Applying this setting will restart the app",
  "api.message": "Message",
  "schema.name": "Name",
  "schema.add": "Add",
  "schema.add-success": "Add success"
}

和 cn.json

{
    "locale.auto": "系统",
    "locale.en": "English",
    "locale.zh-hans": "简体中文",
    "locale.zh-hant": "繁体中文",
    "locale.ja": "日语",
    "index.title": "Hello i18n",
    "index.home": "主页",
    "index.component": "组件",
    "index.api": "API",
    "index.schema": "Schema",
    "index.demo": "uni-app 国际化演示",
    "index.demo-description": "包含 uni-framework、manifest.json、pages.json、tabbar、页面、组件、API、Schema",
    "index.detail": "详情",
    "index.language": "语言",
    "index.language-info": "语言信息",
    "index.system-language": "系统语言",
    "index.application-language": "应用语言",
    "index.language-change-confirm": "应用此设置将重启App",
    "api.message": "提示",
  "schema.name": "姓名",
    "schema.add": "新增",
    "schema.add-success": "新增成功"
}

和index.js:

import en from './en.json'
import zhHans from './zh-Hans.json'
import zhHant from './zh-Hant.json'
import ja from './ja.json'
export default {
    en,
    'zh-Hans': zhHans,
    'zh-Hant': zhHant,
    ja
}

三 在main.js中配置

import App from './App'
import messages from './locale/index'

let i18nConfig = {
  locale: uni.getLocale(),
  messages
}
 

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
  ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
import { createI18n } from 'vue-i18n'
const i18n = createI18n(i18nConfig)
export function createApp() {
  const app = createSSRApp(App)
  app.use(i18n)
  return {
    app
  }
}
// #endif

四 最后在页面中使用多语言,如i18ntest.vue:如下

<template>
  <view class="container">
    <view class="title">{{$t('index.demo')}}</view>
    <view class="description">{{$t('index.demo-description')}}</view>
    <view class="detail-link">{{$t('index.detail')}}: <text
        class="link">https://uniapp.dcloud.net.cn/collocation/i18n</text></view>
    <view class="locale-setting">{{$t('index.language-info')}}</view>
    <view class="list-item">
      <text class="k">{{$t('index.system-language')}}:</text>
      <text class="v">{{systemLocale}}</text>
    </view>
    <view class="list-item">
      <text class="k">{{$t('index.application-language')}}:</text>
      <text class="v">{{applicationLocale}}</text>
    </view>
    <view class="locale-setting">{{$t('index.language')}}</view>
    <view class="locale-list">
      <view class="locale-item" v-for="(item, index) in locales" :key="index" @click="onLocaleChange(item)">
        <text class="text">{{item.text}}</text>
        <text class="icon-check" v-if="item.code == applicationLocale"></text>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        systemLocale: '',
        applicationLocale: ''
      }
    },
    computed:{
      locales() {
        return [{
            text: this.$t('locale.auto'),
            code: 'auto'
          }, {
            text: this.$t('locale.en'),
            code: 'en'
          },
          {
            text: this.$t('locale.zh-hans'),
            code: 'zh-Hans'
          },
          {
            text: this.$t('locale.zh-hant'),
            code: 'zh-Hant'
          },
          {
            text: this.$t('locale.ja'),
            code: 'ja'
          }
        ]
      }
    },
    onl oad() {
      let systemInfo = uni.getSystemInfoSync();
      this.systemLocale = systemInfo.language;
      this.applicationLocale = uni.getLocale();
      this.isAndroid = systemInfo.platform.toLowerCase() === 'android';
      uni.onLocaleChange((e) => {
        this.applicationLocale = e.locale;
      })
    },
    methods: {
      onLocaleChange(e) {
          uni.setLocale(e.code);
          this.$i18n.locale = e.code;
      }
    }
  }
</script>

<style>
  .title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 15px;
  }

  .description {
    font-size: 14px;
    opacity: 0.6;
    margin-bottom: 15px;
  }

  .detail-link {
    font-size: 14px;
    word-break: break-all;
  }

  .link {
    color: #007AFF;
    margin-left: 10px;
  }

  .locale-setting {
    font-size: 16px;
    font-weight: bold;
    margin-top: 25px;
    margin-bottom: 5px;
    padding-bottom: 5px;
    border-bottom: 1px solid #f0f0f0;
  }

  .list-item {
    font-size: 14px;
    padding: 10px 0;
  }

  .list-item .v {
    margin-left: 5px;
  }

  .locale-item {
    display: flex;
    flex-direction: row;
    padding: 10px 0;
  }

  .locale-item .text {
    flex: 1;
  }

  .icon-check {
    margin-right: 5px;
    border: 2px solid #007aff;
    border-left: 0;
    border-top: 0;
    height: 12px;
    width: 6px;
    transform-origin: center;
    /* #ifndef APP-NVUE */
    transition: all 0.3s;
    /* #endif */
    transform: rotate(45deg);
  }
</style>

  参考了插件hello-i18n。

标签:uniapp,vue,zh,language,index,locale,i18n,import
From: https://www.cnblogs.com/Insist-Y/p/17709927.html

相关文章

  • Vue js 框下制作登录页面的新方法
    ......
  • Vue mavon-editor 本地加载 – 关闭 CDN
    ​ 转载自Vuemavon-editor本地加载–关闭CDN-前端教程。仅自用。时间2022-03-3121:07:09前言在Vue里面使用Markdown编辑器的选择并不多。mavon-editor大概是GitHub上星星最多的VueMarkdown编辑器了,用起来也比较方便。但是由于mavon-editor默认使用Clo......
  • 如何在Vue组件中定义方法
    在Vue组件中定义方法,你可以按照以下步骤进行:在Vue组件的methods选项中定义方法。methods:{methodName(){//方法的具体逻辑},anotherMethod(){//另一个方法的逻辑}}在上述示例中,使用methods选项来定义了两个方法:methodName和anotherMethod。......
  • 关于vue2的模块级总结
    前阵子在赶一个项目的进度,一直没时间做总结,今日闲来无事,消化一下。背景vue2的项目,面向受众为g端内容1.项目原因,单路由下包含详情&列表两页面。根据v-if跳转,笔者这里用的是动态组件的方式2.同样由于项目原因,使用的模块级vuex,因而在使用时,也有了许多盲点:(如图:)使用createNa......
  • uniapp项目实践总结(十六)自定义下拉刷新组件
    导语:在日常的开发过程中,我们经常遇到下拉刷新的场景,很方便的刷新游览的内容,在此我也实现了一个下拉刷新的自定义组件。目录准备工作原理分析组件实现实战演练内置刷新案例展示准备工作在components新建一个q-pull文件夹,并新建一个q-pull.vue的组件;按照前面文章所说......
  • uniApp 打包H5工程
    打开这个项目,选择“发行”-->"网站-PCWeb或手机H5(仅适用于uniapp)(H)";页面中间出现一个弹窗,直接点击绿色的按钮“发行”;在底部的控制台显示项目编译成功,编译好路径在控制台的里面;打开本地路径,把h5文件夹复制出来就是打包好的h5项目,后台把这个包上传到服务器就完成了......
  • vue3中验证手机号的正则表达式
    在Vue3中,你可以使用正则表达式来验证手机号。以下是一个基本的手机号验证正则表达式示例,可以用于检查中国大陆地区的手机号码:constphoneNumberRegex=/^1[3456789]\d{9}$/;//示例用法constphoneNumber="13812345678";if(phoneNumberRegex.test(phoneNumber)){cons......
  • vue项目中的Tinymce富文本编辑器如何从word中粘贴图片上传到七牛云
    Tinymce富文本编辑器粘贴图片时需要上传到自己的空间中才能被打开。一、首先需要安装引入七牛云npminstallqiniu-jsvarqiniu=require('qiniu-js')//orimport*asqiniufrom'qiniu-js'二、同时引入客户端生成的tokenimport{qiniuTokenCreate}from"@/assets/js/qin......
  • Vue学习六:路由进阶
    一、路由的封装抽离目标:将路由模块抽离出来。好处:拆分模块,易于维护。第一步:在src目录下新建一个router目录,在创建一个index.js文件,将先前main.js中的路由代码转移到index.js文件中。(这里需要使用到vue所以需将vue包导入;需修改组件路径,@符号代表绝对路径src;需将路由实例导出)index......
  • 报错error Component name "Index" should always be multi-word vue/multi-word-co
    1、问题说明:在创建组件命名时,引用index.vue的过程中报错;2、报错的原因及分析:其一、报错的全称为:errorComponentname"index"shouldalwaysbemulti-wordvue/multi-word-component-names翻译为:错误组件名称“索引”应始终为多词vue/多词组件名称其二、问题分析:新手在使用......