Add detectLanguage.md

This commit is contained in:
Pataar
2020-10-05 17:03:26 +02:00
committed by GitHub
parent 9e6882f04e
commit 2199e3a65b

View File

@ -0,0 +1,22 @@
---
title: detectLanguage
tags: browser,intermediate
---
Detects what the language of the current user is.
- Retrieve the current language by looking in several places of the navigator object.
```js
const detectLanguage = () => {
if (navigator.languages && navigator.languages.length) {
return navigator.languages[0];
} else {
return navigator.userLanguage || navigator.language || navigator.browserLanguage || 'en-US';
}
};
```
```js
detectLanguage(); // "nl-NL"
```