2021-03-26 04:17:02 +04:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
2021-03-09 16:44:09 +03:30
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-03-11 00:11:29 +01:00
|
|
|
import type { MessageDescriptor } from '@lingui/core';
|
2026-01-10 19:45:02 +01:00
|
|
|
import { msg, t } from '@lingui/core/macro';
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2026-03-11 00:11:29 +01:00
|
|
|
import type { LanguageObject } from '@/lib/ISOLanguageUtil.ts';
|
|
|
|
|
import { getISOLanguage, getPreferredISOLanguageCodes } from '@/lib/ISOLanguageUtil.ts';
|
2026-01-11 17:18:42 +01:00
|
|
|
|
2026-01-10 19:45:02 +01:00
|
|
|
import { i18n } from '@/i18n';
|
|
|
|
|
|
2023-04-21 00:27:42 +02:00
|
|
|
export enum DefaultLanguage {
|
|
|
|
|
ALL = 'all',
|
|
|
|
|
OTHER = 'other',
|
|
|
|
|
LOCAL_SOURCE = 'localsourcelang',
|
2025-05-17 22:17:15 +02:00
|
|
|
PINNED = 'pinned',
|
2025-05-17 23:37:00 +02:00
|
|
|
LAST_USED_SOURCE = 'last_used_source',
|
2023-04-21 00:27:42 +02:00
|
|
|
}
|
2021-09-18 00:48:48 +04:30
|
|
|
|
2026-01-10 19:45:02 +01:00
|
|
|
const DEFAULT_LANGUAGE_TO_TRANSLATION: Record<DefaultLanguage, MessageDescriptor> = {
|
|
|
|
|
[DefaultLanguage.ALL]: msg`All`,
|
|
|
|
|
[DefaultLanguage.OTHER]: msg`Other`,
|
|
|
|
|
[DefaultLanguage.LOCAL_SOURCE]: msg`Other`,
|
|
|
|
|
[DefaultLanguage.PINNED]: msg`Pinned`,
|
|
|
|
|
[DefaultLanguage.LAST_USED_SOURCE]: msg`Last used`,
|
2025-05-09 01:08:44 +02:00
|
|
|
};
|
|
|
|
|
|
2025-04-21 19:01:52 +02:00
|
|
|
export function getLanguage(code: string): LanguageObject {
|
2024-03-09 02:52:34 +01:00
|
|
|
const isoLanguage = getISOLanguage(code);
|
|
|
|
|
|
|
|
|
|
if (isoLanguage) {
|
|
|
|
|
return isoLanguage;
|
2021-03-09 18:05:34 +03:30
|
|
|
}
|
|
|
|
|
|
2024-03-09 02:52:34 +01:00
|
|
|
return {
|
2025-04-21 19:01:52 +02:00
|
|
|
orgCode: code,
|
|
|
|
|
isoCode: code,
|
2026-01-10 19:45:02 +01:00
|
|
|
name: t`Language with code: ${code}`,
|
|
|
|
|
nativeName: t`Language with code: ${code}`,
|
2024-03-09 02:52:34 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 00:42:56 +02:00
|
|
|
export function languageCodeToName(code: string): string {
|
2025-05-09 01:08:44 +02:00
|
|
|
const isCustomLanguage = Object.keys(DEFAULT_LANGUAGE_TO_TRANSLATION).includes(code);
|
|
|
|
|
if (isCustomLanguage) {
|
2026-01-10 19:45:02 +01:00
|
|
|
return i18n._(DEFAULT_LANGUAGE_TO_TRANSLATION[code as DefaultLanguage]);
|
2025-05-09 01:08:44 +02:00
|
|
|
}
|
|
|
|
|
|
2024-03-09 02:52:34 +01:00
|
|
|
return getLanguage(code).nativeName;
|
2021-03-09 16:44:09 +03:30
|
|
|
}
|
|
|
|
|
|
2026-01-14 16:33:37 +01:00
|
|
|
export const toUniqueLanguageCodes = (codes: string[]): string[] => {
|
|
|
|
|
const languages = codes.map((code) => getLanguage(code));
|
|
|
|
|
const languagesByIsoCode = Object.groupBy(languages, (language) => language.isoCode);
|
|
|
|
|
|
|
|
|
|
return Object.entries(languagesByIsoCode)
|
|
|
|
|
.filter(([, languagesOfIsoCode]) => !!languagesOfIsoCode?.length)
|
|
|
|
|
.map(([, languagesOfIsoCode]) => languagesOfIsoCode![0].orgCode);
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-24 13:38:05 +02:00
|
|
|
export const toComparableLanguage = (code: string): string => getLanguage(code).isoCode;
|
|
|
|
|
|
|
|
|
|
export const toComparableLanguages = (codes: string[]): string[] => codes.map(toComparableLanguage);
|
|
|
|
|
|
2025-05-09 00:44:48 +02:00
|
|
|
export function getDefaultLanguages(): string[] {
|
2026-01-11 17:18:42 +01:00
|
|
|
return [...getPreferredISOLanguageCodes(), DefaultLanguage.ALL];
|
2021-03-09 16:44:09 +03:30
|
|
|
}
|
|
|
|
|
|
2025-05-09 01:08:44 +02:00
|
|
|
/**
|
|
|
|
|
* Sort languages by their native name.
|
|
|
|
|
* Custom languages are optionally treated specially:
|
|
|
|
|
* - All: first
|
|
|
|
|
* - Other: last
|
|
|
|
|
*/
|
|
|
|
|
export const languageSortComparator = (a: string, b: string, specialCustomLanguagesHandling?: boolean) => {
|
|
|
|
|
const isALanguageAll = a === DefaultLanguage.ALL;
|
|
|
|
|
const isALanguageOther = a === DefaultLanguage.OTHER || a === DefaultLanguage.LOCAL_SOURCE;
|
|
|
|
|
|
|
|
|
|
const isBLanguageAll = b === DefaultLanguage.ALL;
|
|
|
|
|
const isBLanguageOther = b === DefaultLanguage.OTHER || b === DefaultLanguage.LOCAL_SOURCE;
|
|
|
|
|
|
|
|
|
|
if (specialCustomLanguagesHandling) {
|
|
|
|
|
if (isALanguageAll || isBLanguageOther) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isALanguageOther || isBLanguageAll) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return languageCodeToName(a).localeCompare(languageCodeToName(b));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sort languages by their native name.
|
|
|
|
|
* Custom languages are treated specially:
|
|
|
|
|
* - All: first
|
|
|
|
|
* - Other: last
|
|
|
|
|
*/
|
|
|
|
|
export const languageSpecialSortComparator = (a: string, b: string) => languageSortComparator(a, b, true);
|