UNPKG

ten-design-vue

Version:

ten-vue

108 lines (88 loc) 2.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.localMixin = exports.default = void 0; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } var config = { locale: require('../locale/zh-CN').default, set: function set(options) { config = _extends({}, config, options); listeners.forEach(function (listener) { return listener(); }); }, get: function get(key) { if (key) { return config[key]; } return config; } }; function set(options) { config = _extends({}, config, options); listeners.forEach(function (listener) { return listener(); }); } function get(key) { if (key) { return config[key]; } return config; } var listeners = []; function addListener(listener) { if (listeners.indexOf(listener) === -1) { listeners.push(listener); } } function removeListener(listener) { var index = listeners.indexOf(listener); if (index > -1) { listeners.splice(index, 1); } } var localMixin = { mounted: function mounted() { addListener(this.onLocaleChange); }, beforeDestroy: function beforeDestroy() { removeListener(this.onLocaleChange); }, methods: { t: function t() { var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var placement = arguments.length > 1 ? arguments[1] : undefined; if (!path) { return ''; } var regx = /\{\s*([\w-]+)\s*\}/g; var pathArr = path.split('.'); var index = 0; var pattern = config.get('locale'); while (_typeof(pattern) === 'object' && pathArr[index]) { var key = pathArr[index]; if (/^\d+$/.test(key)) { key = Number(key); } pattern = pattern[key] || ''; index += 1; } // eslint-disable-next-line no-confusing-arrow var translated = pattern.replace(regx, function (_match, key) { return placement ? placement[key] : ''; }); return translated; }, onLocaleChange: function onLocaleChange() { this.$forceUpdate(); } } }; exports.localMixin = localMixin; var _default = { get: get, set: set }; exports.default = _default;