All files / src/mixins/i18n index.js

100% Statements 6/6
50% Branches 1/2
100% Functions 3/3
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23              245x     470x 2589x 2589x 2589x   2589x              
import camelCase from 'lodash/camelCase';
import get from 'lodash/get';
import template from 'lodash/template';
 
import locale from '../../locale';
 
export default function (name) {
    return {
        // 在注册props之前,往vm上挂载一个$tt方法
        beforeCreate() {
            this.$tt = function (key, data) {
                const scopeKey = `${camelCase(name)}_${key}`;
                const messages = locale.messages();
                const message = get(messages, scopeKey) || get(messages, key);
 
                return template(message, {
                    interpolate: /\{(.+?)\}/g,
                })(data);
            };
        },
    };
}