soon-i18n-vue
安装
npm install soon-i18n-vue
完整示例
soon-admin后台管理系统 soon-admin-vue
或者
npx degit https://github.com/leafio/soon-i18n/packages/soon-i18n-vue/demo
使用说明
创建实例
import { createI18n } from "soon-i18n-vue";const global_locales = { zh: { g_welcome: "全局:欢迎 {name}" }, en: { g_welcome: "Global: Welcome {name}" },} as const;type Lang = "zh" | "en";export const { tLocales, lang } = createI18n( { lang: "zh" as Lang, fallbacks: ["en"] }, global_locales);
在 js/ts 中使用
import { tLocales } from "../lang";export const showToast = () => { const t = tLocales({ zh: { tip: "哈哈,一条中文提醒!!!" }, en: { tip: "Aha, an English tip" }, }); alert(t("tip"));};
在组件中使用
<template> <div>{{ t("hello") }}</div></template>
<script setup>import { tLocales } from "../lang";const t = tLocales({ zh: { hello: "你好" }, en: { hello: "Hello" },});</script>
切换语言
<template> <button @click="handleToggle">{{ lang }}</button></template>
<script setup>import { lang } from "../lang";
const handleToggle = () => { lang.value = lang.value === "zh" ? "en" : "zh";};</script>