Skip to content

Commit

Permalink
feat(i18n): add react-intl and local state
Browse files Browse the repository at this point in the history
  • Loading branch information
pwambach committed Oct 8, 2019
1 parent 8656882 commit 79766f0
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 17 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/ban-ts-ignore": 0,
"@typescript-eslint/no-unused-vars": 2,
"react/prop-types": 0
}
}
4 changes: 4 additions & 0 deletions i18n/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"layerSelector.main": "Haupt",
"layerSelector.compare": "Vergleich"
}
4 changes: 4 additions & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"layerSelector.main": "Main",
"layerSelector.compare": "Compare"
}
81 changes: 75 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"cesium": "^1.61.0",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-intl": "^3.3.2",
"react-redux": "^7.1.1",
"redux": "^4.0.4",
"redux-logger": "^3.0.6",
Expand Down
31 changes: 31 additions & 0 deletions src/scripts/actions/set-locale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {ThunkDispatch} from 'redux-thunk';

import {State} from '../reducers/index';
import fetchLayers, {FetchLayersActions} from './fetch-layers';

export const SET_LOCALE = 'SET_LOCALE';

export enum Locale {
EN = 'en',
DE = 'de'
}

export interface SetLocaleAction {
type: typeof SET_LOCALE;
locale: Locale;
}

type AllThunkActions = SetLocaleAction | FetchLayersActions;

const setLocaleAction = (locale: Locale) => (
dispatch: ThunkDispatch<State, void, AllThunkActions>
) => {
dispatch({
type: SET_LOCALE,
locale
});

dispatch(fetchLayers());
};

export default setLocaleAction;
31 changes: 23 additions & 8 deletions src/scripts/components/app/app.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
import React, {FunctionComponent} from 'react';
import {createStore, applyMiddleware} from 'redux';
import {Provider} from 'react-redux';
import {Provider as StoreProvider, useSelector} from 'react-redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import {IntlProvider} from 'react-intl';

import rootReducer from '../../reducers/index';
import LayerSelector from '../layer-selector/layer-selector';
import Globe from '../globe/globe';
import Menu from '../menu/menu';

import {localeSelector} from '../../reducers/locale';
import translations from '../../i18n';

import styles from './app.styl';

const store = createStore(rootReducer, applyMiddleware(thunk, logger));

const App: FunctionComponent<{}> = () => (
<Provider store={store}>
<div className={styles.app}>
<Globe />
<LayerSelector />
<Menu />
</div>
</Provider>
<StoreProvider store={store}>
<TranslatedApp />
</StoreProvider>
);

const TranslatedApp: FunctionComponent<{}> = () => {
const locale = useSelector(localeSelector);

return (
<IntlProvider locale={locale} messages={translations[locale]}>
<div className={styles.app}>
<Globe />
<LayerSelector />
<Menu />
</div>
</IntlProvider>
);
};

export default App;
7 changes: 5 additions & 2 deletions src/scripts/components/layer-selector/layer-selector.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, {FunctionComponent, useEffect, useState} from 'react';
import {useSelector, useDispatch} from 'react-redux';
import {useIntl} from 'react-intl';

import {layersSelector} from '../../reducers/layers';
import {selectedLayerIdSelector} from '../../reducers/selected-layer-id';
import fetchLayers from '../../actions/fetch-layers';
Expand All @@ -9,17 +11,18 @@ import Tabs from '../tabs/tabs';
import styles from './layer-selector.styl';

const LayerSelector: FunctionComponent<{}> = () => {
const intl = useIntl();
const layers = useSelector(layersSelector);
const layerIds = useSelector(selectedLayerIdSelector);
const dispatch = useDispatch();
const tabs = [
{
id: 'main',
label: 'Main'
label: intl.formatMessage({id: 'layerSelector.main'})
},
{
id: 'compare',
label: 'Compare'
label: intl.formatMessage({id: 'layerSelector.compare'})
}
];

Expand Down
7 changes: 7 additions & 0 deletions src/scripts/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import en from '../../i18n/en.json';
import de from '../../i18n/de.json';

export default {
en,
de
};
2 changes: 2 additions & 0 deletions src/scripts/reducers/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {combineReducers} from 'redux';

import localeReducer from './locale';
import layersReducer from './layers';
import selectedLayerReducer from './selected-layer-id';

const rootReducer = combineReducers({
locale: localeReducer,
layers: layersReducer,
selectedLayer: selectedLayerReducer
});
Expand Down
22 changes: 22 additions & 0 deletions src/scripts/reducers/locale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {State} from './index';
import {SET_LOCALE, Locale, SetLocaleAction} from '../actions/set-locale';

const initialState: Locale = Locale.EN;

function localeReducer(
localeState: Locale = initialState,
action: SetLocaleAction
): Locale {
switch (action.type) {
case SET_LOCALE:
return action.locale;
default:
return localeState;
}
}

export function localeSelector(state: State): Locale {
return state.locale;
}

export default localeReducer;
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"target": "es5",
"jsx": "react"
"jsx": "react",
"resolveJsonModule": true
}
}

0 comments on commit 79766f0

Please sign in to comment.