Nuxt3国际化配置指南

本篇专栏基于笔者的Vue专栏,Nuxt3系列,如有不连贯之处可查看Nuxt3系列其他文章!特此说明

一、安装插件

二、配置插件

  • 根目录创建lang/目录,作为多语言目录;
// nuxt.config.ts
export default defineNuxtConfig({
  ...
  modules: [
    [
      '@nuxtjs/i18n',
      {
        baseUrl: "", // 正式环境域名
        locales: ["zh", "ja", "en"],
        defaultLocale: "zh",

        vueI18n: "i18n.config.ts",
      },
    ],
  ],
  ...
});
// i18n.config.ts
import zh from "@/lang/zh/index";
import ja from "@/lang/ja/index";
import en from "@/lang/en/index";

export default defineI18nConfig(() => ({
  legacy: false, // 是否兼容之前
  locale: "zh",
  fallbackLocale: 'zh', // 兼容不到就用en
  messages: { zh, ja, en },
}));

三、使用示例

3.1 - 切换多语言

<ul class="lang-switch-wrap">
  <li v-for="(item, ii) in locals" :key="`header-nav-local-${ii}`">
    <NuxtLink :to="switchLocalePath(item)">{{ $t(item) }}</NuxtLink>
  </li>
</ul>

<script setup lang="ts">
const locals = ["zh", "ja", "en"];
const switchLocalePath = useSwitchLocalePath();
</script>

3.2 - setup中获取i8n

const i18n = useI18n();

console.log("当前的语言", i18n.locale.value);

四、多语言Mobile站适配

场景:PC端和移动端在同一个项目,且ui差异较大,使用传统m站去做

具体说明请查看笔者的《Nuxt3适配指南》

export default defineEventHandler(async (event) => {
    // 服务端

    if (event.path.indexOf("images") == -1) {
        const cookies = parseCookies(event);

        // 是否是移动端设备
        const isMobile = /(Android|webOS|iPhone|iPod|tablet|BlackBerry|Mobile)/i.test(<string>event.headers.get("user-agent"));
        // 是否是手机端路由
        const isRouterMobile = event.path.indexOf("/mobile") == -1 ? false : true;

        if (isMobile && !isRouterMobile) {
            let url = event.path.replace("/zh", "").replace("/en", "");
            console.log("mobile route", event.path, url);
            if (!cookies["i18n_redirected"] || cookies["i18n_redirected"] == "ja") {
                await sendRedirect(event, `/mobile${url}`, 302);
            } else {
                await sendRedirect(event, `/${cookies["i18n_redirected"]}/mobile${url}`, 302);
            }
            return
        }
        if (!isMobile && isRouterMobile) {
            let url = event.path.replace("/zh", "").replace("/en", "").replace("/mobile", "");
            console.log("pc route", event.path, url);
            if (!cookies["i18n_redirected"] || cookies["i18n_redirected"] == "ja") {
                await sendRedirect(event, `${url}`, 302);
            } else {
                await sendRedirect(event, `/${cookies["i18n_redirected"]}${url}`, 302);
            }
        }
        if (!cookies["i18n_redirected"]) {
            let url = event.path.replace("/zh", "").replace("/en", "").replace("/mobile", "");
            setCookie(event, "i18n_redirected", "ja");
            await sendRedirect(event, `${url}`, 302);
            return
        }
    }
});
  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梨花炖海棠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值