diff --git a/electron/createWindow.js b/electron/createWindow.js index aa5402e09..16e98327e 100644 --- a/electron/createWindow.js +++ b/electron/createWindow.js @@ -5,7 +5,7 @@ import windowStateKeeper from 'electron-window-state'; import setupBarMenu from './menu/setupBarMenu'; import * as PAGES from '../../ui/constants/pages'; -import SUPPORTED_LANGUAGES from '../../ui/constants/supported_languages'; +import setLanguage from './setLanguage'; export default appState => { // Get primary display dimensions from Electron. @@ -147,18 +147,3 @@ export default appState => { return window; }; - -async function setLanguage(window) { - const storedLanguage = await window.webContents.executeJavaScript("localStorage.getItem('language')"); - const lang = storedLanguage || app.getLocale().slice(0, 2) || 'en'; - - const supportedNonEnglish = Object.keys(SUPPORTED_LANGUAGES).filter(language => language !== 'en'); - if (supportedNonEnglish.includes(lang)) { - const response = await fetch('https://lbry.com/i18n/get/lbry-desktop/app-strings/' + lang + '.json'); - const json = await response.json(); - const messages = {}; - messages[lang] = json; - // Send message to render layer to update language. - window.webContents.send('language-update', messages, lang); - } -} diff --git a/src/platforms/electron/setLanguage.js b/src/platforms/electron/setLanguage.js new file mode 100644 index 000000000..a8db87a0d --- /dev/null +++ b/src/platforms/electron/setLanguage.js @@ -0,0 +1,22 @@ +import SUPPORTED_LANGUAGES from '../../ui/constants/supported_languages'; + +export default async function setLanguage(window) { + const storedLanguage = await window.webContents.executeJavaScript("localStorage.getItem('language')"); + const lang = storedLanguage || app.getLocale().slice(0, 2) || 'en'; + const supportedNonEnglish = Object.keys(SUPPORTED_LANGUAGES).filter(language => language !== 'en'); + + if (supportedNonEnglish.includes(lang)) { + fetch('https://lbry.com/i18n/get/lbry-desktop/app-strings/' + lang + '.json') + .then(response => response.json()) + .then(json => { + const messages = {}; + messages[lang] = json; + window.webContents.send('language-set', messages, lang); + }) + .catch(error => { + window.webContents.send('language-set', {}, lang, error); + }); + } else { + window.webContents.send('language-set', {}, lang); + } +} diff --git a/static/index-electron.html b/static/index-electron.html index c30ddb9e6..6af12eb1d 100644 --- a/static/index-electron.html +++ b/static/index-electron.html @@ -8,9 +8,21 @@
diff --git a/static/index-web.html b/static/index-web.html index e36691261..3a132c168 100644 --- a/static/index-web.html +++ b/static/index-web.html @@ -24,13 +24,20 @@ document.body.appendChild(script); } + function supportedNonEnglish(language) { + return language === 'pl' || + language === 'id' || + language === 'de' + } + let lang; try { - lang = window.localStorage.getItem('language') || 'en'; + browserLocale = window.navigator.language.slice(0,2); + lang = window.localStorage.getItem('language') || browserLocale || 'en'; } catch { lang = 'en'; } - if (lang && lang != 'en') { + if (lang && supportedNonEnglish(lang)) { fetch('https://lbry.com/i18n/get/lbry-desktop/app-strings/' + lang + '.json') .then(r => r.json()) .then(j => { window.i18n_messages[lang] = j; loadUi(); }) diff --git a/ui/index.jsx b/ui/index.jsx index 3bcb11581..b874c8000 100644 --- a/ui/index.jsx +++ b/ui/index.jsx @@ -31,7 +31,6 @@ import { PersistGate } from 'redux-persist/integration/react'; import analytics from 'analytics'; import { getAuthToken, setAuthToken } from 'util/saved-passwords'; import { X_LBRY_AUTH_TOKEN } from 'constants/token'; -import * as SETTINGS from 'constants/settings'; // Import our app styles // If a style is not necessary for the initial page load, it should be removed from `all.scss` @@ -194,11 +193,6 @@ ipcRenderer.on('devtools-is-opened', () => { doLogWarningConsoleMessage(); }); -ipcRenderer.on('language-update', (event, messages, language) => { - window.i18n_messages = messages; - window.localStorage.setItem(SETTINGS.LANGUAGE, language); -}); - // Force exit mode for html5 fullscreen api // See: https://github.com/electron/electron/issues/18188 remote.getCurrentWindow().on('leave-full-screen', event => {