@@ -31,39 +31,33 @@ import { useObjectTranslation } from './provider';
3131export 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}
0 commit comments