From 6a3e66bc7e857152d8f9f8338007f5ac8c9b67c5 Mon Sep 17 00:00:00 2001 From: Shevek Date: Mon, 8 Nov 2021 21:21:41 -0500 Subject: [PATCH] properly fallback on locale --- app/soapbox/actions/settings.js | 9 ++++++++- app/soapbox/actions/streaming.js | 9 +-------- app/soapbox/containers/soapbox.js | 8 +++----- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/app/soapbox/actions/settings.js b/app/soapbox/actions/settings.js index 55ca6b0b5..9d6956070 100644 --- a/app/soapbox/actions/settings.js +++ b/app/soapbox/actions/settings.js @@ -5,6 +5,7 @@ import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrde import { isLoggedIn } from 'soapbox/utils/auth'; import uuid from '../uuid'; import { createSelector } from 'reselect'; +import messages from 'soapbox/locales/messages'; export const SETTING_CHANGE = 'SETTING_CHANGE'; export const SETTING_SAVE = 'SETTING_SAVE'; @@ -27,7 +28,7 @@ export const defaultSettings = ImmutableMap({ defaultPrivacy: 'public', defaultContentType: 'text/plain', themeMode: 'light', - locale: navigator.language.split(/[-_]/)[0] || 'en', + locale: navigator.language || 'en', showExplanationBox: true, explanationBox: true, otpEnabled: false, @@ -174,6 +175,12 @@ export function changeSetting(path, value) { }; } +export function getLocale(state) { + const locale = getSettings(state).get('locale').replace('_', '-'); + const locale2 = locale.split('-')[0]; + return Object.keys(messages).includes(locale) ? locale : Object.keys(messages).includes(locale2) ? locale2 : 'en'; +} + const debouncedSave = debounce((dispatch, getState) => { if (!isLoggedIn(getState)) return; diff --git a/app/soapbox/actions/streaming.js b/app/soapbox/actions/streaming.js index 1b8de7b97..d0305f569 100644 --- a/app/soapbox/actions/streaming.js +++ b/app/soapbox/actions/streaming.js @@ -9,19 +9,12 @@ import { import { updateNotificationsQueue, expandNotifications } from './notifications'; import { updateConversations } from './conversations'; import { fetchFilters } from './filters'; -import { getSettings } from 'soapbox/actions/settings'; +import { getSettings, getLocale } from 'soapbox/actions/settings'; import messages from 'soapbox/locales/messages'; export const STREAMING_CHAT_UPDATE = 'STREAMING_CHAT_UPDATE'; export const STREAMING_FOLLOW_RELATIONSHIPS_UPDATE = 'STREAMING_FOLLOW_RELATIONSHIPS_UPDATE'; -const validLocale = locale => Object.keys(messages).includes(locale); - -const getLocale = state => { - const locale = getSettings(state).get('locale'); - return validLocale(locale) ? locale : 'en'; -}; - function updateFollowRelationships(relationships) { return (dispatch, getState) => { const me = getState().get('me'); diff --git a/app/soapbox/containers/soapbox.js b/app/soapbox/containers/soapbox.js index c011d0f0d..07fd31dd7 100644 --- a/app/soapbox/containers/soapbox.js +++ b/app/soapbox/containers/soapbox.js @@ -20,15 +20,13 @@ import { loadInstance } from 'soapbox/actions/instance'; import { fetchSoapboxConfig } from 'soapbox/actions/soapbox'; import { fetchMe } from 'soapbox/actions/me'; import PublicLayout from 'soapbox/features/public_layout'; -import { getSettings } from 'soapbox/actions/settings'; +import { getSettings, getLocale } from 'soapbox/actions/settings'; import { getSoapboxConfig } from 'soapbox/actions/soapbox'; import { generateThemeCss } from 'soapbox/utils/theme'; import messages from 'soapbox/locales/messages'; import { FE_SUBDIRECTORY } from 'soapbox/build_config'; import { createGlobals } from 'soapbox/globals'; -const validLocale = locale => Object.keys(messages).includes(locale); - const previewMediaState = 'previewMediaModal'; const previewVideoState = 'previewVideoModal'; @@ -53,7 +51,7 @@ const mapStateToProps = (state) => { const showIntroduction = account ? state.getIn(['settings', 'introductionVersion'], 0) < INTRODUCTION_VERSION : false; const settings = getSettings(state); const soapboxConfig = getSoapboxConfig(state); - const locale = settings.get('locale'); + const locale = getLocale(state); return { showIntroduction, @@ -63,7 +61,7 @@ const mapStateToProps = (state) => { systemFont: settings.get('systemFont'), dyslexicFont: settings.get('dyslexicFont'), demetricator: settings.get('demetricator'), - locale: validLocale(locale) ? locale : 'en', + locale: locale, themeCss: generateThemeCss(soapboxConfig.get('brandColor')), brandColor: soapboxConfig.get('brandColor'), themeMode: settings.get('themeMode'),