i18n 在.vue/.js 的使用

vue-i8n  在后台或前台项目中常常被用到

在此处做个记录,帮助别人,同时也让自己加深一遍印象

下载i8n  这在https://www.npmjs.com/中能查到

npm i vue-i18n

 重点在于i18n的使用,在此我先粘贴代码,后续放讲解

main.js文件

import { createApp } from "vue";

import App from "./App.vue";
import i18n from "./locale";


const app = createApp(App);
app.use(i18n);
app.mount("#app");

src文件下的locale文件,是我用来创建i18n和存储本地用到的多语言

local文件夹下的index.js

import { createI18n } from "vue-i18n";

import en from "./modules/en";
import cn from "./modules/zh";

export const LOCALE_OPTIONS = [
  { label: "中文", value: "zh-CN" },
  { label: "English", value: "en-US" },
];

const defaultLocale = localStorage.getItem("locale") || "en-US";

const i18n = createI18n({
  locale: defaultLocale,
  legacy: false,
  allowComposition: true,
  messages: {
    "en-US": en,
    "zh-CN": cn,
  },
});
export default i18n;

local文件夹下的modules文件夹存放英文和中文语言包

modules下的en.js



export default {
  home:'Home'
};

modules下的zh.js

export default{
    home:'首页'
}

到此,基础的配置已经结束,我这里只配了一个home首页来测试中英文切换

在vue文件中的使用

<template>
<div>{{$t("home")}}</div>
</template>

在js文件中的使用,主要是使用i8n 的t函数,以下展示两种方式

方式1:

import {useI18n} from "vue-i18n"

const i18 = useI18n()  //error

// 如果直接在外部环境使用的话,会提示i8n必须在setup语法中,所以可以创建一个方法

const useI18nFn = ()=>{
    const i18 = useI18n()
    console.log(i18n.t("home")) // t函数
    console.log(i18n.local.value) // 当前的语言环境
}

方式2:

// 因为我注册i8n 是在  local文件夹下的index.js中

// 所以在这里使用,要先导入

import i18n from "@/local"

// 在这里就可以随便取用,不过要注意t函数所在的位置

console.log(i18n.global.t("home"))

//获取当前的语言环境

console.log(i8n.global.local.value)

以上就是配置i18n,以及在js和vue文件中的使用

特别注意,一定不要忘记导入组件的语言包,如果使用element-plus,那么就可以如下导入

<template>
  <el-config-provider :locale="locale">
    <router-view></router-view>
  </el-config-provider>
</template>

<script setup>
import { computed } from "vue";


import useLocale from "@/hooks/locale";

import zhCn from "element-plus/es/locale/lang/zh-cn";
import en from "element-plus/es/locale/lang/zh-cn";
import zhTw from "element-plus/es/locale/lang/zh-tw";
import ja from "element-plus/es/locale/lang/ja";
const { currentLocale } = useLocale();

const locale = computed(() => {
  switch (currentLocale.value) {
    case "zh-CN":
      return zhCn;
    case "en-US":
      return en;
    case "zh-TW":
      return zhTw;
    case "ja-ri":
      return ja;
    default:
      return en;
  }
});

</script>

如果使用vant 可以如下导入

<template>
  <a-config-provider :locale="locale">
    <RouterView />
  </a-config-provider>
</template>

<script lang="ts" setup>
import { computed } from "vue";
import enUS from "@arco-design/web-vue/es/locale/lang/en-us";
import zhCN from "@arco-design/web-vue/es/locale/lang/zh-cn";
import useLocale from "@/hooks/locale";

const { currentLocale } = useLocale();

const locale = computed(() => {
  switch (currentLocale.value) {
    case "zh-CN":
      return zhCN;
    case "en-US":
      return enUS;
    default:
      return enUS;
  }
});

</script>

 createI18n({
  locale: defaultLocale,
  legacy: false,
  allowComposition: true,
  messages: {
    "en-US": en,
    "zh-CN": cn,
  },
});

讲解一下这块,local :本地语言环境   message :数据源  legacy :默认是ture 是否启用或禁用遗留模式

allowComposition 配合legacy 遗留模式使用

源码中显示

If you specified `allowComposition: true` option in Legacy API mode, return `true`, else `false`. else you use the Composition API mode, this property will always return `true`.

翻译:

如果你在遗留API模式下指定了‘ allowComposition: true ’选项,返回‘ true ’,否则返回‘ false ’。否则你使用复合API模式,这个属性将总是返回“true”。

这种情况下可以合并了吗? git status On branch lqn/feat/aio-v1.0-device All conflicts fixed but you are still merging. (use "git commit" to conclude merge) Changes to be committed: modified: ../../../../../.env modified: ../../../../../omada-central-env.config.js modified: ../../../../../package.json modified: ../../../../../pnpm-lock.yaml modified: ../../../../../rsbuild.config.ts modified: ../../../../api/auth.ts modified: ../../../../api/video.ts modified: ../../../../assets/icons/package-near-expire.svg modified: ../../../../components/AppAiFreeTrial.vue modified: ../../../../components/eptz/Index.vue modified: ../../../../components/live/PreviewPlayer.vue modified: ../../../../components/live/use-live-tool.ts modified: ../../../../components/navigator/Index.vue new file: ../../../../config-env/common-util.ts modified: ../../../../config-env/index.ts new file: ../../../../config-env/log/index.ts new file: ../../../../config-env/nav/index.ts modified: ../../../../config-env/system-settings/index.ts modified: ../../../../config-env/user/index.ts modified: ../../../../config-env/video/index.ts modified: ../../../../constants/custom-role.ts new file: ../../../../constants/errors/device.ts new file: ../../../../constants/errors/index.ts new file: ../../../../constants/errors/player.ts modified: ../../../../constants/navigator.ts modified: ../../../../hooks/use-preview.ts modified: ../../../../i18n/locales/en_US.json modified: ../../../../index.ts modified: ../../../../player/interface.ts modified: ../../../../player/media-player.ts modified: ../../../../player/plugins/instances/timeline/TimeSpanSlider.vue modified: ../../../../player/preview/preview-player.ts new file: ../../../../player/stream/base.ts new file: ../../../../player/stream/direct-stream.ts new file: ../../../../player/stream/https-relay-stream.ts new file: ../../../../player/stream/index.ts new file: ../../../../player/stream/local-media-stream.ts new file: ../../../../player/stream/multipart-conversion.ts new file: ../../../../player/stream/p2p-stream.ts new file: ../../../../player/stream/racing-stream.ts new file: ../../../../player/stream/relay-stream-base.ts new file: ../../../../player/stream/relay-stream.ts modified: ../../../../store/modules/auth.ts modified: ../../../../styles/application/var.less modified: ../../../../utils/device-pre-connect.ts modified: ../../../../utils/rsa-encode.ts modified: ../../../../utils/video.ts modified: ../../../account-settings/uid-account/Index.vue modified: ../../../auth/login-redirect/uid-cloud-redirect/Index.vue modified: ../../Index.vue modified: ../../ai-search/face/hooks/use-face.ts modified: ../../ai-search/hooks/use-first-empty-guide.ts modified: ../../ai-search/hooks/utils.ts modified: ../../ai-search/human/const.ts modified: ../../ai-search/human/hooks/use-human.ts modified: ../../ai-search/vehicle/hooks/use-vehicle.ts modified: ../../alarm/events/components/event-menu/Recipients.vue modified: ../../alarm/events/custom-alerts/custom-alerts-content/Index.vue modified: ../../alarm/events/device-event/Index.vue modified: ../../billing-account/order-history/Index.vue modified: ../../billing-account/order-history/hooks/use-order.ts modified: ../../components/video-batch-select/Index.vue modified: ../../dashboard/line-chart/hooks/use-line.ts modified: ../components/delete-device-modal/delete-aio-device/Index.vue modified: ../components/delete-device-modal/delete-device-ability.ts modified: ../components/device.less modified: ../components/edit-device-nvr/Index.vue modified: ../components/edit-device-nvr/alarm-info/hotzone-detection/use-device.ts modified: ../components/edit-device-nvr/alarm-info/pet-detection/Index.vue modified: ../components/edit-device-nvr/alarm-info/pet-detection/use-pet-detection.ts modified: ../components/edit-device-nvr/camera-info/device-access/use-table.ts modified: ../components/edit-device-nvr/storage-info/array-management/AddHarddriveModal.vue modified: ../components/edit-device-nvr/storage-info/array-management/CreateRAIDModal.vue modified: ../components/edit-device-nvr/storage-info/array-management/CreateRAIDOnceModal.vue modified: ../components/edit-device-nvr/storage-info/array-management/EditRAIDModal.vue modified: ../components/edit-device-nvr/storage-info/array-management/Index.vue modified: ../components/edit-device-nvr/storage-info/recording-manager/hooks/constants.ts modified: ../components/edit-device-nvr/storage-info/recording-manager/hooks/use-columns.tsx modified: ../components/edit-device-nvr/storage-info/storage-manager/components/storage-status/Index.vue modified: ../components/edit-device-nvr/storage-info/storage-manager/components/storage-status/hooks/use-storage-status.ts modified: ../components/edit-device/Index.vue modified: ../components/edit-device/cloud-storage/Index.vue modified: ../components/edit-device/network-settings/ddns/Index.vue modified: ../components/edit-device/network-settings/ddns/hooks/useAction.ts modified: ../components/edit-device/smart-analysis/people-counting/use-device.ts modified: ../components/edit-device/smart-event/pet-detection/Index.vue modified: ../components/edit-device/smart-event/pet-detection/use-pet-detection.ts modified: ../components/edit-device/smart-event/queue-detection/use-queue-detection.ts modified: ../components/edit-device/system-manager/capture-management/Index.vue modified: ../components/edit-device/system-manager/certificate-management/Index.vue modified: ../components/edit-solar-drawer/Index.vue modified: ../components/information-display/components/IpcInformation.vue modified: ../components/local-upgrade/Index.vue modified: Index.vue modified: components/TableFilter.vue modified: ../hooks/use-columns-cloud.tsx modified: ../hooks/use-columns-local.tsx modified: ../../license/license-manage/Index.vue modified: ../../log/components/SystemTable.vue modified: ../../log/hooks/system-table/use-log.ts modified: ../../map/device-map/components/LiveViewList.vue modified: ../../organization/hooks/index.ts deleted: ../../organization/hooks/use-actions.ts modified: ../../passenger-flow/components/passenger-flow-quick-settings/components/SingleLiveView.vue modified: ../../plan/Index.vue modified: ../../rules/event-rules/Index.vue modified: ../../rules/event-rules/hooks/use-actions.tsx modified: ../../rules/event-rules/hooks/use-event-rule.tsx modified: ../../system-setting/Index.vue modified: ../../user/ReinviteIcon.vue modified: ../../video/video-common/VideoToolTip.vue modified: ../../video/video-common/use-get-videoInfo.ts modified: ../../video/video-surveillance/component/time-span/Index.vue modified: ../../video/video-surveillance/hooks/utils.ts modified: ../../video/video-surveillance/live-view/Index.vue modified: ../../video/video-surveillance/live-view/hooks/preview-port.ts modified: ../../video/video-surveillance/live-view/plugins/ratio.vue modified: ../../video/video-surveillance/playback/Playback.vue modified: ../../video/video-surveillance/playback/event/hook/use-image-cloud.ts modified: ../../video/video-surveillance/playback/main/const/playback-player.ts new file: ../../video/video-surveillance/playback/main/const/retry-white-list.ts modified: ../../video/video-surveillance/playback/main/error-hander/use-playback-error.ts modified: ../../video/video-surveillance/playback/main/playback-manage.ts modified: ../../video/video-surveillance/playback/main/playback-player/device-player.ts modified: ../../video/video-surveillance/playback/main/playback-player/hooks/use-decryption.ts Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) deleted: ../components/add-device-modal/transfer/transfer.d.ts Untracked files: (use "git add <file>..." to include in what will be committed) ../../../../../dist.7z
最新发布
11-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值