Remove the need to call 'app.getLocale()'

The removal is primarily to ease the upcoming language refactoring, but it was also never hit anyway, because if the `storedLanguage` is indeed null or undefined (like in the case of a fresh install), the `then` block never runs.

Fortunately, we do still get the effect of "falling back to OS's language", because `selectLanguage` will call `getDefaultLanguage` if `language` is null. While the `language` localStorage remains null, the rest of the code still gets the native language via `selectLanguage`.

Aside: `getDefaultLanguage` uses `navigator.language`. It does seem to grab my OS's locale correctly.
This commit is contained in:
infinite-persistence 2021-03-16 16:30:19 +08:00
parent 7743f08a6a
commit 39b662265c
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0

View file

@ -3,26 +3,11 @@ import { app, BrowserWindow, dialog, shell, screen, nativeImage } from 'electron
import isDev from 'electron-is-dev'; import isDev from 'electron-is-dev';
import windowStateKeeper from 'electron-window-state'; import windowStateKeeper from 'electron-window-state';
import SUPPORTED_LANGUAGES from 'constants/supported_languages'; import SUPPORTED_LANGUAGES from 'constants/supported_languages';
import { SUPPORTED_SUB_LANGUAGE_CODES, SUB_LANG_CODE_LEN } from 'constants/supported_sub_languages';
import SUPPORTED_BROWSER_LANGUAGES from 'constants/supported_browser_languages';
import { TO_TRAY_WHEN_CLOSED } from 'constants/settings'; import { TO_TRAY_WHEN_CLOSED } from 'constants/settings';
import setupBarMenu from './menu/setupBarMenu'; import setupBarMenu from './menu/setupBarMenu';
import * as PAGES from 'constants/pages'; import * as PAGES from 'constants/pages';
function GetAppLangCode() {
// https://www.electronjs.org/docs/api/locales
// 1. Gets the user locale.
// 2. Converts unsupported sub-languages to its primary (e.g. "en-GB" -> "en").
// Note that the primary itself may or may not be a supported language
// (up to clients to verify against SUPPORTED_LANGUAGES).
const langCode = app.getLocale();
if (langCode.length === SUB_LANG_CODE_LEN && !SUPPORTED_SUB_LANGUAGE_CODES.includes(langCode)) {
return SUPPORTED_BROWSER_LANGUAGES[langCode.slice(0, 2)];
}
return SUPPORTED_BROWSER_LANGUAGES[langCode];
}
export default appState => { export default appState => {
// Get primary display dimensions from Electron. // Get primary display dimensions from Electron.
const { width, height } = screen.getPrimaryDisplay().workAreaSize; const { width, height } = screen.getPrimaryDisplay().workAreaSize;
@ -171,11 +156,9 @@ export default appState => {
window.webContents.session.setUserAgent(`LBRY/${app.getVersion()}`); window.webContents.session.setUserAgent(`LBRY/${app.getVersion()}`);
// restore the user's previous language - we have to do this from here because only electron process can access app.getLocale() // restore the user's previous language - we have to do this from here because only electron process can access app.getLocale()
// Update: we don't really need to call app.getLocale (didn't work anyway), so we could move this if we want.
window.webContents.executeJavaScript("localStorage.getItem('language')").then(storedLanguage => { window.webContents.executeJavaScript("localStorage.getItem('language')").then(storedLanguage => {
const language = const language = storedLanguage;
storedLanguage && storedLanguage !== 'undefined' && storedLanguage !== 'null'
? storedLanguage
: GetAppLangCode();
if (language !== 'en' && SUPPORTED_LANGUAGES[language]) { if (language !== 'en' && SUPPORTED_LANGUAGES[language]) {
window.webContents.send('language-set', language); window.webContents.send('language-set', language);
} }