From 63f20bc3979c61f71afcad1e9c7bbcdbe783a98e Mon Sep 17 00:00:00 2001 From: Ildar Kamalov Date: Thu, 22 Nov 2018 17:56:57 +0300 Subject: [PATCH] Request language from server --- client/src/actions/index.js | 30 ++++++++++++++++++++++++++++++ client/src/api/Api.js | 18 ++++++++++++++++++ client/src/components/App/index.js | 24 ++++++++++++++++++++++++ client/src/reducers/index.js | 7 +++++++ i18n.go | 3 ++- 5 files changed, 81 insertions(+), 1 deletion(-) diff --git a/client/src/actions/index.js b/client/src/actions/index.js index 4f55f23c..a4da8c9a 100644 --- a/client/src/actions/index.js +++ b/client/src/actions/index.js @@ -492,3 +492,33 @@ export const testUpstream = servers => async (dispatch) => { 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()); + } +}; diff --git a/client/src/api/Api.js b/client/src/api/Api.js index 0e9a1c74..f0d90941 100644 --- a/client/src/api/Api.js +++ b/client/src/api/Api.js @@ -284,4 +284,22 @@ export default class Api { const { path, method } = this.SAFESEARCH_DISABLE; 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); + } } diff --git a/client/src/components/App/index.js b/client/src/components/App/index.js index b4b040a9..98c37afa 100644 --- a/client/src/components/App/index.js +++ b/client/src/components/App/index.js @@ -17,6 +17,7 @@ import Footer from '../ui/Footer'; import Toasts from '../Toasts'; import Status from '../ui/Status'; import Update from '../ui/Update'; +import i18n from '../../i18n'; class App extends Component { componentDidMount() { @@ -24,10 +25,32 @@ class App extends Component { this.props.getVersion(); } + componentDidUpdate(prevProps) { + if (this.props.dashboard.language !== prevProps.dashboard.language) { + this.setLanguage(); + } + } + handleStatusChange = () => { 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() { const { dashboard } = this.props; const updateAvailable = @@ -78,6 +101,7 @@ App.propTypes = { isCoreRunning: PropTypes.bool, error: PropTypes.string, getVersion: PropTypes.func, + changeLanguage: PropTypes.func, }; export default App; diff --git a/client/src/reducers/index.js b/client/src/reducers/index.js index 2e32be98..9a54f249 100644 --- a/client/src/reducers/index.js +++ b/client/src/reducers/index.js @@ -49,6 +49,7 @@ const dashboard = handleActions({ querylog_enabled: queryLogEnabled, upstream_dns: upstreamDns, protection_enabled: protectionEnabled, + language, } = payload; const newState = { ...state, @@ -60,6 +61,7 @@ const dashboard = handleActions({ queryLogEnabled, upstreamDns: upstreamDns.join('\n'), protectionEnabled, + language, }; return newState; }, @@ -145,6 +147,11 @@ const dashboard = handleActions({ const { upstreamDns } = payload; return { ...state, upstreamDns }; }, + + [actions.getLanguageSuccess]: (state, { payload }) => { + const newState = { ...state, language: payload }; + return newState; + }, }, { processing: true, isCoreRunning: false, diff --git a/i18n.go b/i18n.go index 54c11018..7f0652e1 100644 --- a/i18n.go +++ b/i18n.go @@ -27,7 +27,8 @@ func isLanguageAllowed(language string) bool { func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) { 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 { errortext := fmt.Sprintf("Unable to write response json: %s", err) log.Println(errortext)