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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-03-23 13:59:32 +01:00
|
|
|
import { t } from 'i18next';
|
2024-06-25 17:11:33 +02:00
|
|
|
import { ISOLanguage, ISOLanguages } from '@/util/isoLanguages.ts';
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2023-04-21 00:27:42 +02:00
|
|
|
export enum DefaultLanguage {
|
|
|
|
|
ALL = 'all',
|
|
|
|
|
OTHER = 'other',
|
|
|
|
|
LOCAL_SOURCE = 'localsourcelang',
|
|
|
|
|
}
|
2021-09-18 00:48:48 +04:30
|
|
|
|
2024-03-09 02:52:34 +01:00
|
|
|
function getISOLanguage(code: string): ISOLanguage | null {
|
|
|
|
|
if (ISOLanguages[code]) {
|
|
|
|
|
return ISOLanguages[code];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ISOLanguages[code.toLocaleLowerCase()]) {
|
|
|
|
|
return ISOLanguages[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;
|
2024-03-09 02:52:34 +01:00
|
|
|
const processedCode = code.toLocaleLowerCase().substring(0, whereToCut);
|
|
|
|
|
if (ISOLanguages[processedCode]) {
|
|
|
|
|
return ISOLanguages[processedCode];
|
|
|
|
|
}
|
2021-03-09 18:05:34 +03:30
|
|
|
|
2024-03-09 02:52:34 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
2021-03-09 18:05:34 +03:30
|
|
|
|
2024-03-09 02:52:34 +01:00
|
|
|
export function getLanguage(code: string): { name: string; nativeName: string } {
|
|
|
|
|
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 {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-10-18 14:02:51 +03:30
|
|
|
function defaultNativeLang() {
|
|
|
|
|
return 'en'; // TODO: infer from the browser
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function extensionDefaultLangs() {
|
2023-04-21 00:27:42 +02:00
|
|
|
return [defaultNativeLang(), DefaultLanguage.ALL];
|
2021-10-18 14:02:51 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function sourceDefualtLangs() {
|
2023-04-21 00:27:42 +02:00
|
|
|
return [defaultNativeLang(), DefaultLanguage.LOCAL_SOURCE];
|
2021-10-18 14:02:51 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function sourceForcedDefaultLangs(): string[] {
|
2023-04-21 00:27:42 +02:00
|
|
|
return [DefaultLanguage.LOCAL_SOURCE];
|
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
|
|
|
};
|