fix: export with i18n (#5591)

This commit is contained in:
ChengLei Shao 2024-11-06 10:29:26 +08:00 committed by GitHub
parent 1050997ff1
commit 5788841df6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 93 additions and 5 deletions

View File

@ -40,7 +40,12 @@ export class BooleanInterface extends BaseInterface {
const option = enumConfig.find((item) => item.value === value);
return option?.label;
} else {
return value ? '是' : value === null || value === undefined ? '' : '否';
const label = value ? 'True' : value === null || value === undefined ? '' : 'False';
if (ctx?.t) {
return ctx.t(label, { ns: 'action-export' });
}
return label;
}
}
}

View File

@ -36,7 +36,16 @@ export class MultipleSelectInterface extends BaseInterface {
.castArray(value)
.map((value) => {
const option = enumConfig.find((item) => item.value === value);
return option ? option.label : value;
if (option) {
if (ctx?.t) {
return ctx.t(option.label, { ns: 'lm-collections' });
}
return option.label;
}
return value;
})
.join(',');
}

View File

@ -32,6 +32,15 @@ export class SelectInterface extends BaseInterface {
toString(value: any, ctx?: any) {
const enumConfig = this.options.uiSchema?.enum || [];
const option = enumConfig.find((item) => item.value === value);
return option?.label || value;
if (option) {
if (ctx?.t) {
return ctx.t(option.label, { ns: 'lm-collections' });
}
return option.label;
}
return value;
}
}

View File

@ -1,4 +1,6 @@
{
"Export warning": "You can export up to {{limit}} rows of data at a time, any excess will be ignored.",
"Start export": "Start export"
"Start export": "Start export",
"True": "True",
"False": "False"
}

View File

@ -1,5 +1,7 @@
{
"Export warning": "每次最多导出 {{limit}} 行数据,超出的将被忽略。",
"Start export": "开始导出",
"another export action is running, please try again later.": "另一导出任务正在运行,请稍后重试。"
"another export action is running, please try again later.": "另一导出任务正在运行,请稍后重试。",
"True": "是",
"False": "否"
}

View File

@ -31,6 +31,67 @@ describe('export to xlsx with preset', () => {
await app.destroy();
});
it('should export with checkbox field', async () => {
const Post = app.db.collection({
name: 'posts',
fields: [
{ type: 'string', name: 'title' },
{
type: 'boolean',
name: 'test_field',
interface: 'checkbox',
uiSchema: {
type: 'boolean',
'x-component': 'Checkbox',
},
},
],
});
await app.db.sync();
await Post.repository.create({
values: {
title: 'p1',
test_field: true,
},
});
const exporter = new XlsxExporter({
collectionManager: app.mainDataSource.collectionManager,
collection: Post,
chunkSize: 10,
columns: [
{ dataIndex: ['title'], defaultTitle: 'Title' },
{
dataIndex: ['test_field'],
defaultTitle: 'test_field',
},
],
});
const wb = await exporter.run();
const xlsxFilePath = path.resolve(__dirname, `t_${uid()}.xlsx`);
try {
XLSX.writeFile(wb, xlsxFilePath);
// read xlsx file
const workbook = XLSX.readFile(xlsxFilePath);
const firstSheet = workbook.Sheets[workbook.SheetNames[0]];
const sheetData = XLSX.utils.sheet_to_json(firstSheet, { header: 1 });
const header = sheetData[0];
expect(header).toEqual(['Title', 'test_field']);
const data = sheetData[1];
expect(data[1]).toBe('True');
} finally {
fs.unlinkSync(xlsxFilePath);
}
});
it('should export number field with cell format', async () => {
const Post = app.db.collection({
name: 'posts',