Skip to content

Commit 17af9f7

Browse files
feat(locale): add i18n plural for pagination.total (#3641)
* feat(locale): add i18n plural for pagination.total * chore: update common * fix(locale): update example {count} for test --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 12cad50 commit 17af9f7

File tree

6 files changed

+11
-30
lines changed

6 files changed

+11
-30
lines changed

src/config-provider/_example-composition/pagination.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const globalConfig = ref(
1717
itemsPerPage: '{size} / page',
1818
jumpTo: 'jump to',
1919
page: '',
20-
total: 'Total {total} items',
20+
total: 'Total {count} items',
2121
},
2222
}),
2323
);

src/config-provider/_example/pagination.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
itemsPerPage: '{size} / page',
1919
jumpTo: 'jump to',
2020
page: '',
21-
total: 'Total {total} items',
21+
total: 'Total {count} items',
2222
},
2323
}),
2424
};

src/config-provider/config-receiver.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { mergeWith } from 'lodash-es';
33
import { GlobalIconConfig } from 'tdesign-icons-vue';
44
import { defaultGlobalConfig } from './context';
55
import { GlobalConfigProvider, AnimationType, AttachNodeComponent } from './type';
6+
import { t as commonT } from '../_common/js/global-config/t';
67

78
import type { AttachNode } from '../common';
89

@@ -78,22 +79,11 @@ export default function getConfigReceiverMixins<BasicComponent extends Vue, C ex
7879
},
7980

8081
methods: {
81-
t<T>(pattern: T, placement?: Placement): string {
82-
if (typeof pattern === 'string') {
83-
if (!placement) return pattern;
84-
const regexp = /\{\s*([\w-]+)\s*\}/g;
85-
const translated = pattern.replace(regexp, (match, key) => {
86-
if (placement) {
87-
return String(placement[key]);
88-
}
89-
return '';
90-
});
91-
return translated;
92-
}
82+
t<T>(pattern: T, placement?: Placement | number): string {
9383
if (typeof pattern === 'function') {
9484
return pattern(placement);
9585
}
96-
return '';
86+
return commonT(pattern as string, placement as any);
9787
},
9888
},
9989
});

src/config-provider/useConfig.tsx

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,18 @@ import {
22
inject, h, ref, computed,
33
} from '@vue/composition-api';
44
import { GlobalConfigProvider, defaultGlobalConfig } from './context';
5+
import { t as commonT } from '../_common/js/global-config/t';
56

67
// 处理正则表达式
78
const t = function <T> (pattern: T, ...args: any[]) {
8-
const [data] = args;
9-
if (typeof pattern === 'string') {
10-
if (!data) return pattern;
11-
const regular = /\{\s*([\w-]+)\s*\}/g;
12-
const translated = pattern.replace(regular, (match, key) => {
13-
if (data) {
14-
return String(data[key]);
15-
}
16-
return '';
17-
});
18-
return translated;
19-
}
209
if (typeof pattern === 'function') {
2110
// 重要:组件的渲染必须存在参数 h,不能移除
2211
if (!args.length) return pattern(h);
2312
return pattern(...args);
2413
}
25-
return '';
14+
// 使用公共翻译函数,以支持复数处理
15+
// @ts-expect-error be passed to rest parameter
16+
return commonT(pattern, ...args);
2617
};
2718

2819
/**

src/pagination/pagination.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export default mixins(getConfigReceiverMixins<Vue, PaginationConfig>('pagination
331331
{renderTNodeJSX(
332332
this,
333333
'totalContent',
334-
<div class={this.totalClass}>{this.t(this.global.total, { total: this.total })}</div>,
334+
<div class={this.totalClass}>{this.t(this.global.total, this.total)}</div>,
335335
)}
336336
{/* 分页器 */}
337337
{this.showPageSize && this.pageSizeOptions.length ? (

0 commit comments

Comments
 (0)