Skip to content

Commit 7c15fd1

Browse files
Copilothotlong
andcommitted
refactor: address code review feedback - extract resolve helper, add comment, clean up ternary
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent b56865f commit 7c15fd1

3 files changed

Lines changed: 16 additions & 20 deletions

File tree

packages/i18n/src/useObjectLabel.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,33 @@ import { useObjectTranslation } from './provider';
3131
export function useObjectLabel() {
3232
const { t } = useObjectTranslation();
3333

34+
/** Check whether a translation lookup returned a valid translated string. */
35+
const resolve = (key: string, fallback: string): string => {
36+
const translated = t(key, { defaultValue: '' });
37+
return (translated && translated !== key && translated !== '')
38+
? translated
39+
: fallback;
40+
};
41+
3442
return {
3543
/**
3644
* Resolve translated object label, falling back to objectDef.label.
3745
*/
38-
objectLabel: (objectDef: { name: string; label: string }) => {
39-
const key = `crm.objects.${objectDef.name}.label`;
40-
const translated = t(key, { defaultValue: '' });
41-
return (translated && translated !== key && translated !== '')
42-
? translated
43-
: objectDef.label;
44-
},
46+
objectLabel: (objectDef: { name: string; label: string }) =>
47+
resolve(`crm.objects.${objectDef.name}.label`, objectDef.label),
4548

4649
/**
4750
* Resolve translated object description, falling back to objectDef.description.
4851
*/
4952
objectDescription: (objectDef: { name: string; description?: string }) => {
5053
if (!objectDef.description) return undefined;
51-
const key = `crm.objects.${objectDef.name}.description`;
52-
const translated = t(key, { defaultValue: '' });
53-
return (translated && translated !== key && translated !== '')
54-
? translated
55-
: objectDef.description;
54+
return resolve(`crm.objects.${objectDef.name}.description`, objectDef.description);
5655
},
5756

5857
/**
5958
* Resolve translated field label, falling back to the provided fallback string.
6059
*/
61-
fieldLabel: (objectName: string, fieldName: string, fallback: string) => {
62-
const key = `crm.fields.${objectName}.${fieldName}`;
63-
const translated = t(key, { defaultValue: '' });
64-
return (translated && translated !== key && translated !== '')
65-
? translated
66-
: fallback;
67-
},
60+
fieldLabel: (objectName: string, fieldName: string, fallback: string) =>
61+
resolve(`crm.fields.${objectName}.${fieldName}`, fallback),
6862
};
6963
}

packages/layout/src/NavigationRenderer.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ function resolveItemLabel(
185185
resolver?: (objectName: string, fallbackLabel: string) => string,
186186
): string {
187187
const base = resolveLabel(item.label);
188+
// Only apply convention-based resolution for object-type items with plain string labels.
189+
// I18nLabel objects (with explicit key/defaultValue) already have their own translation keys.
188190
if (resolver && item.type === 'object' && item.objectName && typeof item.label === 'string') {
189191
return resolver(item.objectName, base);
190192
}

packages/plugin-grid/src/ObjectGrid.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ export const ObjectGrid: React.FC<ObjectGridProps> = ({
861861
const CellRenderer = getCellRenderer(field.type);
862862
const numericTypes = ['number', 'currency', 'percent'];
863863
generatedColumns.push({
864-
header: schema.objectName ? resolveFieldLabel(schema.objectName, fieldName, field.label || fieldName) : (field.label || fieldName),
864+
header: schema.objectName ? resolveFieldLabel(schema.objectName, fieldName, field.label || fieldName) : field.label || fieldName,
865865
accessorKey: fieldName,
866866
...(numericTypes.includes(field.type) && { align: 'right' }),
867867
cell: (value: any) => <CellRenderer value={value} field={field} />,

0 commit comments

Comments
 (0)