Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

fix(i18n): fall back to English translations

Gabe Kangas0e00aa932417
diff --git a/web/tests/localeLoader.test.ts b/web/tests/localeLoader.test.ts
index 022e296233..d263ea415a 100644
--- a/web/tests/localeLoader.test.ts
+++ b/web/tests/localeLoader.test.ts
@@ -110,6 +110,23 @@ describe('loadViewerLocale', () => {
});
});
+ test('missing and empty translations fall back to English', async () => {
+ setBrowserLanguage('el-GR');
+ const { load, i18n } = freshModules();
+ const { router } = makeRouter({});
+
+ await load(router);
+
+ const english = i18n.translations.en as {
+ Admin: { Plugins: Record<string, string> };
+ Testing: Record<string, string>;
+ };
+ const greek = i18n.translations.el as typeof english;
+ expect(greek.Admin.Plugins.browseSearch).toBe(english.Admin.Plugins.browseSearch);
+ expect(greek.Testing.noPluralKey).toBe(english.Testing.noPluralKey);
+ expect(greek.Testing.simpleKey).not.toBe(english.Testing.simpleKey);
+ });
+
test('explicit ?lang=de bounces the query param off and back on', async () => {
setBrowserLanguage('en-US');
const { load, i18n } = freshModules();
diff --git a/web/utils/localeLoader.ts b/web/utils/localeLoader.ts
index af4e0e7e7b..c91eb5d731 100644
--- a/web/utils/localeLoader.ts
+++ b/web/utils/localeLoader.ts
@@ -1,6 +1,29 @@
import type { NextRouter } from 'next/router';
import i18n from '../i18n';
+interface TranslationCatalog {
+ [key: string]: string | TranslationCatalog;
+}
+
+const withFallbackTranslations = (
+ catalog: TranslationCatalog,
+ fallback: TranslationCatalog,
+): TranslationCatalog => {
+ const merged = { ...catalog };
+ Object.entries(fallback).forEach(([key, fallbackValue]) => {
+ const value = merged[key];
+ if (typeof fallbackValue === 'string') {
+ if (typeof value !== 'string' || value.trim() === '') {
+ merged[key] = fallbackValue;
+ }
+ return;
+ }
+
+ merged[key] = withFallbackTranslations(typeof value === 'object' ? value : {}, fallbackValue);
+ });
+ return merged;
+};
+
/**
* Locales with a catalog under web/i18n/ that we offer to browsers. This
* mirrors the set that used to be bundled statically in i18n/index.js.
@@ -103,8 +126,12 @@ export const loadViewerLocale = async (router: NextRouter): Promise<void> => {
// The locale is only known at runtime (browser language or ?lang=), so a
// static import cannot work here. webpack turns this into one chunk per
// catalog and loads just the one the viewer needs.
- const catalog = await import(`../i18n/${locale}/translation.json`);
- i18n.translations[locale] = catalog.default ?? catalog;
+ const importedCatalog = await import(`../i18n/${locale}/translation.json`);
+ const catalog = (importedCatalog.default ?? importedCatalog) as TranslationCatalog;
+ i18n.translations[locale] = withFallbackTranslations(
+ catalog,
+ i18n.translations.en as TranslationCatalog,
+ );
const { lang, ...rest } = router.query;
if (lang) {