[英]Download different files according to browser language
如何根据 HTML 中的 lang 属性下载不同的文件?
目前,我的代码如下所示:
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;
重命名为包含 lang 属性的内容。
例如“cv_pt”或“cv_en”。
然后就可以得到带有 lang 属性的目标文件了。
const onDownloadCV = async () => {
const lang = document.getElementsByTagName("html")[0].getAttribute("lang");
const targetCV = await require(`../assets/cv_${lang}.pdf`);
... ... ...
};
由于您使用的是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.