i18nUtil.js 2.84 KB
Newer Older
水落(YangLei)'s avatar
水落(YangLei) committed
1 2
import routesI18n from '@/router/i18n';
import { getI18nKey } from '@/utils/routerUtil';
wb-ct393452's avatar
wb-ct393452 committed
3 4 5 6 7 8 9 10 11

/**
 * 根据 router options 配置生成 国际化语言
 * @param lang
 * @param routes
 * @param valueKey
 * @returns {*}
 */
function generateI18n(lang, routes, valueKey) {
水落(YangLei)'s avatar
水落(YangLei) committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    routes.forEach(route => {
        let keys = getI18nKey(route.fullPath).split('.');
        console.log(keys);
        let value =
            valueKey === 'path'
                ? route[valueKey]
                      .split('/')
                      .filter(item => !item.startsWith(':') && item != '')
                      .join('.')
                : route[valueKey];
        if (value.includes('_')) {
            value = value.replace('_', ' ');
            value = value.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase());
        } else {
            value = value.charAt(0).toUpperCase() + value.toLowerCase().substring(1);
        }

        lang.assignProps(keys, value);
        if (route.children) {
            generateI18n(lang, route.children, valueKey);
        }
    });
    return lang;
wb-ct393452's avatar
wb-ct393452 committed
35 36 37 38 39 40 41 42
}

/**
 * 格式化 router.options.routes,生成 fullPath
 * @param routes
 * @param parentPath
 */
function formatFullPath(routes, parentPath = '') {
水落(YangLei)'s avatar
水落(YangLei) committed
43 44 45 46 47 48 49 50 51 52 53
    routes.forEach(route => {
        let isFullPath = route.path.substring(0, 1) === '/';
        route.fullPath = isFullPath
            ? route.path
            : parentPath === '/'
            ? parentPath + route.path
            : parentPath + '/' + route.path;
        if (route.children) {
            formatFullPath(route.children, route.fullPath);
        }
    });
wb-ct393452's avatar
wb-ct393452 committed
54 55 56 57 58 59 60 61
}

/**
 * 从路由提取国际化数据
 * @param i18n
 * @param routes
 */
function mergeI18nFromRoutes(i18n, routes) {
水落(YangLei)'s avatar
水落(YangLei) committed
62 63 64 65 66 67 68 69 70 71
    formatFullPath(routes);
    const zh_CN = generateI18n(new Object(), routes, 'name');
    const en_US = generateI18n(new Object(), routes, 'path');

    i18n.mergeLocaleMessage('zh_CN', zh_CN);
    i18n.mergeLocaleMessage('en_US', en_US);
    const messages = routesI18n.messages;
    Object.keys(messages).forEach(lang => {
        i18n.mergeLocaleMessage(lang, messages[lang]);
    });
wb-ct393452's avatar
wb-ct393452 committed
72 73 74 75 76 77 78 79 80
}

/**
 * 给对象注入属性
 * @param keys 属性key数组, 如 keys = ['config', 'path'] , 则会给对象注入 object.config.path 的属性
 * @param value 属性值
 * @returns {Object}
 */
Object.defineProperty(Object.prototype, 'assignProps', {
水落(YangLei)'s avatar
水落(YangLei) committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    writable: false,
    enumerable: false,
    configurable: true,
    value: function(keys, value) {
        let props = this;
        for (let i = 0; i < keys.length; i++) {
            let key = keys[i];
            if (i == keys.length - 1) {
                props[key] = value;
            } else {
                props[key] = props[key] == undefined ? {} : props[key];
                props = props[key];
            }
        }
        return this;
    },
});
wb-ct393452's avatar
wb-ct393452 committed
98

水落(YangLei)'s avatar
水落(YangLei) committed
99
export { mergeI18nFromRoutes, formatFullPath };