I have implemented i18n into my app and the only way i found to implement the language switch was with a button for every language, like this:
<div>
{Object.keys(lngs).map((lng) => (
<button key={lng} style={{ fontWeight: i18n.resolvedLanguage === lng ? 'bold' : 'normal' }} type="submit" onClick={() => i18n.changeLanguage(lng)}>
{lngs[lng].nativeName}
</button>
))}
</div>
i wanna switch it to a dropdown menu, can someone help?
2
Answers
create a state and set it to default locale:
use the select element to choose from the available locales:
for the handleLanguageChange function:
Please make necessary changes in this code according to your usecase and check if this works for you. 🙂
If you just need a dropdown here is one option. You can create an array of languages to map through and display/set on change.