load translations before the application starts

This commit is contained in:
peterjgrainger 2019-10-17 16:20:06 +01:00 committed by Sean Yesmunt
parent ef1f6e9836
commit 1d053c5302
5 changed files with 47 additions and 27 deletions

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -8,9 +8,21 @@
<body>
<div id="app"></div>
<script>
const script = document.createElement('script');
script.setAttribute('src', 'ui.js');
document.body.appendChild(script);
function loadUi() {
const script = document.createElement('script');
script.setAttribute('src', 'ui.js');
document.body.appendChild(script);
}
require('electron').ipcRenderer.on('language-set', (event, messages, language, error) => {
window.i18n_messages = messages;
window.localStorage.setItem('language', language);
if(error) {
console.error('Can\'t set language to ' + language);
console.error(error);
}
loadUi();
});
</script>
</body>
</html>

View file

@ -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(); })

View file

@ -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 => {