zoukankan      html  css  js  c++  java
  • 使用 nuxt 开发网站 之 如何做国际化?

      在页面画的差不多的时候,我就开始查一查如何国际化,并对一些静态的文字做国际化处理,接口请求时传参处理、路由变化时处理。路由要求:your_domain/en 就可以进行语言的切换。

    • 使用中间件 i18n.js 。首先需要安装 vue-i18n ,装好了之后,在/middleware  文件夹下面新建 i18n.js,这里来设置store 、i18n.locale 中的anguage和路由中的展示(我也说不明白,大概就是这个意思)。 在路由变化,页面渲染前会调用中间件。
       1 export default function ({ isHMR, app, store, route, params, error, redirect }) {
       2     const defaultLocale = app.i18n.fallbackLocale
       3     // If middleware is called from hot module replacement, ignore it
       4     if (isHMR) { return }
       5     // Get locale from params
       6     const locale = params.lang || store.state.locale || defaultLocale
       7     if (!store.state.locales.includes(locale)) {
       8       return error({ message: '', statusCode: 404 })
       9     }
      10     // Set locale
      11     store.commit('SET_LANG', locale)
      12     app.i18n.locale = store.state.locale
      13     // If route is /<defaultLocale>/... -> redirect to /...
      14     // if (locale === defaultLocale && route.fullPath.indexOf('/' + defaultLocale) === 0) {
      15     //   const toReplace = '^/' + defaultLocale + (route.fullPath.indexOf('/' + defaultLocale + '/') === 0 ? '/' : '')
      16     //   const re = new RegExp(toReplace)
      17     //   return redirect(
      18     //     route.fullPath.replace(re, '/')
      19     //   )
      20     // }
      21     if(route.fullPath.indexOf('/' + locale) === -1){
      22        return redirect('/' + locale + '' + route.fullPath)
      23     }
      24   }
      25   

      就这样 路由就会展示成 your_domain/en/,当然如果在默认的语言下你不需要展示出 /en/,你也可以换成注释调的部分。

    • 将 i18n 注入vue 实例。上面一段代码有用到了 app.i18n 那这里肯定又要将 i18n 注入vue 实例中,在 plugins 中添加文件 i18n,js , 同Vue.use 进行全局注册,并初始化i18n :

       1 import Vue from 'vue'
       2 import VueI18n from 'vue-i18n'
       3 
       4 Vue.use(VueI18n)
       5 
       6 export default ({ app, store }) => {
       7     // Set i18n instance on app
       8     // This way we can use it in middleware and pages asyncData/fetch
       9     app.i18n = new VueI18n({
      10         locale: store.state.locale,
      11         fallbackLocale: 'en', 
      12         messages: {
      13             'en': require('~/locales/en_US.json'),
      14             'zh-CN': require('~/locales/zh_CN.json')
      15         }
      16     })
      17 
      18     app.i18n.path = (link) => {
      19         if (app.i18n.locale === app.i18n.fallbackLocale) {
      20             return `/${link}`
      21           }
      22         return `/${app.i18n.locale}/${link}`
      23     }
      24 }

      这里我只尝试了中英文的语言的的json 配置,对应语言的json 文件放在更目录下 /locales文件夹下。

    • 怎么调用呢?调用方式比较简单,template 中调用:{{$t(‘json 文件中的属性’)}} js 中调用  this.$t('json 文件中的属性')   

         

    作者:胡倩倩0903
    本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    ASP.NET MVC案例——————拦截器
    Windows Azure Virtual Network (10) 使用Azure Access Control List(ACL)设置客户端访问权限
    Windows Azure Storage (20) 使用Azure File实现共享文件夹
    Windows Azure HandBook (5) Azure混合云解决方案
    Windows Azure Service Bus (6) 中继(Relay On) 使用VS2013开发Service Bus Relay On
    Azure PowerShell (9) 使用PowerShell导出订阅下所有的Azure VM的Public IP和Private IP
    Windows Azure Service Bus (5) 主题(Topic) 使用VS2013开发Service Bus Topic
    Azure China (9) 在Azure China配置CDN服务
    Windows Azure Storage (19) 再谈Azure Block Blob和Page Blob
    Windows Azure HandBook (4) 分析Windows Azure如何处理Session
  • 原文地址:https://www.cnblogs.com/kitty-blog/p/14411707.html
Copyright © 2011-2022 走看看