Created
September 19, 2019 14:06
-
-
Save AlexanderProd/ea90204ba91e7f1d80bba97fe921b6d0 to your computer and use it in GitHub Desktop.
custom React hook that returns the default greeting of your users language based on navigator.language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react' | |
const dict = { | |
"af": "Goeie dag", | |
"sq": "Tungjatjeta", | |
"ar": "Ahlan bik", | |
"zh": "Nín hao", | |
"hr": "Zdravo", | |
"cs": "Nazdar", | |
"da": "Hallo", | |
"nl": "Hallo", | |
"en": "Hello", | |
"de": "Hallo", | |
"fi": "Hei", | |
"fr": "Bonjour", | |
"el": "Geia", | |
"he": "Shalóm", | |
"hi": "Namasté", | |
"hu": "Szia", | |
"is": "halló", | |
"id": "Hai", | |
"ga": "Dia duit", | |
"it": "Buongiorno", | |
"ja": "Kónnichi wa", | |
"kn": "Hello", | |
"ko": "Annyeonghaseyo", | |
"la": "Ave", | |
"lv": "Es mīlu tevi", | |
"lt": "Sveiki", | |
"lb": "Hallo", | |
"no": "Hallo", | |
"fa": "Salâm", | |
"pl": "Witajcie", | |
"pt": "Olá", | |
"pa": "ਹੈਲੋ", | |
"ro": "Romanian", | |
"ru": "Privét", | |
"sr": "ćao", | |
"sk": "Nazdar", | |
"sl": "Zdravo", | |
"es": "Hola", | |
"sv": "Hej", | |
"tr": "Merhaba", | |
"uk": "Pryvít", | |
"vi": "Chào" | |
} | |
const useGreeting = () => { | |
const [greeting, setGreeting] = useState('Hello') | |
useEffect(() => { | |
const lang = navigator.language || window.navigator.language | |
Object.keys(dict).map(elem => { | |
if (lang.includes(elem)) { | |
setGreeting(dict[elem]) | |
} | |
}) | |
}, []) | |
return greeting | |
} | |
export default useGreeting |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment