简体   繁体   中英

Download different files according to browser language

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;

Rename to what contains the lang attribute.

eg "cv_pt" or "cv_en".

Then you can get the target file with the lang attribute.

const onDownloadCV = async () => {
  const lang = document.getElementsByTagName("html")[0].getAttribute("lang");
  const targetCV = await require(`../assets/cv_${lang}.pdf`);
  ... ... ...
};

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 lang = i18next.language;
// lang will be string such as: "en", "fr", ...
// now you can use it to specify the relevant language file.
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM