简体   繁体   English

根据浏览器语言下载不同的文件

[英]Download different files according to browser language

How can I download different files according to the lang attribute in HTML?如何根据 HTML 中的 lang 属性下载不同的文件?

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.重命名为包含 lang 属性的内容。

eg "cv_pt" or "cv_en".例如“cv_pt”或“cv_en”。

Then you can get the target file with the lang attribute.然后就可以得到带有 lang 属性的目标文件了。

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:由于您使用的是i18next ,因此最好直接从i18next而不是 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.
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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