diff options
| author | Ben Woodward <ben@bdw.to> | 2021-08-08 20:58:51 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-08 20:58:51 -0700 |
| commit | 1f753e8fb1c3d44b938a1ec541587cd6301f4181 (patch) | |
| tree | c1c101ddab714b18e5d6c08cd0860d2ae1f84e04 /src/components/Settings.js | |
| parent | 2f52ba13dcaeaa3336a542e6bd9847a5e1ad13c9 (diff) | |
Dynamic language select defaultValue (#182)
Replaces the hardcoded 'en-US' defaultValue of the language select with
the value supplied by the i18n language detection. In my testing I found
when setting my browser to "German" or "Dutch", the navigator.language
returned is simply "de" and "nl" but the translation ids in the translations
are "de-DE" and "nl-NL", respectively. This results in the select box
defaulting to the first language (Arabic) because "de" and "nl" are not
values in any of the options.
This patch takes the i18n detected language and first checks if the id
is a known translation. If not, 'en-US' is returned. If it is, the
detected language is returned.
Unfortunately I am not able to test this as both my Firefox and Chrome
browsers are only setting the generic locale language ids. I can
verify the fallback to english works. Additionally, editing the
localStorage 'i18nextLng' key to 'de-DE' produces the expected
translation.
Related to #180
Diffstat (limited to 'src/components/Settings.js')
| -rw-r--r-- | src/components/Settings.js | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/src/components/Settings.js b/src/components/Settings.js index 1e07cb1..f12fe35 100644 --- a/src/components/Settings.js +++ b/src/components/Settings.js | |||
| @@ -13,6 +13,14 @@ export const Settings = (props) => { | |||
| 13 | { label: 'WEP', value: 'WEP' }, | 13 | { label: 'WEP', value: 'WEP' }, |
| 14 | ]; | 14 | ]; |
| 15 | 15 | ||
| 16 | const langSelectDefaultValue = () => { | ||
| 17 | const t = Translations.filter((t) => t.id === i18n.language); | ||
| 18 | if (t.length !== 1) { | ||
| 19 | return 'en-US'; | ||
| 20 | } | ||
| 21 | return t[0].id; | ||
| 22 | }; | ||
| 23 | |||
| 16 | useEffect(() => { | 24 | useEffect(() => { |
| 17 | if (props.firstLoad.current && window.innerWidth < 500) { | 25 | if (props.firstLoad.current && window.innerWidth < 500) { |
| 18 | props.onFirstLoad(); | 26 | props.onFirstLoad(); |
| @@ -26,9 +34,8 @@ export const Settings = (props) => { | |||
| 26 | width={300} | 34 | width={300} |
| 27 | inputHeight={38} | 35 | inputHeight={38} |
| 28 | label={t('select')} | 36 | label={t('select')} |
| 29 | selected={i18n.language} | ||
| 30 | onChange={(e) => props.onLanguageChange(e.target.value)} | 37 | onChange={(e) => props.onLanguageChange(e.target.value)} |
| 31 | defaultValue="en-US" | 38 | defaultValue={langSelectDefaultValue()} |
| 32 | > | 39 | > |
| 33 | {Translations.map((t) => ( | 40 | {Translations.map((t) => ( |
| 34 | <option key={t.id} value={t.id}> | 41 | <option key={t.id} value={t.id}> |
