[英]Display different language text in <noscript> tag, depending on browser language
我是 HTML 的新手,我想在禁用 JavaScript 時顯示一條消息。 所以我將消息放在<noscript>
標記中,效果很好。
<noscript> Need to enable Javascript. </noscript>
但是當瀏覽器語言為中文時,我想將此語言更改為中文。 我不能使用 JavaScript 因為此消息僅在 JavaScript 被禁用時顯示。 有沒有辦法解決這個問題? 謝謝!
當瀏覽器使用 PHP 發出 HTTP 請求時,它會發送標頭。
因此,您可以嘗試使用:
_SERVER["HTTP_ACCEPT_LANGUAGE"]
或者
_SERVER["HTTP_ACCEPT_CHARSET"]
您可以在帶有 Transcrypt 的瀏覽器中使用 Python
Download Python from www.python.org Install Transcrypt from the command prompt by typing python -m pip install transcrypt Create a new folder hello containing hello.html and hello.py Go to that new folder and type python -m transcrypt -b -m -n hello.py
In that same new folder start an HTTP server by typing python -m http.server In your browser, navigate to localhost:8000/hello.html to see the result
print request.META['HTTP_ACCEPT_LANGUAGE']
如果我得到你真正想要的東西並假設你不更改 HTML 文件,這很容易,請執行以下操作
const userLang = navigator.language || navigator.userLanguage; //1-this detects the browser language
//2-in my case i use en-US, change it to zh-sg or any other language iso for chinese
if(userLang === "en-US"){
//3-i used a nonScript class on <nonscript> tag to chnage the language
document.querySelector(".nonScript").textContent = "Change to chinese language";
}
你可以使用alert(userLang);
檢測您的瀏覽器語言並使用您獲得的iso作為if條件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.