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);
|
||||
};
|
||||
|
||||
export const toComparableLanguage = (code: string): string => getLanguage(code).isoCode;
|
||||
|
||||
export const toComparableLanguages = (codes: string[]): string[] => codes.map(toComparableLanguage);
|
||||
|
||||
function defaultNativeLang(): readonly string[] {
|
||||
const preferredLanguages = toUniqueLanguageCodes([...navigator.languages]);
|
||||
|
||||
|
||||
@@ -15,7 +15,14 @@ import {
|
||||
InstalledState,
|
||||
TExtension,
|
||||
} 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 { enhancedCleanup } from '@/util/Strings.ts';
|
||||
|
||||
@@ -98,8 +105,14 @@ export const filterExtensions = (
|
||||
selectedLanguages: string[],
|
||||
showNsfw: boolean,
|
||||
query: string | null | undefined,
|
||||
): TExtension[] =>
|
||||
extensions
|
||||
.filter((extension) => selectedLanguages.includes(extension.lang) || extension.isInstalled)
|
||||
): TExtension[] => {
|
||||
const normalizedSelectedLanguages = toComparableLanguages(toUniqueLanguageCodes(selectedLanguages));
|
||||
|
||||
return extensions
|
||||
.filter(
|
||||
(extension) =>
|
||||
normalizedSelectedLanguages.includes(toComparableLanguage(extension.lang)) || extension.isInstalled,
|
||||
)
|
||||
.filter((extension) => showNsfw || !extension.isNsfw)
|
||||
.filter((extension) => !query || enhancedCleanup(extension.name).includes(enhancedCleanup(query)));
|
||||
};
|
||||
|
||||
@@ -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