Files
suwayomi-material-you-webui/src/modules/core/utils/Languages.ts

106 lines
2.9 KiB
TypeScript
Raw Normal View History

/*
* 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
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { t } from 'i18next';
2024-10-05 23:22:42 +02:00
import { ISOLanguage, IsoLanguages } from '@/modules/core/IsoLanguages.ts';
2021-03-09 16:44:09 +03:30
export enum DefaultLanguage {
ALL = 'all',
OTHER = 'other',
LOCAL_SOURCE = 'localsourcelang',
}
2021-09-18 00:48:48 +04:30
type LanguageObject = ISOLanguage & { orgCode: string; isoCode: string };
function getISOLanguage(code: string): LanguageObject | null {
2024-10-05 16:09:34 +02:00
if (IsoLanguages[code]) {
return {
...IsoLanguages[code],
orgCode: code,
isoCode: code,
};
}
2024-10-05 16:09:34 +02:00
if (IsoLanguages[code.toLocaleLowerCase()]) {
return {
...IsoLanguages[code.toLocaleLowerCase()],
orgCode: code,
isoCode: code.toLocaleLowerCase(),
};
}
2021-03-09 16:44:09 +03:30
2021-03-09 18:05:34 +03:30
const whereToCut = code.indexOf('-') !== -1 ? code.indexOf('-') : code.length;
const processedCode = code.toLocaleLowerCase().substring(0, whereToCut);
2024-10-05 16:09:34 +02:00
if (IsoLanguages[processedCode]) {
return {
...IsoLanguages[processedCode],
orgCode: code,
isoCode: processedCode,
};
}
2021-03-09 18:05:34 +03:30
return null;
}
2021-03-09 18:05:34 +03:30
export function getLanguage(code: string): LanguageObject {
const isoLanguage = getISOLanguage(code);
if (isoLanguage) {
return isoLanguage;
2021-03-09 18:05:34 +03:30
}
return {
orgCode: code,
isoCode: code,
name: t('global.language.label.language_with_code', { code }),
nativeName: t('global.language.label.language_with_code', { code }),
};
}
export function langCodeToName(code: string): string {
return getLanguage(code).nativeName;
2021-03-09 16:44:09 +03:30
}
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);
};
function defaultNativeLang(): readonly string[] {
const preferredLanguages = toUniqueLanguageCodes([...navigator.languages]);
if (!preferredLanguages.length) {
return ['en'];
}
return preferredLanguages;
}
export function extensionDefaultLangs(): string[] {
return [...defaultNativeLang()];
}
export function sourceDefualtLangs(): string[] {
2025-04-21 16:50:06 +02:00
return [...defaultNativeLang()];
2021-03-09 16:44:09 +03:30
}
export const langSortCmp = (a: string, b: string) => {
// puts english first for convience
const aLang = langCodeToName(a);
const bLang = langCodeToName(b);
if (a === 'en') return -1;
if (b === 'en') return 1;
2021-09-18 00:48:48 +04:30
2024-09-30 03:45:37 +02:00
return aLang.localeCompare(bLang);
2021-03-09 16:44:09 +03:30
};