From 97c1dbbd79f2605e0c1b53a06aa6012a4987e137 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 13 Sep 2023 21:59:39 -0500 Subject: [PATCH] Fix locale dynamic imports --- app/soapbox/actions/settings.ts | 2 +- app/soapbox/actions/streaming.ts | 2 +- app/soapbox/containers/soapbox.tsx | 2 +- app/soapbox/{locales => }/messages.ts | 26 ++++++++++++++----------- app/soapbox/schemas/soapbox/settings.ts | 2 +- 5 files changed, 19 insertions(+), 15 deletions(-) rename app/soapbox/{locales => }/messages.ts (70%) diff --git a/app/soapbox/actions/settings.ts b/app/soapbox/actions/settings.ts index 1e5c241d1..57db0e438 100644 --- a/app/soapbox/actions/settings.ts +++ b/app/soapbox/actions/settings.ts @@ -4,7 +4,7 @@ import { createSelector } from 'reselect'; import { v4 as uuid } from 'uuid'; import { patchMe } from 'soapbox/actions/me'; -import messages from 'soapbox/locales/messages'; +import messages from 'soapbox/messages'; import toast from 'soapbox/toast'; import { isLoggedIn } from 'soapbox/utils/auth'; diff --git a/app/soapbox/actions/streaming.ts b/app/soapbox/actions/streaming.ts index fa117d690..e72b20239 100644 --- a/app/soapbox/actions/streaming.ts +++ b/app/soapbox/actions/streaming.ts @@ -2,7 +2,7 @@ import { getLocale, getSettings } from 'soapbox/actions/settings'; import { importEntities } from 'soapbox/entity-store/actions'; import { Entities } from 'soapbox/entity-store/entities'; import { selectEntity } from 'soapbox/entity-store/selectors'; -import messages from 'soapbox/locales/messages'; +import messages from 'soapbox/messages'; import { ChatKeys, IChat, isLastMessage } from 'soapbox/queries/chats'; import { queryClient } from 'soapbox/queries/client'; import { getUnreadChatsCount, updateChatListItem, updateChatMessage } from 'soapbox/utils/chats'; diff --git a/app/soapbox/containers/soapbox.tsx b/app/soapbox/containers/soapbox.tsx index ec63d966d..9f8081c9f 100644 --- a/app/soapbox/containers/soapbox.tsx +++ b/app/soapbox/containers/soapbox.tsx @@ -42,7 +42,7 @@ import { useInstance, useRegistrationStatus, } from 'soapbox/hooks'; -import MESSAGES from 'soapbox/locales/messages'; +import MESSAGES from 'soapbox/messages'; import { normalizeSoapboxConfig } from 'soapbox/normalizers'; import { queryClient } from 'soapbox/queries/client'; import { useCachedLocationHandler } from 'soapbox/utils/redirect'; diff --git a/app/soapbox/locales/messages.ts b/app/soapbox/messages.ts similarity index 70% rename from app/soapbox/locales/messages.ts rename to app/soapbox/messages.ts index 1f69648e0..c7aebb4d4 100644 --- a/app/soapbox/locales/messages.ts +++ b/app/soapbox/messages.ts @@ -2,28 +2,32 @@ type MessageJson = Record; type MessageModule = { default: MessageJson }; /** Import custom messages */ -const importCustom = (locale: string): Promise => { - return import(/* webpackChunkName: "locale_[request]" */`custom/locales/${locale}.json`) - .catch(() => ({ default: {} })); +const importCustom = async (locale: string): Promise => { + try { + return await import(`../../custom/locales/${locale}.json`); + } catch { + return ({ default: {} }); + } }; /** Import git-checked messages */ const importMessages = (locale: string): Promise => { - return import(/* webpackChunkName: "locale_[request]" */`./${locale}.json`); + return import(`./locales/${locale}.json`); }; /** Override custom messages */ -const importMessagesWithCustom = (locale: string): Promise => { - return Promise.all([ - importMessages(locale), - importCustom(locale), - ]).then(messages => { +const importMessagesWithCustom = async (locale: string): Promise => { + try { + const messages = await Promise.all([ + importMessages(locale), + importCustom(locale), + ]); const [native, custom] = messages; return Object.assign(native.default, custom.default); - }).catch(error => { + } catch (error) { console.error(error); throw error; - }); + } }; const locales = [ diff --git a/app/soapbox/schemas/soapbox/settings.ts b/app/soapbox/schemas/soapbox/settings.ts index 1b0db4d86..788f0e96c 100644 --- a/app/soapbox/schemas/soapbox/settings.ts +++ b/app/soapbox/schemas/soapbox/settings.ts @@ -1,6 +1,6 @@ import { z } from 'zod'; -import { locales } from 'soapbox/locales/messages'; +import { locales } from 'soapbox/messages'; const skinToneSchema = z.union([ z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6),