i18next/react-i18next

18n.exists() not working properly?

MattEzekiel opened this issue · 6 comments

🐛 Bug Report

I'm using i18next.addResourceBundle and the hook useTranslation and I have a functión that has to check before translate or not

To Reproduce

A minimal reproducible example.
A codesandbox example or similar
or at least steps to reproduce the behavior:

import i18next from 'i18next';
import es from './i18n/es';
import en from './i18n/en';
i18next.addResourceBundle('es', 'TableExporter', es);
i18next.addResourceBundle('en', 'TableExporter', en);

function TableExporter(props) {
  const { t, i18n } = useTranslation('TableExporter');
// Rest of my code

const translateValues = (data, translateFunction) => {
    console.log('assign already exists, duplicate entry existe:', i18n.exists('assign already exists, duplicate entry'));
    console.log('RESULTS existe:', i18n.exists('RESULTS'));
    // More code...
  };

And this are my es file:

const locale = {
  RESULTS: 'Resultados',
  REASON: 'RAZON',
  PARAMS: 'DATOS',
  SUCCESS: 'ESTADO',
  false: 'Error',
  true: 'Éxito',
  'assign already exists, duplicate entry': 'Ya existe la asignación, registro duplicado',
};

export default locale;

And this are my en file:

const locale = {
  RESULTS: 'Results',
  REASON: 'Reason',
  PARAMS: 'Data',
  SUCCESS: 'State',
  false: 'Error',
  true: 'Success',
  'assign already exists, duplicate entry': 'Assign already exists, duplicate entry',
};

export default locale;

Expected behavior

So the both console.log should be returning true because their exist in both files. But their are returning false

Your Environment

  • runtime version: node 18
  • i18next version: "i18next": "22.5.0", "react-i18next": "12.3.1"
  • os: Windows
  • "react": "18.2.0",

Please create a minimal reproducible example.....
I'm not able to reproduce it: https://codesandbox.io/p/sandbox/affectionate-keldysh-gcgdr2

I think you could:
image
This is returning false while importing the assets and having a key ('RESULTS') in those files. Both should be returning true... Or I'm making or understanding something wrong?

Thank you with this is working!

i18next.init({
  debug: true,
  lng: 'es',
  fallbackLng: 'en',
  defaultNS: 'TableExporter',
});

If I set the resource it messed up my app, because I'm using this in other places... I think...

I will close this issue.

If you are not using an i18next backend you should really pass the resources option... else you might have timing issues on init