From 1692068e45dc492f4661e1ab775323c633250c96 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 17 Sep 2026 15:40:35 +0200 Subject: [PATCH] fix(l10n): fall back to base language Signed-off-by: Ferdinand Thiessen --- lib/utils/l10n.spec.ts | 7 +++++++ lib/utils/l10n.ts | 26 ++++++++++++++++++-------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/utils/l10n.spec.ts b/lib/utils/l10n.spec.ts index 4852ee5f4..af3e91915 100644 --- a/lib/utils/l10n.spec.ts +++ b/lib/utils/l10n.spec.ts @@ -13,3 +13,10 @@ test('translation compressing and decompressing is working', async () => { const { t } = await import('./l10n.ts') expect(t('Names must not be empty.')).toMatchInlineSnapshot('"Namen dürfen nicht leer sein."') }) + +// if a language is not available, it should fall back to the base language (e.g. ja-JP -> ja) +test('properly falls back to base language', async () => { + setLanguage('ja') + const { t } = await import('./l10n.ts') + expect(t('Names must not be empty.')).toMatchInlineSnapshot('"名前は空にできません。"') +}) diff --git a/lib/utils/l10n.ts b/lib/utils/l10n.ts index 3b5ef4261..0ca9603d1 100644 --- a/lib/utils/l10n.ts +++ b/lib/utils/l10n.ts @@ -5,20 +5,30 @@ import type { GettextTranslation, GettextTranslationBundle } from '@nextcloud/l10n/gettext' +import { getLanguage } from '@nextcloud/l10n' import { getGettextBuilder } from '@nextcloud/l10n/gettext' +const currentLanguage = getLanguage().replace('-', '_') const gtBuilder = getGettextBuilder() - .detectLanguage() + .setLanguage(currentLanguage) -for (const data of __TRANSLATIONS__) { +// eslint-disable-next-line @stylistic/semi +const translations = __TRANSLATIONS__; + +const languages = translations.map(({ language }) => language) +const useBaseLanguage = !languages.includes(currentLanguage) +for (const data of translations) { const { language, translations } = data as { language: string, translations: GettextTranslation[] } - const bundle: GettextTranslationBundle = { - headers: {}, - translations: { - '': Object.fromEntries(translations.map((translation: GettextTranslation) => [translation.msgid, translation])), - }, + const needsRename = useBaseLanguage && currentLanguage === language.split('_')[0] + if (language === currentLanguage || language === 'en' || needsRename) { + const bundle: GettextTranslationBundle = { + headers: {}, + translations: { + '': Object.fromEntries(translations.map((translation: GettextTranslation) => [translation.msgid, translation])), + }, + } + gtBuilder.addTranslation(needsRename ? currentLanguage : language, bundle) } - gtBuilder.addTranslation(language, bundle) } const gt = gtBuilder.build()