react-i18next: Type-safe translations do not work with keyPrefix

🐛 Bug Report

After setting up a type-safe translations and having a translation file like

{
  "home": {
    "admin_settings": "Admin settings",
  }
}

To Reproduce

The following thing works (and compiles)

  const b = useTranslation('translation');
  b.t('home.admin_settings');

But the following doesn’t compile (typescript complains)

  const b = useTranslation('translation', { keyPrefix: 'home' });
  b.t('admin_settings');

Expected behavior

The latter examples compiles without errors

Your Environment

  • i18next version: 21.2.4
  • react-i18next version: 11.12.0
  • os: Windows

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 23 (13 by maintainers)

Most upvoted comments

I’ll look into it

Thanks!

Just one case: for me it only works if keyPrefix is without a dot, I mean the following still errors:

// you can define a keyPrefix to be used for the resulting t function
const { t } = useTranslation('translation', { keyPrefix: 'very.deeply.nested' });
const text = t('key'); // "here"

(example is from documentation)

Thank you very much for your help @pedrodurek

fixed in v11.13.0

I wasn’t aware of such feature. keyPrefix definitely needs to work with it. Would like to help, but unfortunately I won’t be able to work on that in the next 2-3 weeks due to my workload.

It works as expected! Thanks, @pedrodurek

Throwing my name in the hat as another dev that would love this fix 🙏 Thank you for working on this!

Same issue here, it would be excellent to have that. Also, making the own ‘keyPrefix’ to be type-safe would be great.

I’ll be taking a look, and measuring the compilation time. For now, the workaround is this:

const b = useTranslation('translation');
b.t('home.admin_settings');

// or

const b = useTranslation('translation');
const keyPrefix = 'home' as const;
b.t(`${keyPrefix}.admin_settings`);