From 0d41e6d88d86d3b85837fab9788b23a69174ea20 Mon Sep 17 00:00:00 2001 From: seanyesmunt Date: Tue, 6 Apr 2021 15:15:08 -0400 Subject: [PATCH] Revert "Simplify language lists" This reverts commit 27d43b2b419b5940e3b217acf871aeaf858592b1. --- ui/component/app/view.jsx | 8 ++++ ui/constants/language-migrations.js | 6 +++ ui/constants/supported_browser_languages.js | 39 ++++++++++++++++++ ui/constants/supported_languages.js | 2 +- ui/constants/supported_sub_languages.js | 8 ++++ ui/i18n.js | 13 +++--- ui/redux/selectors/settings.js | 5 +-- ui/util/default-languages.js | 45 ++------------------- 8 files changed, 72 insertions(+), 54 deletions(-) create mode 100644 ui/constants/language-migrations.js create mode 100644 ui/constants/supported_browser_languages.js create mode 100644 ui/constants/supported_sub_languages.js diff --git a/ui/component/app/view.jsx b/ui/component/app/view.jsx index 7a56887a8..d6150edf2 100644 --- a/ui/component/app/view.jsx +++ b/ui/component/app/view.jsx @@ -37,6 +37,7 @@ import { STATUS_DOWN, } from 'web/effects/use-degraded-performance'; // @endif +import LANGUAGE_MIGRATIONS from 'constants/language-migrations'; export const MAIN_WRAPPER_CLASS = 'main-wrapper'; export const IS_MAC = navigator.userAgent.indexOf('Mac OS X') !== -1; @@ -144,6 +145,7 @@ function App(props: Props) { const useCustomScrollbar = !IS_MAC; const hasMyChannels = myChannelUrls && myChannelUrls.length > 0; const hasNoChannels = myChannelUrls && myChannelUrls.length === 0; + const shouldMigrateLanguage = LANGUAGE_MIGRATIONS[language]; const hasActiveChannelClaim = activeChannelClaim !== undefined; let uri; @@ -257,6 +259,12 @@ function App(props: Props) { // eslint-disable-next-line react-hooks/exhaustive-deps }, [language, languages]); + useEffect(() => { + if (shouldMigrateLanguage) { + setLanguage(shouldMigrateLanguage); + } + }, [shouldMigrateLanguage, setLanguage]); + useEffect(() => { // 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 diff --git a/ui/constants/language-migrations.js b/ui/constants/language-migrations.js new file mode 100644 index 000000000..d400cbcf3 --- /dev/null +++ b/ui/constants/language-migrations.js @@ -0,0 +1,6 @@ +const LANGUAGE_MIGRATIONS = { + 'zh-CN': 'zh-Hans', + 'zh-TW': 'zh-Hant', +}; + +export default LANGUAGE_MIGRATIONS; diff --git a/ui/constants/supported_browser_languages.js b/ui/constants/supported_browser_languages.js new file mode 100644 index 000000000..c672e6100 --- /dev/null +++ b/ui/constants/supported_browser_languages.js @@ -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; diff --git a/ui/constants/supported_languages.js b/ui/constants/supported_languages.js index 31a032f57..f3ca21553 100644 --- a/ui/constants/supported_languages.js +++ b/ui/constants/supported_languages.js @@ -1,5 +1,5 @@ import LANGUAGES from './languages'; - +// supported_browser_languages const SUPPORTED_LANGUAGES = { af: LANGUAGES.af[1], en: LANGUAGES.en[1], diff --git a/ui/constants/supported_sub_languages.js b/ui/constants/supported_sub_languages.js new file mode 100644 index 000000000..98d5f59ee --- /dev/null +++ b/ui/constants/supported_sub_languages.js @@ -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', +]; diff --git a/ui/i18n.js b/ui/i18n.js index 31cfbf60f..6b66bfe62 100644 --- a/ui/i18n.js +++ b/ui/i18n.js @@ -1,5 +1,3 @@ -import { getDefaultLanguage, resolveLanguageAlias } from './util/default-languages'; - // @if TARGET='app' let fs = require('fs'); // @endif @@ -33,7 +31,7 @@ function saveMessageDesktop(message) { knownMessages[message] = removeContextMetadata(message); 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) { throw err; } @@ -85,10 +83,9 @@ export function __(message, tokens) { return ''; } - const language = resolveLanguageAlias( - localStorageAvailable ? window.localStorage.getItem('language') || 'en' : getDefaultLanguage() || 'en' - ); - + const language = localStorageAvailable + ? window.localStorage.getItem('language') || 'en' + : window.navigator.language.slice(0, 2) || 'en'; if (!isProduction) { IS_WEB ? saveMessageWeb(message) : saveMessageDesktop(message); } @@ -100,7 +97,7 @@ export function __(message, tokens) { return translatedMessage; } - return translatedMessage.replace(/%([^%]+)%/g, ($1, $2) => { + return translatedMessage.replace(/%([^%]+)%/g, function($1, $2) { return tokens.hasOwnProperty($2) ? tokens[$2] : $2; }); } diff --git a/ui/redux/selectors/settings.js b/ui/redux/selectors/settings.js index cd7a4a314..85b700b8e 100644 --- a/ui/redux/selectors/settings.js +++ b/ui/redux/selectors/settings.js @@ -2,7 +2,7 @@ import { SETTINGS, DAEMON_SETTINGS } from 'lbry-redux'; import { createSelector } from 'reselect'; import homepages from 'homepages'; 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 || {}; @@ -62,8 +62,7 @@ export const selectHomepageCode = createSelector(makeSelectClientSetting(SETTING }); export const selectLanguage = createSelector(makeSelectClientSetting(SETTINGS.LANGUAGE), (setting) => { - const lang = resolveLanguageAlias(setting) || getDefaultLanguage(); - return lang; + return setting || getDefaultLanguage(); }); export const selectHomepageData = createSelector( diff --git a/ui/util/default-languages.js b/ui/util/default-languages.js index 3a6838dc3..fd5cd4b6b 100644 --- a/ui/util/default-languages.js +++ b/ui/util/default-languages.js @@ -1,49 +1,10 @@ import homepages from 'homepages'; -import SUPPORTED_LANGUAGES from 'constants/supported_languages'; +import SUPPORTED_BROWSER_LANGUAGES from 'constants/supported_browser_languages'; 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 = () => { - const browserLanguage = resolveLanguageAlias(window.navigator.language); - - 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; - } - } + const browserLanguage = window.navigator.language; + return SUPPORTED_BROWSER_LANGUAGES[browserLanguage] || DEFAULT_LANG; }; // If homepages has a key "zh-Hant" return that, otherwise return "zh", otherwise "en"