Localised RowLabel

default discord avatar
sam
last month
4

Hello, I want to have a localised RowLabel for an array field when it is empty, but use one of the fields data when it is present.



Before starting on localisation, I had something like this:



RowLabel: ({ data }) => data.title || 'New item'


I tried picking the locale with the

useLocale

hook, but I believe this is the locale of the content and not the language preference of the user



RowLabel: ({ data }) => {
                    const locale = useLocale();
                    return (
                      data.title ||
                      i18n.accordionBlock.item.emptyRowLabel[locale]
                    );
                  },
  • discord user avatar
    Jarrod
    Payload Team
    last month

    You can use the following:


    const { i18n } = useTranslation()
    const userLanguage = i18n?.language


    To get the currently selected language of the user logged into the dashboard



    And I would turn this into a component that gets imported into your config file, instead of importing hooks into the config file.

  • default discord avatar
    sam
    last month

    Thanks for that, completely missed it in the docs



    I extracted it out to a RowLabelWithFallback (wip name 😅 )

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.