diff --git a/changelog.md b/changelog.md index 8c8bb9ba5..75f0e66b1 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,6 @@ # 更新日志 2.0(2021-07) +- 支持BI.changeI18n动态换i18n方法 - layout支持forceUpdate刷新方式 - width属性支持calc() - 修改了颜色选择器交互 diff --git a/src/base/single/0.single.js b/src/base/single/0.single.js index d6cd92f2c..e12c726d1 100644 --- a/src/base/single/0.single.js +++ b/src/base/single/0.single.js @@ -196,7 +196,8 @@ BI.Single = BI.inherit(BI.Widget, { return this.options.value; }, - destroyed: function () { + __d: function () { + BI.Single.superclass.__d.call(this); if (BI.isNotNull(this.showTimeout)) { clearTimeout(this.showTimeout); this.showTimeout = null; diff --git a/src/base/single/1.text.js b/src/base/single/1.text.js index 125854873..65aa03712 100644 --- a/src/base/single/1.text.js +++ b/src/base/single/1.text.js @@ -45,13 +45,13 @@ }); } if (BI.isWidthOrHeight(o.height)) { - this.element.css({ lineHeight: BI.isNumber(o.height) ? (o.height / BI.pixRatio + BI.pixUnit) : o.height }); + this.element.css({lineHeight: BI.isNumber(o.height) ? (o.height / BI.pixRatio + BI.pixUnit) : o.height}); } if (BI.isWidthOrHeight(o.lineHeight)) { - this.element.css({ lineHeight: BI.isNumber(o.lineHeight) ? (o.lineHeight / BI.pixRatio + BI.pixUnit) : o.lineHeight }); + this.element.css({lineHeight: BI.isNumber(o.lineHeight) ? (o.lineHeight / BI.pixRatio + BI.pixUnit) : o.lineHeight}); } if (BI.isWidthOrHeight(o.maxWidth)) { - this.element.css({ maxWidth: BI.isNumber(o.maxWidth) ? (o.maxWidth / BI.pixRatio + BI.pixUnit) : o.maxWidth }); + this.element.css({maxWidth: BI.isNumber(o.maxWidth) ? (o.maxWidth / BI.pixRatio + BI.pixUnit) : o.maxWidth}); } this.element.css({ textAlign: o.textAlign, @@ -106,7 +106,9 @@ _getShowText: function () { var o = this.options; var text = BI.isFunction(o.text) ? o.text() : o.text; - + if (text && text.addWidget) { + text.addWidget(this); + } return BI.isKey(text) ? BI.Text.formatText(text + "") : text; }, @@ -152,6 +154,11 @@ BI.Text.superclass.setText.apply(this, arguments); this.options.text = text; this._doRedMark(this.options.keyword); + }, + + __d: function () { + BI.Text.superclass.__d.call(this); + BI.i18nProvider.removeWidget(this); } }); var formatters = []; diff --git a/src/core/utils/i18n.js b/src/core/utils/i18n.js index 23fd11600..f725ad666 100644 --- a/src/core/utils/i18n.js +++ b/src/core/utils/i18n.js @@ -1,6 +1,28 @@ !(function () { var i18nStore = {}; + var i18nWidgets = {}; _.extend(BI, { + i18nProvider: { + addWidget: function (widget, i18n) { + if (!i18nWidgets[widget.getName()]) { + i18nWidgets[widget.getName()] = i18n; + i18n.widget = widget; + } + }, + removeWidget: function (widget) { + if (i18nWidgets[widget.getName()]) { + delete i18nWidgets[widget.getName()]; + } + } + }, + changeI18n: function (i18n) { + if (i18n) { + i18nStore = i18n; + } + BI.each(i18nWidgets, function (widgetName, i18n) { + i18n.widget.setText(BI.i18nText.apply(null, i18n.args)); + }); + }, addI18n: function (i18n) { BI.extend(i18nStore, i18n); }, @@ -24,7 +46,18 @@ }); } } - return localeText; + return { + args: arguments, + addWidget: function (widget) { + BI.i18nProvider.addWidget(widget, this); + }, + valueOf: function () { + return localeText; + }, + toString: function () { + return localeText; + } + }; } }); -})(); \ No newline at end of file +})();