react-pdf icon indicating copy to clipboard operation
react-pdf copied to clipboard

Translation not working inside of PDF document

Open mirankorkmaz opened this issue 3 years ago • 21 comments

I'm using the useTranslation hook in react to place inside my <Text></Text>, however it isn't rendering out the text, only the name of variable I'm declaring. Is this a well known issue or should it supposed to work if done correct?

mirankorkmaz avatar Jul 19 '22 11:07 mirankorkmaz

+1

https://github.com/diegomura/react-pdf/issues/1734#issuecomment-1190760837

sagargulati avatar Jul 27 '22 16:07 sagargulati

@sagargulati I might be solving this issue tomorrow, I will let you know if I come up with a solution.

mirankorkmaz avatar Jul 27 '22 19:07 mirankorkmaz

I'm also trying; let me know if you find a fix!

sagargulati avatar Jul 30 '22 13:07 sagargulati

Not sure if this will help you guys and there is probably a better solution, but I had the same problem with react-pdf and I solved it by wrapping react-pdf Document with react-i18next I18nextProvider.

const blob = await pdf(
  <I18nextProvider i18n={i18next}>
    <CVDocument values={formData} />
  </I18nextProvider>
).toBlob();

And I have standard call to i18next.init in index.js of the react app project.

i18next.init({
  interpolation: { escapeValue: false }, // React already does escaping
  lng: 'en', // language to use
  resources: {
    en: {
      main: main_en, // 'main' is our custom namespace
      form: form_en,
    },
    sr: {
      main: main_sr,
      form: form_sr,
    },
  },
});

dragi-ns avatar Jul 31 '22 07:07 dragi-ns

It doesn't seem to work inside the API /api/generate/pdf endpoints

sagargulati avatar Aug 09 '22 14:08 sagargulati

I managed to get it work with react-intl

BorntraegerMarc avatar Sep 09 '22 11:09 BorntraegerMarc

I wouldn't recommend switching to react specific package while using a framework like next.js

sagargulati avatar Sep 09 '22 19:09 sagargulati

react-intl is works with Next.js too. I'm using it for a custom SSR setup and works without issues.

escapedcat avatar Sep 10 '22 05:09 escapedcat

Can you share an example?

sagargulati avatar Oct 13 '22 14:10 sagargulati

any update on this issue?

I'm working on a nextjs project and use next-i18next for i18n feature. About this issue, I try to work around by import the i18n json in pdf-related component directly: import de from '@public/locales/de/crf.json'; import en from '@public/locales/en/crf.json';

and pass locale/language in the component to get the corresponding translation by language: locale === 'en' && en[your-translation-key] || '' locale === 'de' && de[your-translation-key] || ''

still, hope there is a way to make translation work in pdf document.

sdlfkorg avatar May 17 '23 04:05 sdlfkorg