properly fallback on locale

This commit is contained in:
Shevek
2021-11-08 21:21:41 -05:00
parent 6871db26c4
commit 6a3e66bc7e
3 changed files with 12 additions and 14 deletions

View File

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

View File

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

View File

@ -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'),