How can I download different files according to the lang attribute in HTML?
Currently, my code is looking like this:
import { useTranslation } from "react-i18next";
import ENCV from "../assets/englishCV.pdf";
import PTCV from "../assets/portugueseCV.pdf";
const CTA = () => {
const { t } = useTranslation();
const onDownloadCV = () => {
if(document.getElementsByTagName("html")[0].getAttribute("lang") === "pt") {
}
};
return(
<div className="cta">
<a href={} download className="btn">{t("downloadCV")}</a>
<a href="#contact" className="btn btn-primary">{t("contactButton")}</a>
</div>
);
};
export default CTA;
CodePudding user response:
Rename to what contains the lang attribute.
e.g. "cv_pt" or "cv_en".
Then you can get the target file with the lang attribute.
const onDownloadCV = () => {
const lang = document.getElementsByTagName("html")[0].getAttribute("lang");
const targetCV = await require(`../assets/cv_${lang}.pdf`);
... ... ...
};
CodePudding user response:
Since you are using i18next
, it would be better to get the current language directly from i18next
instead of the DOM:
import i18next from "i18next";
// inside component:
const onDownloadCV = () => {
const lang = i18next.language;
// lang will be string such as: "en", "fr", ...
// now you can use it to specify the relevant language file.
...
};