diff options
| author | Ben Woodward <ben@bdw.to> | 2021-08-06 07:52:22 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-06 07:52:22 -0700 |
| commit | 8be42a4e57928f82830be71fa6c9bc17ac63aa8a (patch) | |
| tree | 584b315672d65cb5ad8d296980bf56bf4041c450 /src/App.js | |
| parent | 3a63d9fb3fcc2750be155fa67008887bf38687ac (diff) | |
Refactor translation plumbing (#168)
- Centralize user-defined translations into a single file
- Automatically sort the language dropdown
- Update the translation contribution guide
Diffstat (limited to 'src/App.js')
| -rw-r--r-- | src/App.js | 24 |
1 files changed, 11 insertions, 13 deletions
| @@ -1,13 +1,11 @@ | |||
| 1 | import { Button, Heading, Link, Pane, Paragraph } from 'evergreen-ui'; | 1 | import { Button, Heading, Link, Pane, Paragraph } from 'evergreen-ui'; |
| 2 | import React, { useEffect, useRef, useState } from 'react'; | 2 | import React, { useRef, useState } from 'react'; |
| 3 | import { useTranslation } from 'react-i18next'; | 3 | import { useTranslation } from 'react-i18next'; |
| 4 | import logo from '../src/images/wifi.png'; | 4 | import logo from '../src/images/wifi.png'; |
| 5 | import { Settings } from './components/Settings'; | 5 | import { Settings } from './components/Settings'; |
| 6 | import { WifiCard } from './components/WifiCard'; | 6 | import { WifiCard } from './components/WifiCard'; |
| 7 | import './style.css'; | 7 | import './style.css'; |
| 8 | 8 | import { Translations } from './translations'; | |
| 9 | /* List of languages that require RTL direction (alphabetic order). */ | ||
| 10 | const RTL_LANGUAGES = ['ar', 'fa-IR']; | ||
| 11 | 9 | ||
| 12 | function App() { | 10 | function App() { |
| 13 | const html = document.querySelector('html'); | 11 | const html = document.querySelector('html'); |
| @@ -30,8 +28,14 @@ function App() { | |||
| 30 | passwordError: '', | 28 | passwordError: '', |
| 31 | }); | 29 | }); |
| 32 | 30 | ||
| 31 | const htmlDirection = (languageID) => { | ||
| 32 | languageID = languageID || i18n.language; | ||
| 33 | const rtl = Translations.filter((t) => t.id === languageID)[0].rtl; | ||
| 34 | return rtl ? 'rtl' : 'ltr'; | ||
| 35 | }; | ||
| 36 | |||
| 33 | const onChangeLanguage = (language) => { | 37 | const onChangeLanguage = (language) => { |
| 34 | html.style.direction = RTL_LANGUAGES.includes(language) ? 'rtl' : 'ltr'; | 38 | html.style.direction = htmlDirection(language); |
| 35 | i18n.changeLanguage(language); | 39 | i18n.changeLanguage(language); |
| 36 | }; | 40 | }; |
| 37 | 41 | ||
| @@ -84,16 +88,10 @@ function App() { | |||
| 84 | setSettings({ ...settings, hidePassword }); | 88 | setSettings({ ...settings, hidePassword }); |
| 85 | }; | 89 | }; |
| 86 | const onFirstLoad = () => { | 90 | const onFirstLoad = () => { |
| 91 | html.style.direction = htmlDirection(); | ||
| 87 | firstLoad.current = false; | 92 | firstLoad.current = false; |
| 88 | }; | 93 | }; |
| 89 | 94 | ||
| 90 | useEffect(() => { | ||
| 91 | /* handle the edge case of the initial render requiring RTL direction */ | ||
| 92 | if (RTL_LANGUAGES.includes(i18n.language)) { | ||
| 93 | html.style.direction = 'rtl'; | ||
| 94 | } | ||
| 95 | }); | ||
| 96 | |||
| 97 | return ( | 95 | return ( |
| 98 | <Pane> | 96 | <Pane> |
| 99 | <Pane display="flex"> | 97 | <Pane display="flex"> |
| @@ -116,7 +114,7 @@ function App() { | |||
| 116 | </Pane> | 114 | </Pane> |
| 117 | 115 | ||
| 118 | <WifiCard | 116 | <WifiCard |
| 119 | direction={RTL_LANGUAGES.includes(i18n.language) ? 'rtl' : 'ltr'} | 117 | direction={htmlDirection()} |
| 120 | settings={settings} | 118 | settings={settings} |
| 121 | ssidError={errors.ssidError} | 119 | ssidError={errors.ssidError} |
| 122 | passwordError={errors.passwordError} | 120 | passwordError={errors.passwordError} |
