Used OS language to default localization

This commit is contained in:
peterjgrainger 2019-10-11 15:02:24 +01:00 committed by Sean Yesmunt
parent 2a30fee4b5
commit 4a0855e074
3 changed files with 26 additions and 23 deletions

View file

@ -88,6 +88,8 @@ export default appState => {
window.loadURL(rendererURL + deepLinkingURI); window.loadURL(rendererURL + deepLinkingURI);
setupBarMenu(); setupBarMenu();
setLanguage(window);
window.on('close', event => { window.on('close', event => {
if (!appState.isQuitting && !appState.autoUpdateAccepted) { if (!appState.isQuitting && !appState.autoUpdateAccepted) {
event.preventDefault(); event.preventDefault();
@ -144,3 +146,16 @@ export default appState => {
return window; return window;
}; };
async function setLanguage(window) {
const storedLanguage = await window.webContents.executeJavaScript("localStorage.getItem('language')");
const lang = storedLanguage || app.getLocale().slice(0, 2) || 'en';
if (['pl', 'id', 'de'].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);
}
}

View file

@ -8,28 +8,9 @@
<body> <body>
<div id="app"></div> <div id="app"></div>
<script> <script>
window.i18n_messages = {};
function loadUi () {
const script = document.createElement('script'); const script = document.createElement('script');
script.setAttribute('src', 'ui.js'); script.setAttribute('src', 'ui.js');
document.body.appendChild(script); document.body.appendChild(script);
}
let lang;
try {
lang = window.localStorage.getItem('language') || 'en';
} catch {
lang = 'en';
}
if (lang && lang != 'en') {
fetch('https://lbry.com/i18n/get/lbry-desktop/app-strings/' + lang + '.json')
.then(r => r.json())
.then(j => { window.i18n_messages[lang] = j; loadUi(); })
.catch(loadUi);
} else {
loadUi();
}
</script> </script>
</body> </body>
</html> </html>

View file

@ -13,7 +13,7 @@ import ReactDOM from 'react-dom';
import { Provider } from 'react-redux'; import { Provider } from 'react-redux';
import { doConditionalAuthNavigate, doDaemonReady, doAutoUpdate, doOpenModal, doHideModal } from 'redux/actions/app'; import { doConditionalAuthNavigate, doDaemonReady, doAutoUpdate, doOpenModal, doHideModal } from 'redux/actions/app';
import { Lbry, doToast, isURIValid, setSearchApi } from 'lbry-redux'; import { Lbry, doToast, isURIValid, setSearchApi } from 'lbry-redux';
import { doUpdateIsNightAsync } from 'redux/actions/settings'; import { doUpdateIsNightAsync, doSetClientSetting } from 'redux/actions/settings';
import { import {
doAuthenticate, doAuthenticate,
Lbryio, Lbryio,
@ -31,6 +31,7 @@ import { PersistGate } from 'redux-persist/integration/react';
import analytics from 'analytics'; import analytics from 'analytics';
import { getAuthToken, setAuthToken } from 'util/saved-passwords'; import { getAuthToken, setAuthToken } from 'util/saved-passwords';
import { X_LBRY_AUTH_TOKEN } from 'constants/token'; import { X_LBRY_AUTH_TOKEN } from 'constants/token';
import * as SETTINGS from 'constants/settings';
// Import our app styles // Import our app styles
// If a style is not necessary for the initial page load, it should be removed from `all.scss` // If a style is not necessary for the initial page load, it should be removed from `all.scss`
@ -193,6 +194,12 @@ ipcRenderer.on('devtools-is-opened', () => {
doLogWarningConsoleMessage(); doLogWarningConsoleMessage();
}); });
ipcRenderer.on('language-update', (event, messages, language) => {
window.i18n_messages = messages;
window.localStorage.setItem(SETTINGS.LANGUAGE, language);
doSetClientSetting(SETTINGS.LANGUAGE, language);
});
// Force exit mode for html5 fullscreen api // Force exit mode for html5 fullscreen api
// See: https://github.com/electron/electron/issues/18188 // See: https://github.com/electron/electron/issues/18188
remote.getCurrentWindow().on('leave-full-screen', event => { remote.getCurrentWindow().on('leave-full-screen', event => {