Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text label is not translated. #2

Open
mfilteau opened this issue Jun 27, 2024 · 7 comments
Open

Text label is not translated. #2

mfilteau opened this issue Jun 27, 2024 · 7 comments
Assignees

Comments

@mfilteau
Copy link

When attaching different translations to a field using the mask plugin, the proper translation is not selected.

label: {
   en: 'Phone Number',
   fr: 'Numéro de Téléphone',
}

Payloadcms label uses the following pattern to translate the label text.

import { useTranslation } from 'react-i18next'

...

  const { i18n } = useTranslation()

  {getTranslation(label, i18n)}
@notchris notchris self-assigned this Jul 11, 2024
@notchris
Copy link
Owner

notchris commented Jul 15, 2024

@mfilteau I'm working on this one now. I'm having an issue with i18n's useTranslation when passing the label prop. Is this the incorrect usage? Is it supposed to be the string "label" ?
Screenshot 2024-07-15 at 1 37 43 PM

@mfilteau
Copy link
Author

mfilteau commented Jul 15, 2024

Here's the way it works:

import { useField } from 'payload/components/forms'
import { getTranslation } from 'payload/utilities'
import React, { useRef } from 'react'
import { useTranslation } from 'react-i18next'
import { IMaskInput } from 'react-imask'

...
  const { i18n } = useTranslation()

  return (
    <div className="field-type text">
      {label && (
        <label className="field-label" htmlFor={'field-' + name}>
          {getTranslation(label, i18n)}
          {required && <span className="required">*</span>}
        </label>
      )}
      ...

You call useTranslation from react-18next and then apply it on the label with getTranslation.

@mfilteau
Copy link
Author

There is also an example in PayloadCMS documentation:
https://payloadcms.com/docs/admin/components#label-component

@notchris
Copy link
Owner

@mfilteau

Hmm, even after following the example, I'm still getting

Type instantiation is excessively deep and possibly infinite.ts(2589)

when using const { i18n } = useTranslation()

Maybe my tsconfig is messed up?

@mfilteau
Copy link
Author

I remember getting something like that when I added react-i18next as a direct dependency in my project.
Once I relied on the version that payloadcms needs, the problem disappeared.

There's a discussion about that issue here:
i18next/react-i18next#1601

@notchris
Copy link
Owner

Good to know, thank you @mfilteau! I'll try this out when I get home from work today.

@notchris
Copy link
Owner

I got this working without any errors, but there is a warning now react-i18next:: You will need to pass in an i18next instance by using initReactI18next

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants