parent
951745d105
commit
0d41e6d88d
8 changed files with 72 additions and 54 deletions
|
@ -37,6 +37,7 @@ import {
|
||||||
STATUS_DOWN,
|
STATUS_DOWN,
|
||||||
} from 'web/effects/use-degraded-performance';
|
} from 'web/effects/use-degraded-performance';
|
||||||
// @endif
|
// @endif
|
||||||
|
import LANGUAGE_MIGRATIONS from 'constants/language-migrations';
|
||||||
export const MAIN_WRAPPER_CLASS = 'main-wrapper';
|
export const MAIN_WRAPPER_CLASS = 'main-wrapper';
|
||||||
export const IS_MAC = navigator.userAgent.indexOf('Mac OS X') !== -1;
|
export const IS_MAC = navigator.userAgent.indexOf('Mac OS X') !== -1;
|
||||||
|
|
||||||
|
@ -144,6 +145,7 @@ function App(props: Props) {
|
||||||
const useCustomScrollbar = !IS_MAC;
|
const useCustomScrollbar = !IS_MAC;
|
||||||
const hasMyChannels = myChannelUrls && myChannelUrls.length > 0;
|
const hasMyChannels = myChannelUrls && myChannelUrls.length > 0;
|
||||||
const hasNoChannels = myChannelUrls && myChannelUrls.length === 0;
|
const hasNoChannels = myChannelUrls && myChannelUrls.length === 0;
|
||||||
|
const shouldMigrateLanguage = LANGUAGE_MIGRATIONS[language];
|
||||||
const hasActiveChannelClaim = activeChannelClaim !== undefined;
|
const hasActiveChannelClaim = activeChannelClaim !== undefined;
|
||||||
|
|
||||||
let uri;
|
let uri;
|
||||||
|
@ -257,6 +259,12 @@ function App(props: Props) {
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [language, languages]);
|
}, [language, languages]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (shouldMigrateLanguage) {
|
||||||
|
setLanguage(shouldMigrateLanguage);
|
||||||
|
}
|
||||||
|
}, [shouldMigrateLanguage, setLanguage]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Check that previousHasVerifiedEmail was not undefined instead of just not truthy
|
// Check that previousHasVerifiedEmail was not undefined instead of just not truthy
|
||||||
// This ensures we don't fire the emailVerified event on the initial user fetch
|
// This ensures we don't fire the emailVerified event on the initial user fetch
|
||||||
|
|
6
ui/constants/language-migrations.js
Normal file
6
ui/constants/language-migrations.js
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
const LANGUAGE_MIGRATIONS = {
|
||||||
|
'zh-CN': 'zh-Hans',
|
||||||
|
'zh-TW': 'zh-Hant',
|
||||||
|
};
|
||||||
|
|
||||||
|
export default LANGUAGE_MIGRATIONS;
|
39
ui/constants/supported_browser_languages.js
Normal file
39
ui/constants/supported_browser_languages.js
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
const SUPPORTED_BROWSER_LANGUAGES = {
|
||||||
|
en: 'en',
|
||||||
|
da: 'da',
|
||||||
|
'zh-CN': 'zh-Hans',
|
||||||
|
'zh-TW': 'zh-Hant',
|
||||||
|
'zh-Hans': 'zh-Hans',
|
||||||
|
'zh-Hant': 'zh-Hant',
|
||||||
|
hr: 'hr',
|
||||||
|
nl: 'nl',
|
||||||
|
no: 'no',
|
||||||
|
fi: 'fi',
|
||||||
|
fr: 'fr',
|
||||||
|
de: 'de',
|
||||||
|
gu: 'gu',
|
||||||
|
hi: 'hi',
|
||||||
|
id: 'id',
|
||||||
|
jv: 'jv',
|
||||||
|
it: 'it',
|
||||||
|
ms: 'ms',
|
||||||
|
ml: 'ml',
|
||||||
|
mr: 'mr',
|
||||||
|
pa: 'pa',
|
||||||
|
pl: 'pl',
|
||||||
|
pt: 'pt',
|
||||||
|
ro: 'ro',
|
||||||
|
ru: 'ru',
|
||||||
|
sr: 'sr',
|
||||||
|
sk: 'sk',
|
||||||
|
ur: 'ur',
|
||||||
|
ca: 'ca',
|
||||||
|
es: 'es',
|
||||||
|
sv: 'sv',
|
||||||
|
tr: 'tr',
|
||||||
|
cs: 'cs',
|
||||||
|
kn: 'kn',
|
||||||
|
uk: 'uk',
|
||||||
|
};
|
||||||
|
|
||||||
|
export default SUPPORTED_BROWSER_LANGUAGES;
|
|
@ -1,5 +1,5 @@
|
||||||
import LANGUAGES from './languages';
|
import LANGUAGES from './languages';
|
||||||
|
// supported_browser_languages
|
||||||
const SUPPORTED_LANGUAGES = {
|
const SUPPORTED_LANGUAGES = {
|
||||||
af: LANGUAGES.af[1],
|
af: LANGUAGES.af[1],
|
||||||
en: LANGUAGES.en[1],
|
en: LANGUAGES.en[1],
|
||||||
|
|
8
ui/constants/supported_sub_languages.js
Normal file
8
ui/constants/supported_sub_languages.js
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
// https://www.electronjs.org/docs/api/locales
|
||||||
|
|
||||||
|
export const SUB_LANG_CODE_LEN = 5;
|
||||||
|
|
||||||
|
export const SUPPORTED_SUB_LANGUAGE_CODES = [
|
||||||
|
'zh-CN',
|
||||||
|
'zh-TW',
|
||||||
|
];
|
13
ui/i18n.js
13
ui/i18n.js
|
@ -1,5 +1,3 @@
|
||||||
import { getDefaultLanguage, resolveLanguageAlias } from './util/default-languages';
|
|
||||||
|
|
||||||
// @if TARGET='app'
|
// @if TARGET='app'
|
||||||
let fs = require('fs');
|
let fs = require('fs');
|
||||||
// @endif
|
// @endif
|
||||||
|
@ -33,7 +31,7 @@ function saveMessageDesktop(message) {
|
||||||
knownMessages[message] = removeContextMetadata(message);
|
knownMessages[message] = removeContextMetadata(message);
|
||||||
knownMessages[END] = END;
|
knownMessages[END] = END;
|
||||||
|
|
||||||
fs.writeFile(messagesFilePath, JSON.stringify(knownMessages, null, 2) + '\n', 'utf-8', (err) => {
|
fs.writeFile(messagesFilePath, JSON.stringify(knownMessages, null, 2) + '\n', 'utf-8', err => {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
@ -85,10 +83,9 @@ export function __(message, tokens) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
const language = resolveLanguageAlias(
|
const language = localStorageAvailable
|
||||||
localStorageAvailable ? window.localStorage.getItem('language') || 'en' : getDefaultLanguage() || 'en'
|
? window.localStorage.getItem('language') || 'en'
|
||||||
);
|
: window.navigator.language.slice(0, 2) || 'en';
|
||||||
|
|
||||||
if (!isProduction) {
|
if (!isProduction) {
|
||||||
IS_WEB ? saveMessageWeb(message) : saveMessageDesktop(message);
|
IS_WEB ? saveMessageWeb(message) : saveMessageDesktop(message);
|
||||||
}
|
}
|
||||||
|
@ -100,7 +97,7 @@ export function __(message, tokens) {
|
||||||
return translatedMessage;
|
return translatedMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
return translatedMessage.replace(/%([^%]+)%/g, ($1, $2) => {
|
return translatedMessage.replace(/%([^%]+)%/g, function($1, $2) {
|
||||||
return tokens.hasOwnProperty($2) ? tokens[$2] : $2;
|
return tokens.hasOwnProperty($2) ? tokens[$2] : $2;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { SETTINGS, DAEMON_SETTINGS } from 'lbry-redux';
|
||||||
import { createSelector } from 'reselect';
|
import { createSelector } from 'reselect';
|
||||||
import homepages from 'homepages';
|
import homepages from 'homepages';
|
||||||
import { SIMPLE_SITE } from 'config';
|
import { SIMPLE_SITE } from 'config';
|
||||||
import { resolveLanguageAlias, getDefaultHomepageKey, getDefaultLanguage } from 'util/default-languages';
|
import { getDefaultHomepageKey, getDefaultLanguage } from 'util/default-languages';
|
||||||
|
|
||||||
const selectState = (state) => state.settings || {};
|
const selectState = (state) => state.settings || {};
|
||||||
|
|
||||||
|
@ -62,8 +62,7 @@ export const selectHomepageCode = createSelector(makeSelectClientSetting(SETTING
|
||||||
});
|
});
|
||||||
|
|
||||||
export const selectLanguage = createSelector(makeSelectClientSetting(SETTINGS.LANGUAGE), (setting) => {
|
export const selectLanguage = createSelector(makeSelectClientSetting(SETTINGS.LANGUAGE), (setting) => {
|
||||||
const lang = resolveLanguageAlias(setting) || getDefaultLanguage();
|
return setting || getDefaultLanguage();
|
||||||
return lang;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export const selectHomepageData = createSelector(
|
export const selectHomepageData = createSelector(
|
||||||
|
|
|
@ -1,49 +1,10 @@
|
||||||
import homepages from 'homepages';
|
import homepages from 'homepages';
|
||||||
import SUPPORTED_LANGUAGES from 'constants/supported_languages';
|
import SUPPORTED_BROWSER_LANGUAGES from 'constants/supported_browser_languages';
|
||||||
const DEFAULT_LANG = 'en';
|
const DEFAULT_LANG = 'en';
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if the given language is an alias of a language that we support, and
|
|
||||||
* returns the version that the rest of the application (lbry-sdk, Transifex)
|
|
||||||
* expects. If the given language is not an expected alias, the original value
|
|
||||||
* is returned.
|
|
||||||
*
|
|
||||||
* @param lang
|
|
||||||
* @returns {*}
|
|
||||||
*/
|
|
||||||
export function resolveLanguageAlias(lang) {
|
|
||||||
const KNOWN_LANG_ALIASES = {
|
|
||||||
'zh-CN': 'zh-Hans',
|
|
||||||
'zh-TW': 'zh-Hant',
|
|
||||||
};
|
|
||||||
return KNOWN_LANG_ALIASES[lang] ? KNOWN_LANG_ALIASES[lang] : lang;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the native language of the system, filtered/resolved
|
|
||||||
* to the language code format that the application expects.
|
|
||||||
*
|
|
||||||
* @returns {string|*}
|
|
||||||
*/
|
|
||||||
export const getDefaultLanguage = () => {
|
export const getDefaultLanguage = () => {
|
||||||
const browserLanguage = resolveLanguageAlias(window.navigator.language);
|
const browserLanguage = window.navigator.language;
|
||||||
|
return SUPPORTED_BROWSER_LANGUAGES[browserLanguage] || DEFAULT_LANG;
|
||||||
if (SUPPORTED_LANGUAGES[browserLanguage]) {
|
|
||||||
return browserLanguage;
|
|
||||||
} else {
|
|
||||||
if (browserLanguage.includes('-')) {
|
|
||||||
// Perhaps it is a sub-lang that we are currently not supporting.
|
|
||||||
// See if we support the main one.
|
|
||||||
const mainLang = browserLanguage.substring(0, browserLanguage.indexOf('-'));
|
|
||||||
if (SUPPORTED_LANGUAGES[mainLang]) {
|
|
||||||
return mainLang;
|
|
||||||
} else {
|
|
||||||
return DEFAULT_LANG;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return DEFAULT_LANG;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// If homepages has a key "zh-Hant" return that, otherwise return "zh", otherwise "en"
|
// If homepages has a key "zh-Hant" return that, otherwise return "zh", otherwise "en"
|
||||||
|
|
Loading…
Reference in a new issue