From 1f7b247068cc3881c5a59471924f1dce2ea0011f Mon Sep 17 00:00:00 2001 From: Gina Contrino Date: Mon, 23 Oct 2017 18:08:58 +0200 Subject: [PATCH 1/3] Remember language in browser --- src/utils/ipcLocale.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/utils/ipcLocale.js b/src/utils/ipcLocale.js index 8e3cfa9da..5682cf0e9 100644 --- a/src/utils/ipcLocale.js +++ b/src/utils/ipcLocale.js @@ -14,6 +14,14 @@ export default { ipc.send('set-locale', locale); } }); + } else { + const language = i18n.language || window.localStorage.getItem('lang'); + if (language) i18n.changeLanguage(language); + else i18n.changeLanguage('en'); + + i18n.on('languageChanged', (locale) => { + window.localStorage.setItem('lang', locale); + }); } }, }; From 1e9e8006780a517402cabc5c34e0ae3cdd02c037 Mon Sep 17 00:00:00 2001 From: Gina Contrino Date: Tue, 24 Oct 2017 10:01:58 +0200 Subject: [PATCH 2/3] Set language on refresh for electron --- app/src/main.js | 18 +++++++++++++----- src/utils/ipcLocale.js | 11 +++++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/src/main.js b/app/src/main.js index c21769f5e..30afc92a2 100644 --- a/app/src/main.js +++ b/app/src/main.js @@ -37,11 +37,15 @@ const sendDetectedLang = (locale) => { }; // read config data from JSON file -storage.get('config', (error, data) => { - if (error) throw error; - lang = data.lang; - sendDetectedLang(lang); -}); +const getConfig = () => { + storage.get('config', (error, data) => { + if (error) throw error; + lang = data.lang; + sendDetectedLang(lang); + }); +}; + +getConfig(); function createWindow() { // set language of the react app @@ -182,3 +186,7 @@ ipcMain.on('set-locale', (event, locale) => { event.returnValue = 'Rebuilt electron menu.'; } }); + +ipcMain.on('request-locale', () => { + getConfig(); +}); diff --git a/src/utils/ipcLocale.js b/src/utils/ipcLocale.js index 5682cf0e9..b3cbacade 100644 --- a/src/utils/ipcLocale.js +++ b/src/utils/ipcLocale.js @@ -4,6 +4,10 @@ export default { let localeInit = false; if (ipc) { + if (!i18n.language) { + ipc.send('request-locale'); + } + ipc.on('detectedLocale', (action, locale) => { i18n.changeLanguage(locale); localeInit = true; @@ -16,8 +20,11 @@ export default { }); } else { const language = i18n.language || window.localStorage.getItem('lang'); - if (language) i18n.changeLanguage(language); - else i18n.changeLanguage('en'); + if (language) { + i18n.changeLanguage(language); + } else { + i18n.changeLanguage('en'); + } i18n.on('languageChanged', (locale) => { window.localStorage.setItem('lang', locale); From 6848880bf7af62485dd210f7c3c294862b5a52ff Mon Sep 17 00:00:00 2001 From: Gina Contrino Date: Tue, 24 Oct 2017 10:33:34 +0200 Subject: [PATCH 3/3] Update tests --- src/utils/ipcLocale.test.js | 38 +++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/utils/ipcLocale.test.js b/src/utils/ipcLocale.test.js index 85c6d5894..f643de536 100644 --- a/src/utils/ipcLocale.test.js +++ b/src/utils/ipcLocale.test.js @@ -16,13 +16,40 @@ describe('ipcLocale', () => { describe('init', () => { beforeEach(() => { delete window.ipc; + i18n.language = ''; }); - it('calling init when ipc is not on window should do nothing', () => { - ipcLocale.init(); + + it('calling init when ipc is not on window does not call ipc', () => { + ipcLocale.init(i18n); expect(ipc.on).to.not.have.been.calledWith(); expect(ipc.send).to.not.have.been.calledWith(); }); + it('calling init when ipc is not on window saves locale in browser when there is no locale in i18n', () => { + window.localStorage.getItem = () => 'en'; + + ipcLocale.init(i18n); + expect(ipc.on).to.not.have.been.calledWith(); + expect(ipc.send).to.not.have.been.calledWith(); + + expect(i18n.changeLanguage).to.have.been.calledWith('en'); + }); + + it('calling init when ipc is not on window saves locale in browser when there is no locale in localStorage', () => { + window.localStorage.getItem = () => ''; + i18n.language = 'de'; + + ipcLocale.init(i18n); + expect(i18n.changeLanguage).to.have.been.calledWith('de'); + }); + + it('calling init when ipc is not on window saves locale in browser when there is no locale saved at all', () => { + window.localStorage.getItem = () => ''; + + ipcLocale.init(i18n); + expect(i18n.changeLanguage).to.have.been.calledWith('en'); + }); + it('should be a function', () => { expect(typeof ipcLocale.init).to.be.equal('function'); }); @@ -33,5 +60,12 @@ describe('ipcLocale', () => { expect(ipc.on).to.have.been.calledWith(); expect(i18n.on).to.have.been.calledWith(); }); + + it('calling init when ipc is available and there is no locale in i18n will make a request for locale ', () => { + window.ipc = ipc; + + ipcLocale.init(i18n); + expect(ipc.send).to.have.been.calledWith('request-locale'); + }); }); });