get locale from user if exists

This commit is contained in:
Delucse 2020-12-14 14:48:12 +01:00
parent e52e3943c1
commit c6a1e854d2
6 changed files with 48 additions and 21 deletions

View File

@ -1,8 +1,8 @@
import { MYBADGES_CONNECT, MYBADGES_DISCONNECT, USER_LOADED, USER_LOADING, AUTH_ERROR, LOGIN_SUCCESS, LOGIN_FAIL, LOGOUT_SUCCESS, LOGOUT_FAIL, REFRESH_TOKEN_SUCCESS } from '../actions/types'; import { MYBADGES_CONNECT, MYBADGES_DISCONNECT, USER_LOADED, USER_LOADING, AUTH_ERROR, LOGIN_SUCCESS, LOGIN_FAIL, LOGOUT_SUCCESS, LOGOUT_FAIL, REFRESH_TOKEN_SUCCESS } from '../actions/types';
import axios from 'axios'; import axios from 'axios';
import { returnErrors, returnSuccess } from './messageActions' import { returnErrors, returnSuccess } from './messageActions';
import { setLanguage } from './generalActions';
// Check token & load user // Check token & load user
export const loadUser = () => (dispatch) => { export const loadUser = () => (dispatch) => {
@ -16,6 +16,7 @@ export const loadUser = () => (dispatch) => {
type: USER_LOADED, type: USER_LOADED,
payload: res.data.user payload: res.data.user
}); });
dispatch(setLanguage(res.data.user.language));
}, },
error: err => { error: err => {
if(err.response){ if(err.response){
@ -61,6 +62,7 @@ export const login = ({ email, password }) => (dispatch) => {
type: LOGIN_SUCCESS, type: LOGIN_SUCCESS,
payload: res.data payload: res.data
}); });
dispatch(setLanguage(res.data.user.language));
dispatch(returnSuccess(res.data.message, res.status, 'LOGIN_SUCCESS')); dispatch(returnSuccess(res.data.message, res.status, 'LOGIN_SUCCESS'));
}) })
.catch(err => { .catch(err => {
@ -130,6 +132,11 @@ export const logout = () => (dispatch) => {
dispatch({ dispatch({
type: LOGOUT_SUCCESS type: LOGOUT_SUCCESS
}); });
var locale = 'de_DE';
if (window.localStorage.getItem('locale')) {
locale = window.localStorage.getItem('locale');
}
dispatch(setLanguage(locale));
dispatch(returnSuccess(res.data.message, res.status, 'LOGOUT_SUCCESS')); dispatch(returnSuccess(res.data.message, res.status, 'LOGOUT_SUCCESS'));
clearTimeout(logoutTimerId); clearTimeout(logoutTimerId);
}, },

View File

@ -7,7 +7,10 @@ export const visitPage = () => (dispatch) => {
}); });
}; };
export const setLanguage = (language) => (dispatch) => { export const setLanguage = (language) => (dispatch, getState) => {
if(!getState().auth.isAuthenticated){
window.localStorage.setItem('locale', language);
}
dispatch({ dispatch({
type: LANGUAGE, type: LANGUAGE,
payload: language payload: language

View File

@ -18,16 +18,9 @@ class BlocklyWindow extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.simpleWorkspace = React.createRef(); this.simpleWorkspace = React.createRef();
// if (locale === null) { if (this.props.language === 'de_DE') {
// if (navigator.language === 'de-DE') {
// locale = 'de';
// } else {
// locale = 'en';
// }
// }
if (this.props.language === 'de') {
Blockly.setLocale(De); Blockly.setLocale(De);
} else if (this.props.language === 'en') { } else if (this.props.language === 'en_US') {
Blockly.setLocale(En); Blockly.setLocale(En);
} }
} }

View File

@ -14,18 +14,18 @@ import Cookies from './Cookies';
class Content extends Component { class Content extends Component {
componentDidMount() { componentDidMount() {
if (this.props.language === 'de') { if (this.props.language === 'de_DE') {
Blockly.setLocale(De); Blockly.setLocale(De);
} else if (this.props.language === 'en') { } else if (this.props.language === 'en_US') {
Blockly.setLocale(En); Blockly.setLocale(En);
} }
} }
componentDidUpdate(props){ componentDidUpdate(props){
if(props.language !== this.props.language){ if(props.language !== this.props.language){
if (this.props.language === 'de') { if (this.props.language === 'de_DE') {
Blockly.setLocale(De); Blockly.setLocale(De);
} else if (this.props.language === 'en') { } else if (this.props.language === 'en_US') {
Blockly.setLocale(En); Blockly.setLocale(En);
} }
} }

View File

@ -37,8 +37,8 @@ class LanguageSelector extends Component {
value={this.props.language} value={this.props.language}
onChange={this.handleChange} onChange={this.handleChange}
> >
<MenuItem value={'de'}>{Blockly.Msg.settings_language_de}</MenuItem> <MenuItem value={'de_DE'}>{Blockly.Msg.settings_language_de}</MenuItem>
<MenuItem value={'en'}>{Blockly.Msg.settings_language_en}</MenuItem> <MenuItem value={'en_US'}>{Blockly.Msg.settings_language_en}</MenuItem>
</Select> </Select>
</FormControl> </FormControl>
</div> </div>

View File

@ -1,11 +1,35 @@
import { VISIT, LANGUAGE, RENDERER, STATISTICS } from '../actions/types'; import { VISIT, LANGUAGE, RENDERER, STATISTICS } from '../actions/types';
const initialLanguage = () => {
if (window.localStorage.getItem('locale')) {
return window.localStorage.getItem('locale');
}
if (navigator.language === 'de-DE'){
return 'de_DE';
}
return 'en_US';
};
const initialRenderer = () => {
if (window.localStorage.getItem('renderer')) {
return window.localStorage.getItem('renderer');
}
return 'geras';
};
const initialStatistics = () => {
if (window.localStorage.getItem('statistics')) {
return JSON.parse(window.localStorage.getItem('statistics'));
}
return false;
};
const initialState = { const initialState = {
pageVisits: 0, // detect if previous URL was pageVisits: 0, // detect if previous URL was
language: 'de', language: initialLanguage(),
renderer: window.localStorage.getItem('renderer') || 'geras', renderer: initialRenderer(),
statistics: window.localStorage.getItem('statistics') === 'true' ? true : window.localStorage.getItem('statistics') === 'false' ? false : false statistics: initialStatistics()
}; };
export default function(state = initialState, action){ export default function(state = initialState, action){