fix: i18n problem of dynamic route; 🐛

修复:动态路由的国际化问题;
This commit is contained in:
iczer
2020-08-04 11:30:44 +08:00
parent d7eba7e2b3
commit b796a4c9e4
5 changed files with 34 additions and 17 deletions

View File

@@ -2,6 +2,7 @@ import Vue from 'vue'
import VueI18n from 'vue-i18n'
import routesI18n from '@/router/i18n'
import './Objects'
import {getI18nKey} from '@/utils/routerUtil'
/**
* 创建 i18n 配置
@@ -23,16 +24,17 @@ function initI18n(locale, fallback) {
/**
* 根据 router options 配置生成 国际化语言
* @param lang
* @param options
* @param routes
* @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)
function generateI18n(lang, routes, valueKey) {
routes.forEach(route => {
let keys = getI18nKey(route.fullPath).split('.')
let value = valueKey === 'path' ? route[valueKey].split('/').filter(item => !item.startsWith(':') && item != '').join('.') : route[valueKey]
lang.assignProps(keys, value)
if (route.children) {
generateI18n(lang, route.children, valueKey)
}
})
return lang

View File

@@ -185,4 +185,15 @@ function formatAuthority(routes) {
})
}
export {parseRoutes, loadRoutes, loginGuard, authorityGuard, formatAuthority}
/**
* 从路由 path 解析 i18n key
* @param path
* @returns {*}
*/
function getI18nKey(path) {
const keys = path.split('/').filter(item => !item.startsWith(':') && item != '')
keys.push('name')
return keys.join('.')
}
export {parseRoutes, loadRoutes, loginGuard, authorityGuard, formatAuthority, getI18nKey}