Request language from server

This commit is contained in:
Ildar Kamalov 2018-11-22 17:56:57 +03:00
parent 83544ab0f6
commit 63f20bc397
5 changed files with 81 additions and 1 deletions

View File

@ -492,3 +492,33 @@ export const testUpstream = servers => async (dispatch) => {
dispatch(testUpstreamFailure()); dispatch(testUpstreamFailure());
} }
}; };
export const changeLanguageRequest = createAction('CHANGE_LANGUAGE_REQUEST');
export const changeLanguageFailure = createAction('CHANGE_LANGUAGE_FAILURE');
export const changeLanguageSuccess = createAction('CHANGE_LANGUAGE_SUCCESS');
export const changeLanguage = lang => async (dispatch) => {
dispatch(changeLanguageRequest());
try {
await apiClient.changeLanguage(lang);
dispatch(changeLanguageSuccess());
} catch (error) {
dispatch(addErrorToast({ error }));
dispatch(changeLanguageFailure());
}
};
export const getLanguageRequest = createAction('GET_LANGUAGE_REQUEST');
export const getLanguageFailure = createAction('GET_LANGUAGE_FAILURE');
export const getLanguageSuccess = createAction('GET_LANGUAGE_SUCCESS');
export const getLanguage = () => async (dispatch) => {
dispatch(getLanguageRequest());
try {
const language = await apiClient.getCurrentLanguage();
dispatch(getLanguageSuccess(language));
} catch (error) {
dispatch(addErrorToast({ error }));
dispatch(getLanguageFailure());
}
};

View File

@ -284,4 +284,22 @@ export default class Api {
const { path, method } = this.SAFESEARCH_DISABLE; const { path, method } = this.SAFESEARCH_DISABLE;
return this.makeRequest(path, method); return this.makeRequest(path, method);
} }
// Language
CURRENT_LANGUAGE = { path: 'i18n/current_language', method: 'GET' };
CHANGE_LANGUAGE = { path: 'i18n/change_language', method: 'POST' };
getCurrentLanguage() {
const { path, method } = this.CURRENT_LANGUAGE;
return this.makeRequest(path, method);
}
changeLanguage(lang) {
const { path, method } = this.CHANGE_LANGUAGE;
const parameters = {
data: lang,
headers: { 'Content-Type': 'text/plain' },
};
return this.makeRequest(path, method, parameters);
}
} }

View File

@ -17,6 +17,7 @@ import Footer from '../ui/Footer';
import Toasts from '../Toasts'; import Toasts from '../Toasts';
import Status from '../ui/Status'; import Status from '../ui/Status';
import Update from '../ui/Update'; import Update from '../ui/Update';
import i18n from '../../i18n';
class App extends Component { class App extends Component {
componentDidMount() { componentDidMount() {
@ -24,10 +25,32 @@ class App extends Component {
this.props.getVersion(); this.props.getVersion();
} }
componentDidUpdate(prevProps) {
if (this.props.dashboard.language !== prevProps.dashboard.language) {
this.setLanguage();
}
}
handleStatusChange = () => { handleStatusChange = () => {
this.props.enableDns(); this.props.enableDns();
}; };
setLanguage = () => {
const { processing, language } = this.props.dashboard;
if (!processing) {
if (!language) {
this.props.changeLanguage(i18n.language);
} else {
i18n.changeLanguage(language);
}
}
i18n.on('languageChanged', (lang) => {
this.props.changeLanguage(lang);
});
}
render() { render() {
const { dashboard } = this.props; const { dashboard } = this.props;
const updateAvailable = const updateAvailable =
@ -78,6 +101,7 @@ App.propTypes = {
isCoreRunning: PropTypes.bool, isCoreRunning: PropTypes.bool,
error: PropTypes.string, error: PropTypes.string,
getVersion: PropTypes.func, getVersion: PropTypes.func,
changeLanguage: PropTypes.func,
}; };
export default App; export default App;

View File

@ -49,6 +49,7 @@ const dashboard = handleActions({
querylog_enabled: queryLogEnabled, querylog_enabled: queryLogEnabled,
upstream_dns: upstreamDns, upstream_dns: upstreamDns,
protection_enabled: protectionEnabled, protection_enabled: protectionEnabled,
language,
} = payload; } = payload;
const newState = { const newState = {
...state, ...state,
@ -60,6 +61,7 @@ const dashboard = handleActions({
queryLogEnabled, queryLogEnabled,
upstreamDns: upstreamDns.join('\n'), upstreamDns: upstreamDns.join('\n'),
protectionEnabled, protectionEnabled,
language,
}; };
return newState; return newState;
}, },
@ -145,6 +147,11 @@ const dashboard = handleActions({
const { upstreamDns } = payload; const { upstreamDns } = payload;
return { ...state, upstreamDns }; return { ...state, upstreamDns };
}, },
[actions.getLanguageSuccess]: (state, { payload }) => {
const newState = { ...state, language: payload };
return newState;
},
}, { }, {
processing: true, processing: true,
isCoreRunning: false, isCoreRunning: false,

View File

@ -27,7 +27,8 @@ func isLanguageAllowed(language string) bool {
func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) { func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
_, err := fmt.Fprintf(w, config.Language) log.Printf("config.Language is %s", config.Language)
_, err := fmt.Fprintf(w, "%s\n", config.Language)
if err != nil { if err != nil {
errortext := fmt.Sprintf("Unable to write response json: %s", err) errortext := fmt.Sprintf("Unable to write response json: %s", err)
log.Println(errortext) log.Println(errortext)