From 8d953869da0b79941987ab1dc6eb00e643ee5d81 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 2 Sep 2021 20:44:13 -0700 Subject: Move localization provider --- modern/.eslintrc.js | 2 +- modern/src/App.js | 2 +- modern/src/DevicePage.js | 2 +- modern/src/EditCollectionView.js | 2 +- modern/src/EditItemView.js | 2 +- modern/src/GeofencePage.js | 2 +- modern/src/GeofencesPage.js | 2 +- modern/src/LocalizationProvider.js | 170 +++++++++++++++++++++ modern/src/MainPage.js | 2 +- modern/src/MainToolbar.js | 2 +- modern/src/PositionPage.js | 2 +- modern/src/RegisterDialog.js | 2 +- modern/src/RemoveDialog.js | 2 +- modern/src/UserPage.js | 2 +- modern/src/admin/ServerPage.js | 2 +- modern/src/admin/StatisticsPage.js | 2 +- modern/src/admin/UsersPage.js | 2 +- modern/src/attributes/AddAttributeDialog.js | 2 +- modern/src/attributes/EditAttributesView.js | 2 +- modern/src/attributes/deviceAttributes.js | 2 +- modern/src/attributes/geofenceAttributes.js | 2 +- modern/src/attributes/positionAttributes.js | 2 +- modern/src/attributes/userAttributes.js | 2 +- modern/src/common/formatter.js | 2 +- modern/src/common/localization.js | 170 --------------------- modern/src/components/BottomNav.js | 2 +- modern/src/components/registration/LoginForm.js | 2 +- modern/src/components/registration/RegisterForm.js | 2 +- modern/src/map/Map.js | 2 +- modern/src/map/StatusView.js | 2 +- modern/src/reports/ChartReportPage.js | 2 +- modern/src/reports/EventReportPage.js | 2 +- modern/src/reports/ReplayPage.js | 2 +- modern/src/reports/ReportFilter.js | 2 +- modern/src/reports/ReportLayout.js | 2 +- modern/src/reports/RouteReportPage.js | 2 +- modern/src/reports/StopReportPage.js | 2 +- modern/src/reports/SummaryReportPage.js | 2 +- modern/src/reports/TripReportPage.js | 2 +- modern/src/settings/ComputedAttributePage.js | 2 +- modern/src/settings/ComputedAttributesPage.js | 2 +- modern/src/settings/DriverPage.js | 2 +- modern/src/settings/DriversPage.js | 2 +- modern/src/settings/GroupPage.js | 2 +- modern/src/settings/GroupsPage.js | 2 +- modern/src/settings/MaintenancePage.js | 2 +- modern/src/settings/MaintenancesPage.js | 2 +- modern/src/settings/NotificationPage.js | 2 +- modern/src/settings/NotificationsPage.js | 2 +- modern/src/settings/OptionsLayout/index.js | 2 +- modern/src/settings/OptionsLayout/useRoutes.js | 2 +- 51 files changed, 219 insertions(+), 219 deletions(-) create mode 100644 modern/src/LocalizationProvider.js delete mode 100644 modern/src/common/localization.js diff --git a/modern/.eslintrc.js b/modern/.eslintrc.js index a4cf3053..ace20277 100644 --- a/modern/.eslintrc.js +++ b/modern/.eslintrc.js @@ -6,7 +6,7 @@ module.exports = { plugins: [ 'react', ], - ignorePatterns: ['serviceWorker.js', 'localization.js', 'switcher.js'], + ignorePatterns: ['serviceWorker.js', 'switcher.js'], rules: { 'max-len': [0], 'no-shadow': [0], diff --git a/modern/src/App.js b/modern/src/App.js index 5143ad88..eb1a8818 100644 --- a/modern/src/App.js +++ b/modern/src/App.js @@ -38,7 +38,7 @@ import ResetPasswordForm from './components/registration/ResetPasswordForm'; import theme from './theme'; import GeofencesPage from './GeofencesPage'; import GeofencePage from './GeofencePage'; -import { LocalizationProvider } from './common/localization'; +import { LocalizationProvider } from './LocalizationProvider'; const App = () => { const initialized = useSelector((state) => !!state.session.server && !!state.session.user); diff --git a/modern/src/DevicePage.js b/modern/src/DevicePage.js index 699e97c6..83b19707 100644 --- a/modern/src/DevicePage.js +++ b/modern/src/DevicePage.js @@ -5,7 +5,7 @@ import { Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, FormControlLabel, Checkbox, } from '@material-ui/core'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; -import t from './common/localization'; +import t from './LocalizationProvider'; import EditItemView from './EditItemView'; import EditAttributesView from './attributes/EditAttributesView'; import deviceAttributes from './attributes/deviceAttributes'; diff --git a/modern/src/EditCollectionView.js b/modern/src/EditCollectionView.js index 7d5e4b27..944ef61b 100644 --- a/modern/src/EditCollectionView.js +++ b/modern/src/EditCollectionView.js @@ -7,7 +7,7 @@ import Fab from '@material-ui/core/Fab'; import AddIcon from '@material-ui/icons/Add'; import { useSelector } from 'react-redux'; -import t from './common/localization'; +import t from './LocalizationProvider'; import RemoveDialog from './RemoveDialog'; const useStyles = makeStyles((theme) => ({ diff --git a/modern/src/EditItemView.js b/modern/src/EditItemView.js index e067a39f..add61004 100644 --- a/modern/src/EditItemView.js +++ b/modern/src/EditItemView.js @@ -5,7 +5,7 @@ import Container from '@material-ui/core/Container'; import Button from '@material-ui/core/Button'; import FormControl from '@material-ui/core/FormControl'; -import t from './common/localization'; +import t from './LocalizationProvider'; import { useEffectAsync } from './reactHelper'; import OptionsLayout from './settings/OptionsLayout'; diff --git a/modern/src/GeofencePage.js b/modern/src/GeofencePage.js index 06456fca..2b503a71 100644 --- a/modern/src/GeofencePage.js +++ b/modern/src/GeofencePage.js @@ -5,7 +5,7 @@ import { Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, } from '@material-ui/core'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; -import t from './common/localization'; +import t from './LocalizationProvider'; import EditItemView from './EditItemView'; import EditAttributesView from './attributes/EditAttributesView'; import geofenceAttributes from './attributes/geofenceAttributes'; diff --git a/modern/src/GeofencesPage.js b/modern/src/GeofencesPage.js index f441a2fd..d343a381 100644 --- a/modern/src/GeofencesPage.js +++ b/modern/src/GeofencesPage.js @@ -11,7 +11,7 @@ import CurrentLocationMap from './map/CurrentLocationMap'; import GeofenceEditMap from './map/GeofenceEditMap'; import GeofencesList from './GeofencesList'; -import t from './common/localization'; +import t from './LocalizationProvider'; const useStyles = makeStyles((theme) => ({ root: { diff --git a/modern/src/LocalizationProvider.js b/modern/src/LocalizationProvider.js new file mode 100644 index 00000000..fa1781b8 --- /dev/null +++ b/modern/src/LocalizationProvider.js @@ -0,0 +1,170 @@ +import React, { useState, createContext, useContext } from "react"; + +import af from '../../web/l10n/af.json'; +import ar from '../../web/l10n/ar.json'; +import az from '../../web/l10n/az.json'; +import bg from '../../web/l10n/bg.json'; +import bn from '../../web/l10n/bn.json'; +import cs from '../../web/l10n/cs.json'; +import da from '../../web/l10n/da.json'; +import de from '../../web/l10n/de.json'; +import el from '../../web/l10n/el.json'; +import en from '../../web/l10n/en.json'; +import es from '../../web/l10n/es.json'; +import fa from '../../web/l10n/fa.json'; +import fi from '../../web/l10n/fi.json'; +import fr from '../../web/l10n/fr.json'; +import he from '../../web/l10n/he.json'; +import hi from '../../web/l10n/hi.json'; +import hr from '../../web/l10n/hr.json'; +import hu from '../../web/l10n/hu.json'; +import id from '../../web/l10n/id.json'; +import it from '../../web/l10n/it.json'; +import ja from '../../web/l10n/ja.json'; +import ka from '../../web/l10n/ka.json'; +import kk from '../../web/l10n/kk.json'; +import km from '../../web/l10n/km.json'; +import ko from '../../web/l10n/ko.json'; +import lo from '../../web/l10n/lo.json'; +import lt from '../../web/l10n/lt.json'; +import lv from '../../web/l10n/lv.json'; +import ml from '../../web/l10n/ml.json'; +import mn from '../../web/l10n/mn.json'; +import ms from '../../web/l10n/ms.json'; +import nb from '../../web/l10n/nb.json'; +import ne from '../../web/l10n/ne.json'; +import nl from '../../web/l10n/nl.json'; +import nn from '../../web/l10n/nn.json'; +import pl from '../../web/l10n/pl.json'; +import pt from '../../web/l10n/pt.json'; +import pt_BR from '../../web/l10n/pt_BR.json'; +import ro from '../../web/l10n/ro.json'; +import ru from '../../web/l10n/ru.json'; +import si from '../../web/l10n/si.json'; +import sk from '../../web/l10n/sk.json'; +import sl from '../../web/l10n/sl.json'; +import sq from '../../web/l10n/sq.json'; +import sr from '../../web/l10n/sr.json'; +import sv from '../../web/l10n/sv.json'; +import ta from '../../web/l10n/ta.json'; +import th from '../../web/l10n/th.json'; +import tr from '../../web/l10n/tr.json'; +import uk from '../../web/l10n/uk.json'; +import uz from '../../web/l10n/uz.json'; +import vi from '../../web/l10n/vi.json'; +import zh from '../../web/l10n/zh.json'; +import zh_TW from '../../web/l10n/zh_TW.json'; + +const supportedLanguages = { + af: { data: af, name: 'Afrikaans' }, + ar: { data: ar, name: 'العربية' }, + az: { data: az, name: 'Azərbaycanca' }, + bg: { data: bg, name: 'Български' }, + bn: { data: bn, name: 'বাংলা' }, + cs: { data: cs, name: 'Čeština' }, + de: { data: de, name: 'Deutsch' }, + da: { data: da, name: 'Dansk' }, + el: { data: el, name: 'Ελληνικά' }, + en: { data: en, name: 'English' }, + es: { data: es, name: 'Español' }, + fa: { data: fa, name: 'فارسی' }, + fi: { data: fi, name: 'Suomi' }, + fr: { data: fr, name: 'Français' }, + he: { data: he, name: 'עברית' }, + hi: { data: hi, name: 'हिन्दी' }, + hr: { data: hr, name: 'Hrvatski' }, + hu: { data: hu, name: 'Magyar' }, + id: { data: id, name: 'Bahasa Indonesia' }, + it: { data: it, name: 'Italiano' }, + ja: { data: ja, name: '日本語' }, + ka: { data: ka, name: 'ქართული' }, + kk: { data: kk, name: 'Қазақша' }, + ko: { data: ko, name: '한국어' }, + km: { data: km, name: 'ភាសាខ្មែរ' }, + lo: { data: lo, name: 'ລາວ' }, + lt: { data: lt, name: 'Lietuvių' }, + lv: { data: lv, name: 'Latviešu' }, + ml: { data: ml, name: 'മലയാളം' }, + mn: { data: mn, name: 'Монгол хэл' }, + ms: { data: ms, name: 'بهاس ملايو' }, + nb: { data: nb, name: 'Norsk bokmål' }, + ne: { data: ne, name: 'नेपाली' }, + nl: { data: nl, name: 'Nederlands' }, + nn: { data: nn, name: 'Norsk nynorsk' }, + pl: { data: pl, name: 'Polski' }, + pt: { data: pt, name: 'Português' }, + pt_BR: { data: pt_BR, name: 'Português (Brasil)' }, + ro: { data: ro, name: 'Română' }, + ru: { data: ru, name: 'Русский' }, + si: { data: si, name: 'සිංහල' }, + sk: { data: sk, name: 'Slovenčina' }, + sl: { data: sl, name: 'Slovenščina' }, + sq: { data: sq, name: 'Shqipëria' }, + sr: { data: sr, name: 'Srpski' }, + sv: { data: sv, name: 'Svenska' }, + ta: { data: ta, name: 'தமிழ்' }, + th: { data: th, name: 'ไทย' }, + tr: { data: tr, name: 'Türkçe' }, + uk: { data: uk, name: 'Українська' }, + uz: { data: uz, name: 'Oʻzbekcha' }, + vi: { data: vi, name: 'Tiếng Việt' }, + zh: { data: zh, name: '中文' }, + zh_TW: { data: zh_TW, name: '中文 (Taiwan)' }, +}; + +const languageList = Object.entries(supportedLanguages).map((values) => ({ code: values[0], name: values[1].name })); +const languages = localStorage.getItem('language') ? [localStorage.getItem('language')] : (window.navigator.languages !== undefined ? window.navigator.languages.slice() : []); +let language = localStorage.getItem('language') || window.navigator.userLanguage || window.navigator.language; +languages.push(language); +languages.push(language.substring(0, 2)); +languages.push('en'); +for (let i = 0; i < languages.length; i++) { + language = languages[i].replace('-', '_'); + if (language in supportedLanguages) { + break; + } + if (language.length > 2) { + language = languages[i].substring(0, 2); + if (language in supportedLanguages) { + break; + } + } +} + +let selectedLanguage = supportedLanguages[language]; + +export const findStringKeys = (predicate) => { + return Object.keys(selectedLanguage.data).filter(predicate); +} + +export default key => { + return selectedLanguage.data[key]; +}; + +const setSelectedLanguage = (language) => { + selectedLanguage = supportedLanguages[language]; + localStorage.setItem('language', language); +} + +const defaultLanguage = language; + +const LocalizationContext = createContext({ + language +}); + +export function LocalizationProvider(props) { + const [language, setLanguage] = useState(defaultLanguage); + + const handleLanguageChange = (nextLanguage) => { + setSelectedLanguage(nextLanguage); + setLanguage(nextLanguage); + }; + + return + {props.children} + +} + +export const useLocalization = () => { + return useContext(LocalizationContext); +} \ No newline at end of file diff --git a/modern/src/MainPage.js b/modern/src/MainPage.js index e0707404..5a8d4ce8 100644 --- a/modern/src/MainPage.js +++ b/modern/src/MainPage.js @@ -19,7 +19,7 @@ import GeofenceMap from './map/GeofenceMap'; import CurrentPositionsMap from './map/CurrentPositionsMap'; import CurrentLocationMap from './map/CurrentLocationMap'; import BottomNav from './components/BottomNav'; -import t from './common/localization'; +import t from './LocalizationProvider'; const useStyles = makeStyles((theme) => ({ root: { diff --git a/modern/src/MainToolbar.js b/modern/src/MainToolbar.js index 59c2eca8..65b02f51 100644 --- a/modern/src/MainToolbar.js +++ b/modern/src/MainToolbar.js @@ -18,7 +18,7 @@ import PersonIcon from '@material-ui/icons/Person'; import DescriptionIcon from '@material-ui/icons/Description'; import ReplayIcon from '@material-ui/icons/Replay'; import { sessionActions } from './store'; -import t from './common/localization'; +import t from './LocalizationProvider'; import * as selectors from './common/selectors'; const useStyles = makeStyles((theme) => ({ diff --git a/modern/src/PositionPage.js b/modern/src/PositionPage.js index ce4c4ac2..51c3c856 100644 --- a/modern/src/PositionPage.js +++ b/modern/src/PositionPage.js @@ -4,7 +4,7 @@ import { makeStyles, Typography, ListItem, ListItemText, ListItemSecondaryAction, List, Container, Paper, Divider, } from '@material-ui/core'; import { useParams } from 'react-router-dom'; -import t from './common/localization'; +import t from './LocalizationProvider'; import { useEffectAsync } from './reactHelper'; import MainToolbar from './MainToolbar'; import { formatPosition } from './common/formatter'; diff --git a/modern/src/RegisterDialog.js b/modern/src/RegisterDialog.js index aafec66b..e1483bc2 100644 --- a/modern/src/RegisterDialog.js +++ b/modern/src/RegisterDialog.js @@ -6,7 +6,7 @@ import DialogContent from '@material-ui/core/DialogContent'; import DialogContentText from '@material-ui/core/DialogContentText'; import TextField from '@material-ui/core/TextField'; import Snackbar from '@material-ui/core/Snackbar'; -import t from './common/localization'; +import t from './LocalizationProvider'; const RegisterDialog = ({ showDialog, onResult }) => { const [name, setName] = useState(''); diff --git a/modern/src/RemoveDialog.js b/modern/src/RemoveDialog.js index 8ff162c2..4ba5c1e9 100644 --- a/modern/src/RemoveDialog.js +++ b/modern/src/RemoveDialog.js @@ -4,7 +4,7 @@ import Dialog from '@material-ui/core/Dialog'; import DialogActions from '@material-ui/core/DialogActions'; import DialogContent from '@material-ui/core/DialogContent'; import DialogContentText from '@material-ui/core/DialogContentText'; -import t from './common/localization'; +import t from './LocalizationProvider'; const RemoveDialog = ({ open, endpoint, itemId, onResult, diff --git a/modern/src/UserPage.js b/modern/src/UserPage.js index 6afbdf7e..d09cd631 100644 --- a/modern/src/UserPage.js +++ b/modern/src/UserPage.js @@ -5,7 +5,7 @@ import { Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, } from '@material-ui/core'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; -import t from './common/localization'; +import t from './LocalizationProvider'; import userAttributes from './attributes/userAttributes'; import EditItemView from './EditItemView'; import EditAttributesView from './attributes/EditAttributesView'; diff --git a/modern/src/admin/ServerPage.js b/modern/src/admin/ServerPage.js index ec0034f5..05190643 100644 --- a/modern/src/admin/ServerPage.js +++ b/modern/src/admin/ServerPage.js @@ -7,7 +7,7 @@ import { import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; import { useHistory } from 'react-router-dom'; import { useDispatch, useSelector } from 'react-redux'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import { sessionActions } from '../store'; import EditAttributesView from '../attributes/EditAttributesView'; import deviceAttributes from '../attributes/deviceAttributes'; diff --git a/modern/src/admin/StatisticsPage.js b/modern/src/admin/StatisticsPage.js index 1f32c3d9..96ee4d18 100644 --- a/modern/src/admin/StatisticsPage.js +++ b/modern/src/admin/StatisticsPage.js @@ -3,7 +3,7 @@ import { FormControl, InputLabel, Select, MenuItem, TextField, Button, TableContainer, Table, TableRow, TableCell, TableHead, TableBody, Paper, } from '@material-ui/core'; import moment from 'moment'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import { formatDate } from '../common/formatter'; import OptionsLayout from '../settings/OptionsLayout'; diff --git a/modern/src/admin/UsersPage.js b/modern/src/admin/UsersPage.js index a8b3c849..2bfbb755 100644 --- a/modern/src/admin/UsersPage.js +++ b/modern/src/admin/UsersPage.js @@ -3,7 +3,7 @@ import { TableContainer, Table, TableRow, TableCell, TableHead, TableBody, makeStyles, IconButton, } from '@material-ui/core'; import MoreVertIcon from '@material-ui/icons/MoreVert'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import { useEffectAsync } from '../reactHelper'; import EditCollectionView from '../EditCollectionView'; import { formatBoolean } from '../common/formatter'; diff --git a/modern/src/attributes/AddAttributeDialog.js b/modern/src/attributes/AddAttributeDialog.js index 24c208a5..3d392fd5 100644 --- a/modern/src/attributes/AddAttributeDialog.js +++ b/modern/src/attributes/AddAttributeDialog.js @@ -4,7 +4,7 @@ import { } from '@material-ui/core'; import { Autocomplete, createFilterOptions } from '@material-ui/lab'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; const AddAttributeDialog = ({ open, onResult, definitions }) => { const filter = createFilterOptions({ diff --git a/modern/src/attributes/EditAttributesView.js b/modern/src/attributes/EditAttributesView.js index 13ea9bd4..68395d9c 100644 --- a/modern/src/attributes/EditAttributesView.js +++ b/modern/src/attributes/EditAttributesView.js @@ -5,7 +5,7 @@ import { } from '@material-ui/core'; import CloseIcon from '@material-ui/icons/Close'; import AddIcon from '@material-ui/icons/Add'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import AddAttributeDialog from './AddAttributeDialog'; const useStyles = makeStyles((theme) => ({ diff --git a/modern/src/attributes/deviceAttributes.js b/modern/src/attributes/deviceAttributes.js index 26e3d928..2b874c52 100644 --- a/modern/src/attributes/deviceAttributes.js +++ b/modern/src/attributes/deviceAttributes.js @@ -1,4 +1,4 @@ -import t from '../common/localization'; +import t from '../LocalizationProvider'; export default { speedLimit: { diff --git a/modern/src/attributes/geofenceAttributes.js b/modern/src/attributes/geofenceAttributes.js index 59a8869d..5f197de7 100644 --- a/modern/src/attributes/geofenceAttributes.js +++ b/modern/src/attributes/geofenceAttributes.js @@ -1,4 +1,4 @@ -import t from '../common/localization'; +import t from '../LocalizationProvider'; export default { speedLimit: { diff --git a/modern/src/attributes/positionAttributes.js b/modern/src/attributes/positionAttributes.js index 94f396a1..b44b6062 100644 --- a/modern/src/attributes/positionAttributes.js +++ b/modern/src/attributes/positionAttributes.js @@ -1,4 +1,4 @@ -import t from '../common/localization'; +import t from '../LocalizationProvider'; export default { raw: { diff --git a/modern/src/attributes/userAttributes.js b/modern/src/attributes/userAttributes.js index 6f842f91..33a6657c 100644 --- a/modern/src/attributes/userAttributes.js +++ b/modern/src/attributes/userAttributes.js @@ -1,4 +1,4 @@ -import t from '../common/localization'; +import t from '../LocalizationProvider'; export default { notificationTokens: { diff --git a/modern/src/common/formatter.js b/modern/src/common/formatter.js index f0bb2e11..e9371653 100644 --- a/modern/src/common/formatter.js +++ b/modern/src/common/formatter.js @@ -1,5 +1,5 @@ import moment from 'moment'; -import t from './localization'; +import t from '../LocalizationProvider'; export const formatBoolean = (value) => (value ? t('sharedYes') : t('sharedNo')); diff --git a/modern/src/common/localization.js b/modern/src/common/localization.js deleted file mode 100644 index 90a5739a..00000000 --- a/modern/src/common/localization.js +++ /dev/null @@ -1,170 +0,0 @@ -import React, { useState, createContext, useContext } from "react"; - -import af from '../../../web/l10n/af.json'; -import ar from '../../../web/l10n/ar.json'; -import az from '../../../web/l10n/az.json'; -import bg from '../../../web/l10n/bg.json'; -import bn from '../../../web/l10n/bn.json'; -import cs from '../../../web/l10n/cs.json'; -import da from '../../../web/l10n/da.json'; -import de from '../../../web/l10n/de.json'; -import el from '../../../web/l10n/el.json'; -import en from '../../../web/l10n/en.json'; -import es from '../../../web/l10n/es.json'; -import fa from '../../../web/l10n/fa.json'; -import fi from '../../../web/l10n/fi.json'; -import fr from '../../../web/l10n/fr.json'; -import he from '../../../web/l10n/he.json'; -import hi from '../../../web/l10n/hi.json'; -import hr from '../../../web/l10n/hr.json'; -import hu from '../../../web/l10n/hu.json'; -import id from '../../../web/l10n/id.json'; -import it from '../../../web/l10n/it.json'; -import ja from '../../../web/l10n/ja.json'; -import ka from '../../../web/l10n/ka.json'; -import kk from '../../../web/l10n/kk.json'; -import km from '../../../web/l10n/km.json'; -import ko from '../../../web/l10n/ko.json'; -import lo from '../../../web/l10n/lo.json'; -import lt from '../../../web/l10n/lt.json'; -import lv from '../../../web/l10n/lv.json'; -import ml from '../../../web/l10n/ml.json'; -import mn from '../../../web/l10n/mn.json'; -import ms from '../../../web/l10n/ms.json'; -import nb from '../../../web/l10n/nb.json'; -import ne from '../../../web/l10n/ne.json'; -import nl from '../../../web/l10n/nl.json'; -import nn from '../../../web/l10n/nn.json'; -import pl from '../../../web/l10n/pl.json'; -import pt from '../../../web/l10n/pt.json'; -import pt_BR from '../../../web/l10n/pt_BR.json'; -import ro from '../../../web/l10n/ro.json'; -import ru from '../../../web/l10n/ru.json'; -import si from '../../../web/l10n/si.json'; -import sk from '../../../web/l10n/sk.json'; -import sl from '../../../web/l10n/sl.json'; -import sq from '../../../web/l10n/sq.json'; -import sr from '../../../web/l10n/sr.json'; -import sv from '../../../web/l10n/sv.json'; -import ta from '../../../web/l10n/ta.json'; -import th from '../../../web/l10n/th.json'; -import tr from '../../../web/l10n/tr.json'; -import uk from '../../../web/l10n/uk.json'; -import uz from '../../../web/l10n/uz.json'; -import vi from '../../../web/l10n/vi.json'; -import zh from '../../../web/l10n/zh.json'; -import zh_TW from '../../../web/l10n/zh_TW.json'; - -const supportedLanguages = { - af: { data: af, name: 'Afrikaans' }, - ar: { data: ar, name: 'العربية' }, - az: { data: az, name: 'Azərbaycanca' }, - bg: { data: bg, name: 'Български' }, - bn: { data: bn, name: 'বাংলা' }, - cs: { data: cs, name: 'Čeština' }, - de: { data: de, name: 'Deutsch' }, - da: { data: da, name: 'Dansk' }, - el: { data: el, name: 'Ελληνικά' }, - en: { data: en, name: 'English' }, - es: { data: es, name: 'Español' }, - fa: { data: fa, name: 'فارسی' }, - fi: { data: fi, name: 'Suomi' }, - fr: { data: fr, name: 'Français' }, - he: { data: he, name: 'עברית' }, - hi: { data: hi, name: 'हिन्दी' }, - hr: { data: hr, name: 'Hrvatski' }, - hu: { data: hu, name: 'Magyar' }, - id: { data: id, name: 'Bahasa Indonesia' }, - it: { data: it, name: 'Italiano' }, - ja: { data: ja, name: '日本語' }, - ka: { data: ka, name: 'ქართული' }, - kk: { data: kk, name: 'Қазақша' }, - ko: { data: ko, name: '한국어' }, - km: { data: km, name: 'ភាសាខ្មែរ' }, - lo: { data: lo, name: 'ລາວ' }, - lt: { data: lt, name: 'Lietuvių' }, - lv: { data: lv, name: 'Latviešu' }, - ml: { data: ml, name: 'മലയാളം' }, - mn: { data: mn, name: 'Монгол хэл' }, - ms: { data: ms, name: 'بهاس ملايو' }, - nb: { data: nb, name: 'Norsk bokmål' }, - ne: { data: ne, name: 'नेपाली' }, - nl: { data: nl, name: 'Nederlands' }, - nn: { data: nn, name: 'Norsk nynorsk' }, - pl: { data: pl, name: 'Polski' }, - pt: { data: pt, name: 'Português' }, - pt_BR: { data: pt_BR, name: 'Português (Brasil)' }, - ro: { data: ro, name: 'Română' }, - ru: { data: ru, name: 'Русский' }, - si: { data: si, name: 'සිංහල' }, - sk: { data: sk, name: 'Slovenčina' }, - sl: { data: sl, name: 'Slovenščina' }, - sq: { data: sq, name: 'Shqipëria' }, - sr: { data: sr, name: 'Srpski' }, - sv: { data: sv, name: 'Svenska' }, - ta: { data: ta, name: 'தமிழ்' }, - th: { data: th, name: 'ไทย' }, - tr: { data: tr, name: 'Türkçe' }, - uk: { data: uk, name: 'Українська' }, - uz: { data: uz, name: 'Oʻzbekcha' }, - vi: { data: vi, name: 'Tiếng Việt' }, - zh: { data: zh, name: '中文' }, - zh_TW: { data: zh_TW, name: '中文 (Taiwan)' }, -}; - -const languageList = Object.entries(supportedLanguages).map((values) => ({ code: values[0], name: values[1].name })); -const languages = localStorage.getItem('language') ? [localStorage.getItem('language')] : (window.navigator.languages !== undefined ? window.navigator.languages.slice() : []); -let language = localStorage.getItem('language') || window.navigator.userLanguage || window.navigator.language; -languages.push(language); -languages.push(language.substring(0, 2)); -languages.push('en'); -for (let i = 0; i < languages.length; i++) { - language = languages[i].replace('-', '_'); - if (language in supportedLanguages) { - break; - } - if (language.length > 2) { - language = languages[i].substring(0, 2); - if (language in supportedLanguages) { - break; - } - } -} - -let selectedLanguage = supportedLanguages[language]; - -export const findStringKeys = (predicate) => { - return Object.keys(selectedLanguage.data).filter(predicate); -} - -export default key => { - return selectedLanguage.data[key]; -}; - -const setSelectedLanguage = (language) => { - selectedLanguage = supportedLanguages[language]; - localStorage.setItem('language', language); -} - -const defaultLanguage = language; - -const LocalizationContext = createContext({ - language -}); - -export function LocalizationProvider(props) { - const [language, setLanguage] = useState(defaultLanguage); - - const handleLanguageChange = (nextLanguage) => { - setSelectedLanguage(nextLanguage); - setLanguage(nextLanguage); - }; - - return - {props.children} - -} - -export const useLocalization = () => { - return useContext(LocalizationContext); -} \ No newline at end of file diff --git a/modern/src/components/BottomNav.js b/modern/src/components/BottomNav.js index c47f0f63..fbb115e8 100644 --- a/modern/src/components/BottomNav.js +++ b/modern/src/components/BottomNav.js @@ -12,7 +12,7 @@ import MapIcon from '@material-ui/icons/Map'; import LogoutIcon from '@material-ui/icons/ExitToApp'; import { sessionActions } from '../store'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; const useStyles = makeStyles((theme) => ({ container: { diff --git a/modern/src/components/registration/LoginForm.js b/modern/src/components/registration/LoginForm.js index 9b5ca3ed..c3081d90 100644 --- a/modern/src/components/registration/LoginForm.js +++ b/modern/src/components/registration/LoginForm.js @@ -6,7 +6,7 @@ import { useTheme } from '@material-ui/core/styles'; import { useDispatch, useSelector } from 'react-redux'; import { useHistory } from 'react-router-dom'; import { sessionActions } from '../../store'; -import t, { useLocalization } from '../../common/localization'; +import t, { useLocalization } from '../../LocalizationProvider'; import StartPage from '../../StartPage'; const useStyles = makeStyles((theme) => ({ diff --git a/modern/src/components/registration/RegisterForm.js b/modern/src/components/registration/RegisterForm.js index 06f53551..2d7fa6f9 100644 --- a/modern/src/components/registration/RegisterForm.js +++ b/modern/src/components/registration/RegisterForm.js @@ -5,7 +5,7 @@ import { import { useHistory } from 'react-router-dom'; import ArrowBackIcon from '@material-ui/icons/ArrowBack'; import StartPage from '../../StartPage'; -import t from '../../common/localization'; +import t from '../../LocalizationProvider'; const useStyles = makeStyles((theme) => ({ register: { diff --git a/modern/src/map/Map.js b/modern/src/map/Map.js index 47ebb1f0..9e31fa15 100644 --- a/modern/src/map/Map.js +++ b/modern/src/map/Map.js @@ -10,7 +10,7 @@ import { prepareIcon, loadImage } from './mapUtil'; import { styleCarto, styleMapbox, styleMapTiler, styleOsm, } from './mapStyles'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import { useAttributePreference } from '../common/preferences'; import palette from '../theme/palette'; diff --git a/modern/src/map/StatusView.js b/modern/src/map/StatusView.js index 00d36c50..f4062f5a 100644 --- a/modern/src/map/StatusView.js +++ b/modern/src/map/StatusView.js @@ -18,7 +18,7 @@ import { } from '../common/formatter'; import { useAttributePreference } from '../common/preferences'; import RemoveDialog from '../RemoveDialog'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import { getPosition } from '../common/selectors'; const useStyles = makeStyles((theme) => ({ diff --git a/modern/src/reports/ChartReportPage.js b/modern/src/reports/ChartReportPage.js index c55ed6d5..ba476ef8 100644 --- a/modern/src/reports/ChartReportPage.js +++ b/modern/src/reports/ChartReportPage.js @@ -8,7 +8,7 @@ import Graph from './Graph'; import { useAttributePreference } from '../common/preferences'; import { formatDate } from '../common/formatter'; import { speedFromKnots } from '../common/converter'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; const Filter = ({ children, setItems }) => { const speedUnit = useAttributePreference('speedUnit'); diff --git a/modern/src/reports/EventReportPage.js b/modern/src/reports/EventReportPage.js index b5c3daeb..1c40c3c7 100644 --- a/modern/src/reports/EventReportPage.js +++ b/modern/src/reports/EventReportPage.js @@ -9,7 +9,7 @@ import { formatDate } from '../common/formatter'; import ReportFilter from './ReportFilter'; import ReportLayout from './ReportLayout'; import { prefixString } from '../common/stringUtils'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; const Filter = ({ setItems }) => { const [eventTypes, setEventTypes] = useState(['allEvents']); diff --git a/modern/src/reports/ReplayPage.js b/modern/src/reports/ReplayPage.js index 12bbd351..c1a8d92c 100644 --- a/modern/src/reports/ReplayPage.js +++ b/modern/src/reports/ReplayPage.js @@ -5,7 +5,7 @@ import { import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; import MainToolbar from '../MainToolbar'; import Map from '../map/Map'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import ReplayPathMap from '../map/ReplayPathMap'; import PositionsMap from '../map/PositionsMap'; import { formatPosition } from '../common/formatter'; diff --git a/modern/src/reports/ReportFilter.js b/modern/src/reports/ReportFilter.js index dec8d018..56f3e5d7 100644 --- a/modern/src/reports/ReportFilter.js +++ b/modern/src/reports/ReportFilter.js @@ -4,7 +4,7 @@ import { } from '@material-ui/core'; import { useSelector } from 'react-redux'; import moment from 'moment'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; const ReportFilter = ({ children, handleSubmit, showOnly }) => { const devices = useSelector((state) => Object.values(state.devices.items)); diff --git a/modern/src/reports/ReportLayout.js b/modern/src/reports/ReportLayout.js index 69487f28..67d2ab70 100644 --- a/modern/src/reports/ReportLayout.js +++ b/modern/src/reports/ReportLayout.js @@ -13,7 +13,7 @@ import ArrowBackIcon from '@material-ui/icons/ArrowBack'; import SideNav from '../components/SideNav'; import NavBar from '../components/NavBar'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; const useStyles = makeStyles((theme) => ({ root: { diff --git a/modern/src/reports/RouteReportPage.js b/modern/src/reports/RouteReportPage.js index fffcdcb5..185486bc 100644 --- a/modern/src/reports/RouteReportPage.js +++ b/modern/src/reports/RouteReportPage.js @@ -8,7 +8,7 @@ import { import ReportFilter from './ReportFilter'; import ReportLayout from './ReportLayout'; import { useAttributePreference, usePreference } from '../common/preferences'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; const Filter = ({ setItems }) => { const handleSubmit = async (deviceId, from, to, mail, headers) => { diff --git a/modern/src/reports/StopReportPage.js b/modern/src/reports/StopReportPage.js index d2e7e7ed..c00c2383 100644 --- a/modern/src/reports/StopReportPage.js +++ b/modern/src/reports/StopReportPage.js @@ -7,7 +7,7 @@ import { import ReportFilter from './ReportFilter'; import ReportLayout from './ReportLayout'; import { useAttributePreference } from '../common/preferences'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; const Filter = ({ setItems }) => { const handleSubmit = async (deviceId, from, to, mail, headers) => { diff --git a/modern/src/reports/SummaryReportPage.js b/modern/src/reports/SummaryReportPage.js index 4523e652..d606dda2 100644 --- a/modern/src/reports/SummaryReportPage.js +++ b/modern/src/reports/SummaryReportPage.js @@ -8,7 +8,7 @@ import { import ReportFilter from './ReportFilter'; import ReportLayout from './ReportLayout'; import { useAttributePreference } from '../common/preferences'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; const Filter = ({ setItems }) => { const [daily, setDaily] = useState(false); diff --git a/modern/src/reports/TripReportPage.js b/modern/src/reports/TripReportPage.js index f611dde1..4fa6a340 100644 --- a/modern/src/reports/TripReportPage.js +++ b/modern/src/reports/TripReportPage.js @@ -7,7 +7,7 @@ import { import ReportFilter from './ReportFilter'; import ReportLayout from './ReportLayout'; import { useAttributePreference } from '../common/preferences'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; const Filter = ({ setItems }) => { const handleSubmit = async (deviceId, from, to, mail, headers) => { diff --git a/modern/src/settings/ComputedAttributePage.js b/modern/src/settings/ComputedAttributePage.js index fea613a9..a3b45b4a 100644 --- a/modern/src/settings/ComputedAttributePage.js +++ b/modern/src/settings/ComputedAttributePage.js @@ -3,7 +3,7 @@ import { Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, FormControl, InputLabel, MenuItem, Select, TextField, } from '@material-ui/core'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import EditItemView from '../EditItemView'; import positionAttributes from '../attributes/positionAttributes'; diff --git a/modern/src/settings/ComputedAttributesPage.js b/modern/src/settings/ComputedAttributesPage.js index 9f131d33..9858ea39 100644 --- a/modern/src/settings/ComputedAttributesPage.js +++ b/modern/src/settings/ComputedAttributesPage.js @@ -4,7 +4,7 @@ import { } from '@material-ui/core'; import MoreVertIcon from '@material-ui/icons/MoreVert'; import { useSelector } from 'react-redux'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import { useEffectAsync } from '../reactHelper'; import EditCollectionView from '../EditCollectionView'; import OptionsLayout from './OptionsLayout'; diff --git a/modern/src/settings/DriverPage.js b/modern/src/settings/DriverPage.js index 01400c5c..7bb4497b 100644 --- a/modern/src/settings/DriverPage.js +++ b/modern/src/settings/DriverPage.js @@ -4,7 +4,7 @@ import { Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, } from '@material-ui/core'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import EditItemView from '../EditItemView'; import EditAttributesView from '../attributes/EditAttributesView'; diff --git a/modern/src/settings/DriversPage.js b/modern/src/settings/DriversPage.js index 03bf49c5..ad3c398e 100644 --- a/modern/src/settings/DriversPage.js +++ b/modern/src/settings/DriversPage.js @@ -3,7 +3,7 @@ import { TableContainer, Table, TableRow, TableCell, TableHead, TableBody, makeStyles, IconButton, } from '@material-ui/core'; import MoreVertIcon from '@material-ui/icons/MoreVert'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import { useEffectAsync } from '../reactHelper'; import EditCollectionView from '../EditCollectionView'; import OptionsLayout from './OptionsLayout'; diff --git a/modern/src/settings/GroupPage.js b/modern/src/settings/GroupPage.js index f9af0f8b..ad578e25 100644 --- a/modern/src/settings/GroupPage.js +++ b/modern/src/settings/GroupPage.js @@ -5,7 +5,7 @@ import { Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, } from '@material-ui/core'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import EditItemView from '../EditItemView'; import EditAttributesView from '../attributes/EditAttributesView'; import deviceAttributes from '../attributes/deviceAttributes'; diff --git a/modern/src/settings/GroupsPage.js b/modern/src/settings/GroupsPage.js index ebebd40a..89715928 100644 --- a/modern/src/settings/GroupsPage.js +++ b/modern/src/settings/GroupsPage.js @@ -3,7 +3,7 @@ import { TableContainer, Table, TableRow, TableCell, TableHead, TableBody, makeStyles, IconButton, } from '@material-ui/core'; import MoreVertIcon from '@material-ui/icons/MoreVert'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import { useEffectAsync } from '../reactHelper'; import EditCollectionView from '../EditCollectionView'; import OptionsLayout from './OptionsLayout'; diff --git a/modern/src/settings/MaintenancePage.js b/modern/src/settings/MaintenancePage.js index 89ebaa12..287d040d 100644 --- a/modern/src/settings/MaintenancePage.js +++ b/modern/src/settings/MaintenancePage.js @@ -4,7 +4,7 @@ import { } from '@material-ui/core'; import InputAdornment from '@material-ui/core/InputAdornment'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import { prefixString } from '../common/stringUtils'; import EditItemView from '../EditItemView'; import EditAttributesView from '../attributes/EditAttributesView'; diff --git a/modern/src/settings/MaintenancesPage.js b/modern/src/settings/MaintenancesPage.js index 357d0791..07d78ee9 100644 --- a/modern/src/settings/MaintenancesPage.js +++ b/modern/src/settings/MaintenancesPage.js @@ -3,7 +3,7 @@ import { TableContainer, Table, TableRow, TableCell, TableHead, TableBody, makeStyles, IconButton, } from '@material-ui/core'; import MoreVertIcon from '@material-ui/icons/MoreVert'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import { useEffectAsync } from '../reactHelper'; import EditCollectionView from '../EditCollectionView'; diff --git a/modern/src/settings/NotificationPage.js b/modern/src/settings/NotificationPage.js index 11c427fe..98104d76 100644 --- a/modern/src/settings/NotificationPage.js +++ b/modern/src/settings/NotificationPage.js @@ -4,7 +4,7 @@ import { Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, FormControlLabel, Checkbox, } from '@material-ui/core'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; -import t, { findStringKeys } from '../common/localization'; +import t, { findStringKeys } from '../LocalizationProvider'; import EditItemView from '../EditItemView'; import { prefixString, unprefixString } from '../common/stringUtils'; import SelectField from '../form/SelectField'; diff --git a/modern/src/settings/NotificationsPage.js b/modern/src/settings/NotificationsPage.js index 2f1ee8b7..273c15d5 100644 --- a/modern/src/settings/NotificationsPage.js +++ b/modern/src/settings/NotificationsPage.js @@ -3,7 +3,7 @@ import { TableContainer, Table, TableRow, TableCell, TableHead, TableBody, makeStyles, IconButton, } from '@material-ui/core'; import MoreVertIcon from '@material-ui/icons/MoreVert'; -import t from '../common/localization'; +import t from '../LocalizationProvider'; import { useEffectAsync } from '../reactHelper'; import EditCollectionView from '../EditCollectionView'; import { prefixString } from '../common/stringUtils'; diff --git a/modern/src/settings/OptionsLayout/index.js b/modern/src/settings/OptionsLayout/index.js index 625148d0..dd969196 100644 --- a/modern/src/settings/OptionsLayout/index.js +++ b/modern/src/settings/OptionsLayout/index.js @@ -13,7 +13,7 @@ import ArrowBackIcon from '@material-ui/icons/ArrowBack'; import SideNav from '../../components/SideNav'; import NavBar from '../../components/NavBar'; -import t from '../../common/localization'; +import t from '../../LocalizationProvider'; import useRoutes from './useRoutes'; const useStyles = makeStyles((theme) => ({ diff --git a/modern/src/settings/OptionsLayout/useRoutes.js b/modern/src/settings/OptionsLayout/useRoutes.js index 18320813..f38736f5 100644 --- a/modern/src/settings/OptionsLayout/useRoutes.js +++ b/modern/src/settings/OptionsLayout/useRoutes.js @@ -9,7 +9,7 @@ import BuildIcon from '@material-ui/icons/Build'; import PeopleIcon from '@material-ui/icons/People'; import BarChartIcon from '@material-ui/icons/BarChart'; import { getIsAdmin, getUserId } from '../../common/selectors'; -import t from '../../common/localization'; +import t from '../../LocalizationProvider'; const accountRoute = { name: t('settingsUser'), -- cgit v1.2.3