mirror of https://github.com/ElemeFE/element
Locale: customize i18n handler, close #2129
parent
4da8b2a2dd
commit
171b7fd0f1
|
@ -16,6 +16,7 @@ const install = function(Vue, opts = {}) {
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
if (install.installed) return;
|
if (install.installed) return;
|
||||||
locale.use(opts.locale);
|
locale.use(opts.locale);
|
||||||
|
locale.i18n(opts.i18n);
|
||||||
|
|
||||||
{{install}}
|
{{install}}
|
||||||
|
|
||||||
|
@ -38,6 +39,7 @@ if (typeof window !== 'undefined' && window.Vue) {
|
||||||
module.exports = {
|
module.exports = {
|
||||||
version: '{{version}}',
|
version: '{{version}}',
|
||||||
locale: locale.use,
|
locale: locale.use,
|
||||||
|
i18n: locale.i18n,
|
||||||
install,
|
install,
|
||||||
Loading,
|
Loading,
|
||||||
{{list}}
|
{{list}}
|
||||||
|
|
|
@ -68,6 +68,22 @@ Vue.locale('zh-cn', zhLocale)
|
||||||
Vue.locale('en', enLocale)
|
Vue.locale('en', enLocale)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 兼容其他 i18n 插件
|
||||||
|
如果不使用 `vue-i18n`,而是用其他的 i18n 插件,Element 将无法兼容,但是可以自定义 Element 的 i18n 的处理方法。
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import Vue from 'vue'
|
||||||
|
import Element from 'element-ui'
|
||||||
|
import enLocale from 'element-ui/lib/locale/lang/en'
|
||||||
|
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
|
||||||
|
|
||||||
|
Vue.use(Element, {
|
||||||
|
i18n: function (path, options) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
目前 Element 内置了以下语言:
|
目前 Element 内置了以下语言:
|
||||||
<ul class="language-list">
|
<ul class="language-list">
|
||||||
<li>简体中文(zh-CN)</li>
|
<li>简体中文(zh-CN)</li>
|
||||||
|
|
|
@ -66,6 +66,7 @@ const install = function(Vue, opts = {}) {
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
if (install.installed) return;
|
if (install.installed) return;
|
||||||
locale.use(opts.locale);
|
locale.use(opts.locale);
|
||||||
|
locale.i18n(opts.i18n);
|
||||||
|
|
||||||
Vue.component(Pagination.name, Pagination);
|
Vue.component(Pagination.name, Pagination);
|
||||||
Vue.component(Dialog.name, Dialog);
|
Vue.component(Dialog.name, Dialog);
|
||||||
|
@ -143,6 +144,7 @@ if (typeof window !== 'undefined' && window.Vue) {
|
||||||
module.exports = {
|
module.exports = {
|
||||||
version: '1.1.2',
|
version: '1.1.2',
|
||||||
locale: locale.use,
|
locale: locale.use,
|
||||||
|
i18n: locale.i18n,
|
||||||
install,
|
install,
|
||||||
Loading,
|
Loading,
|
||||||
Pagination,
|
Pagination,
|
||||||
|
|
|
@ -6,8 +6,7 @@ import Format from './format';
|
||||||
const format = Format(Vue);
|
const format = Format(Vue);
|
||||||
let lang = defaultLang;
|
let lang = defaultLang;
|
||||||
let merged = false;
|
let merged = false;
|
||||||
|
let i18nHandler = function() {
|
||||||
export const t = function(path, options) {
|
|
||||||
const vuei18n = Object.getPrototypeOf(this || Vue).$t;
|
const vuei18n = Object.getPrototypeOf(this || Vue).$t;
|
||||||
if (typeof vuei18n === 'function') {
|
if (typeof vuei18n === 'function') {
|
||||||
if (!merged) {
|
if (!merged) {
|
||||||
|
@ -17,14 +16,20 @@ export const t = function(path, options) {
|
||||||
deepmerge(lang, Vue.locale(Vue.config.lang) || {}, { clone: true })
|
deepmerge(lang, Vue.locale(Vue.config.lang) || {}, { clone: true })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return vuei18n.apply(this, [path, options]);
|
return vuei18n.apply(this, arguments);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const t = function(path, options) {
|
||||||
|
let value = i18nHandler.apply(this, arguments);
|
||||||
|
if (value) return value;
|
||||||
|
|
||||||
const array = path.split('.');
|
const array = path.split('.');
|
||||||
let current = lang;
|
let current = lang;
|
||||||
|
|
||||||
for (var i = 0, j = array.length; i < j; i++) {
|
for (let i = 0, j = array.length; i < j; i++) {
|
||||||
var property = array[i];
|
const property = array[i];
|
||||||
var value = current[property];
|
value = current[property];
|
||||||
if (i === j - 1) return format(value, options);
|
if (i === j - 1) return format(value, options);
|
||||||
if (!value) return '';
|
if (!value) return '';
|
||||||
current = value;
|
current = value;
|
||||||
|
@ -35,4 +40,9 @@ export const t = function(path, options) {
|
||||||
export const use = function(l) {
|
export const use = function(l) {
|
||||||
lang = l || lang;
|
lang = l || lang;
|
||||||
};
|
};
|
||||||
export default { use, t };
|
|
||||||
|
export const i18n = function(fn) {
|
||||||
|
i18nHandler = fn || i18nHandler;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default { use, t, i18n };
|
||||||
|
|
Loading…
Reference in New Issue