tachybase_todo/packages/plugins/@tachybase/plugin-localization-management/src/server/utils.ts
sealday ede7ead8b1 chore(version): release v0.21.34 (#1045)
Co-authored-by: sealday <sealday@gmail.com>
Reviewed-on: daoyoucloud/tachybase#1045
2024-05-24 01:06:06 +08:00

50 lines
1.3 KiB
TypeScript

export const compile = (title: string) => (title || '').replace(/{{\s*t\(["|'|`](.*)["|'|`]\)\s*}}/g, '$1');
export const getTextsFromUISchema = (schema: any) => {
const texts = [];
const title = compile(schema.title);
const componentPropsTitle = compile(schema['x-component-props']?.title);
const decoratorPropsTitle = compile(schema['x-decorator-props']?.title);
if (title) {
texts.push(title);
}
if (componentPropsTitle) {
texts.push(componentPropsTitle);
}
if (decoratorPropsTitle) {
texts.push(decoratorPropsTitle);
}
if (schema['x-data-templates']?.items?.length) {
schema['x-data-templates'].items.forEach((item: any) => {
const title = compile(item.title);
if (title) {
texts.push(title);
}
});
}
return texts;
};
export const getTextsFromDBRecord = (fields: string[], record: any) => {
const texts = [];
fields.forEach((field) => {
const value = record[field];
if (typeof value === 'string') {
texts.push(compile(value));
}
if (typeof value === 'object') {
if (value?.uiSchema?.title) {
texts.push(compile(value.uiSchema.title));
}
if (value?.uiSchema?.enum) {
value.uiSchema.enum.forEach((item) => {
if (item?.label) {
texts.push(compile(item.label));
}
});
}
}
});
return texts;
};