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:
@@ -75,6 +75,10 @@ export const toUniqueLanguageCodes = (codes: string[]): string[] => {
|
|||||||
.map(([, languagesOfIsoCode]) => languagesOfIsoCode![0].orgCode);
|
.map(([, languagesOfIsoCode]) => languagesOfIsoCode![0].orgCode);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const toComparableLanguage = (code: string): string => getLanguage(code).isoCode;
|
||||||
|
|
||||||
|
export const toComparableLanguages = (codes: string[]): string[] => codes.map(toComparableLanguage);
|
||||||
|
|
||||||
function defaultNativeLang(): readonly string[] {
|
function defaultNativeLang(): readonly string[] {
|
||||||
const preferredLanguages = toUniqueLanguageCodes([...navigator.languages]);
|
const preferredLanguages = toUniqueLanguageCodes([...navigator.languages]);
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,14 @@ import {
|
|||||||
InstalledState,
|
InstalledState,
|
||||||
TExtension,
|
TExtension,
|
||||||
} from '@/modules/extension/Extensions.types.ts';
|
} from '@/modules/extension/Extensions.types.ts';
|
||||||
import { DefaultLanguage, langCodeToName, langSortCmp } from '@/modules/core/utils/Languages.ts';
|
import {
|
||||||
|
DefaultLanguage,
|
||||||
|
langCodeToName,
|
||||||
|
langSortCmp,
|
||||||
|
toComparableLanguage,
|
||||||
|
toComparableLanguages,
|
||||||
|
toUniqueLanguageCodes,
|
||||||
|
} from '@/modules/core/utils/Languages.ts';
|
||||||
import { extensionLanguageToTranslationKey } from '@/modules/extension/Extensions.constants.ts';
|
import { extensionLanguageToTranslationKey } from '@/modules/extension/Extensions.constants.ts';
|
||||||
import { enhancedCleanup } from '@/util/Strings.ts';
|
import { enhancedCleanup } from '@/util/Strings.ts';
|
||||||
|
|
||||||
@@ -98,8 +105,14 @@ export const filterExtensions = (
|
|||||||
selectedLanguages: string[],
|
selectedLanguages: string[],
|
||||||
showNsfw: boolean,
|
showNsfw: boolean,
|
||||||
query: string | null | undefined,
|
query: string | null | undefined,
|
||||||
): TExtension[] =>
|
): TExtension[] => {
|
||||||
extensions
|
const normalizedSelectedLanguages = toComparableLanguages(toUniqueLanguageCodes(selectedLanguages));
|
||||||
.filter((extension) => selectedLanguages.includes(extension.lang) || extension.isInstalled)
|
|
||||||
|
return extensions
|
||||||
|
.filter(
|
||||||
|
(extension) =>
|
||||||
|
normalizedSelectedLanguages.includes(toComparableLanguage(extension.lang)) || extension.isInstalled,
|
||||||
|
)
|
||||||
.filter((extension) => showNsfw || !extension.isNsfw)
|
.filter((extension) => showNsfw || !extension.isNsfw)
|
||||||
.filter((extension) => !query || enhancedCleanup(extension.name).includes(enhancedCleanup(query)));
|
.filter((extension) => !query || enhancedCleanup(extension.name).includes(enhancedCleanup(query)));
|
||||||
|
};
|
||||||
|
|||||||
@@ -13,7 +13,13 @@ import {
|
|||||||
SourceNsfwInfo,
|
SourceNsfwInfo,
|
||||||
SourceRepoInfo,
|
SourceRepoInfo,
|
||||||
} from '@/modules/source/Source.types.ts';
|
} 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 {
|
export class Sources {
|
||||||
static readonly LOCAL_SOURCE_ID = '0';
|
static readonly LOCAL_SOURCE_ID = '0';
|
||||||
@@ -55,12 +61,14 @@ export class Sources {
|
|||||||
keepLocalSource,
|
keepLocalSource,
|
||||||
}: { showNsfw?: boolean; languages?: string[]; keepLocalSource?: boolean } = {},
|
}: { showNsfw?: boolean; languages?: string[]; keepLocalSource?: boolean } = {},
|
||||||
): Source[] {
|
): Source[] {
|
||||||
|
const normalizedLanguages = toComparableLanguages(toUniqueLanguageCodes(languages ?? []));
|
||||||
|
|
||||||
return sources
|
return sources
|
||||||
.filter((source) => showNsfw || !source.isNsfw || (keepLocalSource && Sources.isLocalSource(source)))
|
.filter((source) => showNsfw || !source.isNsfw || (keepLocalSource && Sources.isLocalSource(source)))
|
||||||
.filter(
|
.filter(
|
||||||
(source) =>
|
(source) =>
|
||||||
!languages ||
|
!languages ||
|
||||||
languages.includes(Sources.getLanguage(source)) ||
|
normalizedLanguages.includes(toComparableLanguage(Sources.getLanguage(source))) ||
|
||||||
(keepLocalSource && Sources.isLocalSource(source)),
|
(keepLocalSource && Sources.isLocalSource(source)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user