From 3e6b82c1a3755a97ae752a335b79b69e0eca61d6 Mon Sep 17 00:00:00 2001 From: fzaninotto Date: Fri, 12 Mar 2021 14:30:08 +0100 Subject: [PATCH] Fix SortButton labels must have a valid translatino message --- .../src/util/getFieldLabelTranslationArgs.ts | 8 ++++---- .../src/button/SortButton.tsx | 20 ++++++++++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/ra-core/src/util/getFieldLabelTranslationArgs.ts b/packages/ra-core/src/util/getFieldLabelTranslationArgs.ts index 411108955a4..7ddb0dfe95a 100644 --- a/packages/ra-core/src/util/getFieldLabelTranslationArgs.ts +++ b/packages/ra-core/src/util/getFieldLabelTranslationArgs.ts @@ -1,9 +1,9 @@ import inflection from 'inflection'; interface Args { - label: string; - resource: string; - source: string; + label?: string; + resource?: string; + source?: string; } type TranslationArguments = [string, any?]; @@ -17,7 +17,7 @@ type TranslationArguments = [string, any?]; * {translate(...getFieldLabelTranslationArgs({ label, resource, source }))} * */ -export default (options: Args): TranslationArguments => { +export default (options?: Args): TranslationArguments => { if (!options) { return ['']; } diff --git a/packages/ra-ui-materialui/src/button/SortButton.tsx b/packages/ra-ui-materialui/src/button/SortButton.tsx index 26acad891a1..2290b7eed96 100644 --- a/packages/ra-ui-materialui/src/button/SortButton.tsx +++ b/packages/ra-ui-materialui/src/button/SortButton.tsx @@ -12,7 +12,11 @@ import { import SortIcon from '@material-ui/icons/Sort'; import ArrowDropDownIcon from '@material-ui/icons/ArrowDropDown'; import { shallowEqual } from 'react-redux'; -import { useListSortContext, useTranslate } from 'ra-core'; +import { + useListSortContext, + useTranslate, + getFieldLabelTranslationArgs, +} from 'ra-core'; /** * A button allowing to change the sort field and order. @@ -72,7 +76,12 @@ const SortButton: FC = ({ }; const buttonLabel = translate(label, { - field: translate(`resources.${resource}.fields.${currentSort.field}`), + field: translate( + ...getFieldLabelTranslationArgs({ + resource, + source: currentSort.field, + }) + ), order: translate(`ra.sort.${currentSort.order}`), _: label, }); @@ -115,7 +124,12 @@ const SortButton: FC = ({ data-sort={field} key={field} > - {translate(`resources.${resource}.fields.${field}`)}{' '} + {translate( + ...getFieldLabelTranslationArgs({ + resource, + source: field, + }) + )}{' '} {translate( `ra.sort.${ currentSort.field === field