nuxt使用i18n进行中英文切换

这篇具有很好参考价值的文章主要介绍了nuxt使用i18n进行中英文切换。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

中文效果图:

nuxt使用i18n进行中英文切换,vue.js,前端,javascript

英文效果图:

nuxt使用i18n进行中英文切换,vue.js,前端,javascript

版本:

nuxt使用i18n进行中英文切换,vue.js,前端,javascript

安装:

 npm install --save @nuxtjs/i18n

 新建en.js与zh.js两个文件进行切换显示

nuxt使用i18n进行中英文切换,vue.js,前端,javascript

en.js内容 

import globals from './../js/global_val'

export default {
  /******
   * 公共内容开始
   * *****/
  seeMore: "More Information", //查看更多
  seeAgenda: "View the conference schedule", //查看议程
  jtlRegister: 'Register for the Golden Gyro Award', //金陀螺奖报名参评/投票
  registrationChannel: 'FBEC Registration channel', //大会参会报名通道
  fbecland: 'FBEC LAND', //大会官方微站
  application: 'Click to register for the selection', //点击报名参评-pc
  applicationOver: 'The entry channel is closed', //参评已结束-pc
  mApplication: 'Click Register to vote', //点击报名参评/投票-手机端
  mAplicationOver: 'Registration is closed', //点击报名参评/投票-手机端
  headerData: [{
      name: "Main page",
      url: "/"
    },
    {
      name: "Main-venue ",
      url: "main-venue"
    },
    {
      name: "Sub-venue",
      url: "branch-venue"
    },
    {
      name: "Golden Gyro Award",
      url: "golden-top-award"
    },
    {
      name: "Conference Exhibition Zone",
      url: "conference-exhibition-area"
    },
    {
      name: "More Exciting Content",
      url: "more-exciting-content"
    },
    {
      name: "Conference dynamics",
      url: "conference-dynamics"
    },
    {
      name: "Review of previous Conference",
      url: ""
    },
  ],
  
  /******
   * 公共内容结束
   * *****/
  /******
   * 首页开始
   * *****/

  asideNav: [{
      label: "Conference  Main Keyvisual",
      id: "zhuk",
    },
    {
      label: "FBEC2023",
      id: "2022",
    },
    {
      label: "Conference Agenda",
      id: "yicheng",
    },
    {
      label: "Conference Guests",
      id: "jiabin",
    },
    // {
    //   label: "大会展商",
    //   id: "zhanshang",
    // },
    {
      label: "Great Video",
      id: "shipin",
    },
    {
      label: "Conference Distribution Map",
      id: "fenbutu",
    },
    {
      label: "Organization",
      id: "zhuzhi",
    },
    {
      label: "Conference Media",
      id: "meiti",
    },
    {
      label: "Conference Coverage",
      id: "baodao",
    },
    {
      label: "Conference Photo",
      id: "huigu",
    },
  ], //首页时间轴
  part1: {
    time: 'Sheraton Shenzhen Futian Hotel · 8th Dec 2023',
    des1: 'In2023, we have experienced the booming development of artificial intelligence in the early stage, and the rapid evolution of AI technology has not only multiplied the innovation of business models, but also given rise to market changes. When the market bubble recedes and the new wave of the AI era surges, we stick to our original intention, explore the future development pattern of science and technology and the Internet, and plan for the future value of new technology, new business, and new models. We insist that the only way to make progress is to maintain indomitable resilience and vitality. It is only when we work together that we can go farther.',
    des2: 'The Future Business Ecology Link Conference 2023 and the 8th Golden Gyro Award Ceremony (“FBEC2023”) is organized by Gyro Technology, with the theme of “Collaboration Brings Mutual Benefits·Perseverance Leads to Success”, taking the perspective of “navigators” with industry foresight and the main line of “ice-breaking” journey, focusing on the frontier fields of Internet such as AI、metaverse、 XR、games、e-sports and digital economy, presenting the frontier achievements of science and technology, discussing the times and business issues, planning the future value of new technology, new business and new models, and going on an ice-breaking journey with the real courageous people in the winter under the drastic changes and innovations!'
  }, //首页大会简介

  

}

zh.js内容:

// 首页
// const globals =  require('./js/global_val.js')
import globals from './../js/global_val'

export default {
  /******
   * 公共内容开始
   * *****/
  seeMore: "查看更多", //查看更多
  seeAgenda: "查看议程", //查看议程
  jtlRegister: '金陀螺奖报名参评/投票', //金陀螺奖报名参评/投票
  registrationChannel: '大会参会报名通道', //大会参会报名通道
  fbecland: '大会互动微站 FBEC LAND', //大会官方微站
  application: '点击报名参评', //点击报名参评-pc
  applicationOver: '参评已结束', //参评已结束-pc
  mApplication: '点击报名参评/投票', //点击报名参评/投票-手机端
  mAplicationOver: '点击报名参评/投票', //点击报名参评/投票-手机端
  headerData: [{
      name: "首页",
      url: "/"
    },
    {
      name: "主会场",
      url: "main-venue"
    },
    {
      name: "分会场",
      url: "branch-venue"
    },
    {
      name: "金陀螺奖",
      url: "golden-top-award"
    },
    {
      name: "大会展区",
      url: "conference-exhibition-area"
    },
    {
      name: "更多精彩",
      url: "more-exciting-content"
    },
    {
      name: "大会动态",
      url: "conference-dynamics"
    },
    {
      name: "往届回顾",
      url: ""
    },
  ],
  
  /******
   * 公共内容结束
   * *****/
  /******
   * 首页开始
   * *****/
  asideNav: [{
      label: "大会主K",
      id: "zhuk",
    },
    {
      label: "FBEC2023",
      id: "2022",
    },
    {
      label: "大会议程",
      id: "yicheng",
    },
    {
      label: "大会嘉宾",
      id: "jiabin",
    },
    // {
    //   label: "大会展商",
    //   id: "zhanshang",
    // },
    {
      label: "精彩视频",
      id: "shipin",
    },
    {
      label: "会场分布图",
      id: "fenbutu",
    },
    {
      label: "组织机构",
      id: "zhuzhi",
    },
    {
      label: "合作媒体",
      id: "meiti",
    },
    {
      label: "大会报道",
      id: "baodao",
    },
    {
      label: "精彩现场",
      id: "huigu",
    },
  ], //首页时间轴
  part1: {
    time: '深圳·大中华喜来登酒店 2023/12/08',
    des1: '2023年,我们亲历了人工智能的蓬勃发展,AI技术的飞速演进,不仅加乘了商业模式的创新,也催生了市场变化。当市场泡沫退去,AI时代的新浪潮涌动,我们坚守初心,对未来互联网发展形态进行探索,对新科技、新商业、新模式未来价值进行谋划,我们坚信唯有保持百折不挠的韧性与生命力,不断进化自身以适应市场环境变化,共荣共生,方能韧行致远。',
    des2: '2023未来商业生态链接大会暨第八届金陀螺奖颁奖典礼(简称“FBEC2023”)由陀螺科技主办,以“合力共生 · 韧者行远”为大会主题,以具有行业前瞻洞察的“航行者”为视角,破冰之旅为主线,关注AI、元宇宙、XR、游戏、电竞、数字经济等科技与互联网前沿领域,全方位呈现科技前沿成果,聚焦时代与商业热点议题,探讨新科技、新商业、新模式的未来价值,与真正的勇者共赴剧变革新下的凛冬破冰之旅!'
  }, //首页大会简介

}

在plugins下新建i18n.js

nuxt使用i18n进行中英文切换,vue.js,前端,javascript

i18n.js内容:

import zh_cn from '../assets/lang/zh.js'
import en_us from '../assets/lang/en.js'
const i18n = {
  locales: ['en', 'zh'], //有多少地区的语言就添加多少种
  defaultLocale: 'zh', //默认的地区语言
  vueI18n: {
    fallbackLocale: 'en', //回退策略,指定的locale中没有找到对应资源的情况下使用的locale
    messages: { //要渲染的信息,有多少语言就添加多少种
      en:en_us,
      zh:zh_cn
    }
  }

}

export default i18n

 在nuxt.config.js引入i18n.js

import i18n from './plugins/i18n'
export default {
  modules: ['@nuxtjs/axios',['@nuxtjs/i18n',i18n]
  ],
}

切换按钮(两种切换方式选择一种即可

<!--方式一 -->
      <div class="change-languag-box">
          <nuxt-link
            class="change-language cn-icon"
            v-if="$i18n.locale !== 'en'"
            :to="switchLocalePath('en')"
          >中文</nuxt-link>
          <nuxt-link
            class="change-language en-icon"
            v-if="$i18n.locale !== 'zh'"
            :to="switchLocalePath('zh')"
          >英文</nuxt-link>
        </div>
<!--方式二 -->
<!--html -->
        <div class="change-languag-box">
     
          <div
            class="change-language cn-icon"
            v-if="$i18n.locale !== 'en'"
            @click="changeLocale('en')"
          ></div>
          <div
            class="change-language en-icon"
            v-if="$i18n.locale !== 'zh'"
            @click="changeLocale('zh')"
          ></div>
        </div>
<!--js -->
  changeLocale(locale) {
      // 在页面中添加 changeLocale 方法,调用 $i18n.setLocale 方法来切换语言
      this.$i18n.setLocale(locale);
    },

页面展示

<template>
  <!-- <client-only></client-only>加这个刷新页面才不会显示异常 -->
  <client-only>

        <!--内容 -->
      <div class="des">
          <div
            class="time"
            :class="$i18n.locale == 'en' ? 'time-en' : ''"
            v-html="$t('part1.time')"
          ></div>
          <div class="detail" v-html="$t('part1.des1')"></div>
          <div class="hr default-bg"></div>
          <div class="detail">{{$t('part1.des2')}}</div>
        </div>
  </client-only>
</template>

有导航的需要在导航切换的时候处理一下相关内容:文章来源地址https://www.toymoban.com/news/detail-722053.html

  <!-- html(headerData为导航内容) --> 
<div class="com-header-cont flex-center-start">
          <div
            class="com-header-item"
            v-for="(tabItem, tabIdx) in $t('headerData')"
            :key="tabIdx"
            @click="changeHeader(tabIdx, tabItem.url)"
            :class="[
              tabIdx == headerIdx ? 'headerActive' : '',
              $i18n.locale == 'en' ? 'com-header-item-en' : '',
            ]"
          >
            <span :class="{ dropdownBtn: tabIdx == 7 }" @click="dropdown">{{
              tabItem.name
            }}</span>
            <ul
              v-if="tabIdx == 7"
              class="dropdown-wrapper"
              v-show="dropdownActive"
            >
              <li
                class="dropdown-wrapper-li"
                v-for="(item, index) in dropParams"
                :key="index"
                @click="closeDropdown"
              >
                <a target="_blank" :href="item.path">{{ item.name }}</a>
              </li>
            </ul>
          </div>
        </div>
 <!-- js --> 
  // 导航切换
    changeHeader(index, path) {
      this.headerIdx = index;
      
      // 默认中文,所以this.$i18n.locale == "zh"时走正常的路由跳转
      if (this.$i18n.locale == "zh") {
        this.$router.push({ path: path });
      } else {
        if (process.client) {
          let routerPath = this.$route.fullPath;
          // 如果路由中没有包含/en/就添加,如果有则走正常的路由跳转
          if (routerPath.indexOf("/en/") == -1) {
            this.$router.push({ path: `${this.$i18n.locale}/${path}` });
          } else {
            this.$router.push({ path: path });
          }
        }
      }
    },

到了这里,关于nuxt使用i18n进行中英文切换的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring之国际化:i18n

    学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。各位小伙伴,如果您: 想系统/深入学习某技术知识点… 一个人摸索学习很难坚持,想组团高效学习… 想写博客但无从下手,急需写作干货注入能量… 热爱写作,愿意让自己成为更好

    2024年02月03日
    浏览(48)
  • i18n多国语言Internationalization的实现

    i18n 是\\\"Internationalization”的缩写,这个术语来源于英文单词中首尾字母“”和“n”以及中间的字符数(共计18个字符) 当我们需要开发不同语言版本时,就可以使用i18n多国语言的一个操作处理,i18n主要实现那一方面的内容呢?例如: 文本内容本地化、日期/时间格式、货币与度

    2024年01月16日
    浏览(31)
  • i18n(国际化)代码简单实现

    各个国家都有各个国家的语言,如果网站需要让全世界的人使用,那就需要进行国际化功能开发 国际化我知道的一共有两种,其中一种是不同国家不同网站,也就是说页面风格都不一样。另外一种是网站都是一样的,只是里面的文字不同罢了。第一种没啥好说了,毕竟都是两

    2024年02月07日
    浏览(49)
  • vue3 i18n配置详细教学

    npm install vue-i18n@next @next 表示安装i18n 最新的 vue3的版本 点击按钮切换语言 

    2024年02月16日
    浏览(29)
  • Next实现 i18n 传递 locales 给 getStaticPaths

    在 Next.js 中实现国际化( i18n )时,可以通过配置 next.config.js 文件来传递 locales 给 getStaticPaths 函数。下面是一个示例代码,演示如何在 next.config.js 中配置 locales ,并在 getStaticPaths 中获取并使用这些 locales : 1、配置 next.config.js 文件: 2、在页面组件中使用 getStaticPaths : 在

    2024年04月24日
    浏览(23)
  • 【angular】实现简单的angular国际化(i18n)

    实现简单的angular国际化。本博客实现中文版和法语版。 将 Hello i18n! 变为 中文版:你好 i18n! 或 法语版:Bonjour l’i18n ! 。 创建一个项目: 在集成终端中打开。 添加本地化包: 在html中添加格式化标识: 现在运行一下,页面是: 生成翻译模板语言包: 生成了一个文件夹: l

    2024年02月08日
    浏览(41)
  • SpringBoot - SpringBoot整合i18n实现消息国际化

    在我们开发WEB项目的时候,项目可能涉及到在国外部署或者应用,也有可能会有国外的用户对项目进行访问,那么在这种项目中, 为客户展现的页面或者操作的信息就需要根据客户系统的环境来使用不同的语言,这就是我们所说的项目国际化。 1. MessageSource源码 Spring中定义了

    2024年02月03日
    浏览(41)
  • 解决pinia中的i18n切换语言不生效的问题

    在我的项目中,使用i18n切换语言后,会进行 router.push 来刷新页面。 但我发现写在store中的选项(我把它们用作下拉框组件的 options ,例如 options=\\\"store.statusOption\\\" ),却并没有切换语言。它们需要我手动刷新页面后才能够切换语言。然而其它组件中的语言切换都很正常。 个人

    2024年02月13日
    浏览(26)
  • spring6-国际化:i18n | 数据校验:Validation

    1.1、i18n概述 国际化也称作i18n,其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数。由于软件发行可能面向多个国家,对于不同国家的用户,软件显示不同语言的过程就是国际化。通常来讲,软件中的国际化是通过配置文件来实现的,假设要支撑两种语言,

    2024年02月08日
    浏览(38)
  • 如何在Vue3中配置国际化语言i18n

    1. 安装 vue-i18n 2. 创建一个i8n的配置文件 如:i18nConfig.js 3. 新建语言文件 zh-CN.js 和 en-US.js zh-CN.js 文件 en-US.js 文件 CONFIG.js 文件 4. 在 main.js 里面全局配置 通过上面四步即可配置完毕 下面说一下如何使用,分三种情况 在 .vue 组件中的 template 使用 在 .vue 组件中的 script 中使用 在

    2024年02月09日
    浏览(46)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包