Normalize language code comparisons

Due to using the browsers' preferred languages by default, there was a possibility that the preferred language codes did not match any extension/source language, which lead to broken filtering.

While this happened, it was also possible that the preferred languages still matched properly to a language in the language filter, which caused confusion.
This was the case e.g., for "en-GB" or "en-US"
This commit is contained in:
schroda
2025-04-24 13:38:05 +02:00
parent 01856b9847
commit 4c04e1d17c
3 changed files with 31 additions and 6 deletions

View File

@@ -13,7 +13,13 @@ import {
SourceNsfwInfo,
SourceRepoInfo,
} from '@/modules/source/Source.types.ts';
import { DefaultLanguage, langSortCmp } from '@/modules/core/utils/Languages.ts';
import {
DefaultLanguage,
langSortCmp,
toComparableLanguage,
toComparableLanguages,
toUniqueLanguageCodes,
} from '@/modules/core/utils/Languages.ts';
export class Sources {
static readonly LOCAL_SOURCE_ID = '0';
@@ -55,12 +61,14 @@ export class Sources {
keepLocalSource,
}: { showNsfw?: boolean; languages?: string[]; keepLocalSource?: boolean } = {},
): Source[] {
const normalizedLanguages = toComparableLanguages(toUniqueLanguageCodes(languages ?? []));
return sources
.filter((source) => showNsfw || !source.isNsfw || (keepLocalSource && Sources.isLocalSource(source)))
.filter(
(source) =>
!languages ||
languages.includes(Sources.getLanguage(source)) ||
normalizedLanguages.includes(toComparableLanguage(Sources.getLanguage(source))) ||
(keepLocalSource && Sources.isLocalSource(source)),
);
}