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()