index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.i18n = exports.use = exports.t = undefined;
  4. var _zhCN = require('element-ui/lib/locale/lang/zh-CN');
  5. var _zhCN2 = _interopRequireDefault(_zhCN);
  6. var _vue = require('vue');
  7. var _vue2 = _interopRequireDefault(_vue);
  8. var _deepmerge = require('deepmerge');
  9. var _deepmerge2 = _interopRequireDefault(_deepmerge);
  10. var _format = require('./format');
  11. var _format2 = _interopRequireDefault(_format);
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. var format = (0, _format2.default)(_vue2.default);
  14. var lang = _zhCN2.default;
  15. var merged = false;
  16. var i18nHandler = function i18nHandler() {
  17. var vuei18n = Object.getPrototypeOf(this || _vue2.default).$t;
  18. if (typeof vuei18n === 'function' && !!_vue2.default.locale) {
  19. if (!merged) {
  20. merged = true;
  21. _vue2.default.locale(_vue2.default.config.lang, (0, _deepmerge2.default)(lang, _vue2.default.locale(_vue2.default.config.lang) || {}, { clone: true }));
  22. }
  23. return vuei18n.apply(this, arguments);
  24. }
  25. };
  26. var t = exports.t = function t(path, options) {
  27. var value = i18nHandler.apply(this, arguments);
  28. if (value !== null && value !== undefined) return value;
  29. var array = path.split('.');
  30. var current = lang;
  31. for (var i = 0, j = array.length; i < j; i++) {
  32. var property = array[i];
  33. value = current[property];
  34. if (i === j - 1) return format(value, options);
  35. if (!value) return '';
  36. current = value;
  37. }
  38. return '';
  39. };
  40. var use = exports.use = function use(l) {
  41. lang = l || lang;
  42. };
  43. var i18n = exports.i18n = function i18n(fn) {
  44. i18nHandler = fn || i18nHandler;
  45. };
  46. exports.default = { use: use, t: t, i18n: i18n };