Browser language and cross-browsing: ko vs ko-KR in navigator.language
While working on a company project recently, I had to build a feature that hides price information only from users in Korea. As I worked through several conditions, I ran into the question how can I detect the language set in the browser?, and in the process I learned about the Navigator object in Web APIs.
The Navigator object has a property called language, and its value is the language the user set in the browser. But one more problem came up here. Each browser expressed Korean differently.
* This may differ between versions.
- Usually Chrome and Edge: ko
- Usually Safari, Firefox, Opera: ko-KR
Even for the same Korean, the value comes back as ko or ko-KR depending on the browser, so I needed to handle both.
To check the exact behavior, I ran tests myself in the console of the major browsers.
Chrome
navigator.language: ko
navigator.userLanguage: undefined
Edge
navigator.language: ko
navigator.userLanguage: undefined
Safari
navigator.language: ko-KR
navigator.userLanguage: undefined
FireFox
navigator.language: ko-KR
navigator.userLanguage: undefined
Opera
navigator.language: ko-KR
navigator.userLanguage: undefined
The tests showed that navigator.language works as expected, while navigator.userLanguage shows undefined in modern browsers other than Internet Explorer (IE).
A blog post I referred to said navigator.userLanguage is useful in IE, so I tried testing it in the IE console myself, but I wasn't able to.
Also, checking MDN's Browser Compatibility section clearly confirmed that modern browsers other than IE no longer support navigator.userLanguage.
In the end, since this feature had several other conditions besides the browser language setting, I decided not to add special handling for IE.
- #navigator.language
- #Web APIs
- #browser language
- #cross-browser compatibility