parent
							
								
									d91f3a8661
								
							
						
					
					
						commit
						d21a2ab062
					
				
				 12 changed files with 144 additions and 110 deletions
			
			
		@ -1,55 +1,57 @@ | 
				
			||||
export default { | 
				
			||||
  CN: { | 
				
			||||
    home: {name: '首页'}, | 
				
			||||
  }, | 
				
			||||
  US: { | 
				
			||||
    home: {name: 'home'}, | 
				
			||||
  }, | 
				
			||||
  HK: { | 
				
			||||
    home: {name: '首頁'}, | 
				
			||||
    dashboard: { | 
				
			||||
      name: 'Dashboard', | 
				
			||||
      workplace: {name: '工作台'}, | 
				
			||||
      analysis: {name: '分析頁'} | 
				
			||||
module.exports = { | 
				
			||||
  messages: { | 
				
			||||
    CN: { | 
				
			||||
      home: {name: '首页'}, | 
				
			||||
    }, | 
				
			||||
    form: { | 
				
			||||
      name: '表單頁', | 
				
			||||
      basic: {name: '基礎表單'}, | 
				
			||||
      step: {name: '分步表單'}, | 
				
			||||
      advance: {name: '分步表單'} | 
				
			||||
    US: { | 
				
			||||
      home: {name: 'home'}, | 
				
			||||
    }, | 
				
			||||
    list: { | 
				
			||||
      name: '列表頁', | 
				
			||||
      query: {name: '查詢表格'}, | 
				
			||||
      primary: {name: '標準列表'}, | 
				
			||||
      card: {name: '卡片列表'}, | 
				
			||||
      search: { | 
				
			||||
        name: '搜索列表', | 
				
			||||
        article: {name: '文章'}, | 
				
			||||
        application: {name: '應用'}, | 
				
			||||
        project: {name: '項目'} | 
				
			||||
    HK: { | 
				
			||||
      home: {name: '首頁'}, | 
				
			||||
      dashboard: { | 
				
			||||
        name: 'Dashboard', | 
				
			||||
        workplace: {name: '工作台'}, | 
				
			||||
        analysis: {name: '分析頁'} | 
				
			||||
      }, | 
				
			||||
      form: { | 
				
			||||
        name: '表單頁', | 
				
			||||
        basic: {name: '基礎表單'}, | 
				
			||||
        step: {name: '分步表單'}, | 
				
			||||
        advance: {name: '分步表單'} | 
				
			||||
      }, | 
				
			||||
      list: { | 
				
			||||
        name: '列表頁', | 
				
			||||
        query: {name: '查詢表格'}, | 
				
			||||
        primary: {name: '標準列表'}, | 
				
			||||
        card: {name: '卡片列表'}, | 
				
			||||
        search: { | 
				
			||||
          name: '搜索列表', | 
				
			||||
          article: {name: '文章'}, | 
				
			||||
          application: {name: '應用'}, | 
				
			||||
          project: {name: '項目'} | 
				
			||||
        } | 
				
			||||
      }, | 
				
			||||
      details: { | 
				
			||||
        name: '詳情頁', | 
				
			||||
        basic: {name: '基礎詳情頁'}, | 
				
			||||
        advance: {name: '高級詳情頁'} | 
				
			||||
      }, | 
				
			||||
      result: { | 
				
			||||
        name: '結果頁', | 
				
			||||
        success: {name: '成功'}, | 
				
			||||
        error: {name: '失敗'} | 
				
			||||
      }, | 
				
			||||
      exception: { | 
				
			||||
        name: '異常頁', | 
				
			||||
        404: {name: '404'}, | 
				
			||||
        403: {name: '403'}, | 
				
			||||
        500: {name: '500'} | 
				
			||||
      }, | 
				
			||||
      components: { | 
				
			||||
        name: '小組件', | 
				
			||||
        taskCard: {name: '任務卡片'}, | 
				
			||||
        palette: {name: '顏色複選框'} | 
				
			||||
      } | 
				
			||||
    }, | 
				
			||||
    details: { | 
				
			||||
      name: '詳情頁', | 
				
			||||
      basic: {name: '基礎詳情頁'}, | 
				
			||||
      advance: {name: '高級詳情頁'} | 
				
			||||
    }, | 
				
			||||
    result: { | 
				
			||||
      name: '結果頁', | 
				
			||||
      success: {name: '成功'}, | 
				
			||||
      error: {name: '失敗'} | 
				
			||||
    }, | 
				
			||||
    exception: { | 
				
			||||
      name: '異常頁', | 
				
			||||
      404: {name: '404'}, | 
				
			||||
      403: {name: '403'}, | 
				
			||||
      500: {name: '500'} | 
				
			||||
    }, | 
				
			||||
    components: { | 
				
			||||
      name: '小組件', | 
				
			||||
      taskCard: {name: '任務卡片'}, | 
				
			||||
      palette: {name: '顏色複選框'} | 
				
			||||
    } | 
				
			||||
  } | 
				
			||||
} | 
				
			||||
 | 
				
			||||
@ -0,0 +1,67 @@ | 
				
			||||
import Vue from 'vue' | 
				
			||||
import VueI18n from 'vue-i18n' | 
				
			||||
import routesI18n from '@/router/i18n' | 
				
			||||
import './Objects' | 
				
			||||
 | 
				
			||||
/** | 
				
			||||
 * 创建 i18n 配置 | 
				
			||||
 * @param router 路由 | 
				
			||||
 * @param locale 本地化语言 | 
				
			||||
 * @param fallback 回退语言 | 
				
			||||
 * @returns {VueI18n} | 
				
			||||
 */ | 
				
			||||
function initI18n(router, locale, fallback) { | 
				
			||||
  Vue.use(VueI18n) | 
				
			||||
  const options = router.options.routes.find(item => item.path === '/').children | 
				
			||||
  formatOptions(options, '') | 
				
			||||
  const CN = generateI18n(new Object(), options, 'name') | 
				
			||||
  const US = generateI18n(new Object(), options, 'path') | 
				
			||||
  const i18n = new VueI18n({ | 
				
			||||
    locale, | 
				
			||||
    fallbackLocale: fallback, | 
				
			||||
    silentFallbackWarn: true, | 
				
			||||
    messages: {CN, US} | 
				
			||||
  }) | 
				
			||||
  const messages = routesI18n.messages | 
				
			||||
  Object.keys(messages).forEach(key => { | 
				
			||||
    i18n.mergeLocaleMessage(key, messages[key]) | 
				
			||||
  }) | 
				
			||||
  return i18n | 
				
			||||
} | 
				
			||||
 | 
				
			||||
/** | 
				
			||||
 * 根据 router options 配置生成 国际化语言 | 
				
			||||
 * @param lang | 
				
			||||
 * @param options | 
				
			||||
 * @param valueKey | 
				
			||||
 * @returns {*} | 
				
			||||
 */ | 
				
			||||
function generateI18n(lang, options, valueKey) { | 
				
			||||
  options.forEach(menu => { | 
				
			||||
    let keys = menu.fullPath.substring(1).split('/').concat('name') | 
				
			||||
    lang.assignProps(keys, menu[valueKey]) | 
				
			||||
    if (menu.children) { | 
				
			||||
      generateI18n(lang, menu.children, valueKey) | 
				
			||||
    } | 
				
			||||
  }) | 
				
			||||
  return lang | 
				
			||||
} | 
				
			||||
 | 
				
			||||
/** | 
				
			||||
 * 格式化 router options,生成 fullPath | 
				
			||||
 * @param options | 
				
			||||
 * @param parentPath | 
				
			||||
 */ | 
				
			||||
function formatOptions(options, parentPath) { | 
				
			||||
  options.forEach(route => { | 
				
			||||
    let isFullPath = route.path.substring(0, 1) === '/' | 
				
			||||
    route.fullPath = isFullPath ? route.path : parentPath + '/' + route.path | 
				
			||||
    if (route.children) { | 
				
			||||
      formatOptions(route.children, route.fullPath) | 
				
			||||
    } | 
				
			||||
  }) | 
				
			||||
} | 
				
			||||
 | 
				
			||||
export { | 
				
			||||
  initI18n | 
				
			||||
} | 
				
			||||
					Loading…
					
					
				
		Reference in new issue