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:
@@ -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)),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user