'Display different language text in <noscript> tag, depending on browser language
I'm new to HTML, and I want to display a message when JavaScript is disabled. So I put the message inside <noscript>
tag, which works fine.
<noscript> Need to enable Javascript. </noscript>
But I want to change this language to Chinese when the browser language is Chinese. I can't use JavaScript because this message only displays when JavaScript is disabled. Is there any way to solve this? Thank you!
Solution 1:[1]
When a browser makes an HTTP request, it sends an Accept-Language
request HTTP header.
So, for example if you are using PHP, you could use the following to get this header value:
$_SERVER["HTTP_ACCEPT_LANGUAGE"]
or
$_SERVER["HTTP_ACCEPT_CHARSET"]
You can use this value to decide on the server-side which language is being used and insert the text in the correct language.
Solution 2:[2]
You can use Python in browser with Transcrypt
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']
Solution 3:[3]
that's quite easy if I'm getting what you actually want and assuming that you dont change the HTML file, do as below
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";
}
you can use alert(userLang);
to detect your browser language and use the iso u get for the if condition
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ivar |
Solution 2 | |
Solution 3 | sham |