From ac8b118d90e4900eae28e0180c0b0a5951bc9532 Mon Sep 17 00:00:00 2001 From: Matthias Ahouansou Date: Mon, 9 Dec 2024 12:51:15 +0000 Subject: [PATCH] mangadex: add option to use source language for title (#6471) * mangadex: add option to use source language for title closes #5747 * use 'extension' instead of 'source' for variable names & translations * simplify title selection --- .../assets/i18n/messages_en.properties | 2 ++ src/all/mangadex/build.gradle | 2 +- .../extension/all/mangadex/MDConstants.kt | 5 ++++ .../extension/all/mangadex/MangaDex.kt | 26 +++++++++++++++--- .../extension/all/mangadex/MangaDexHelper.kt | 27 ++++++++++++++----- 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/all/mangadex/assets/i18n/messages_en.properties b/src/all/mangadex/assets/i18n/messages_en.properties index 435187ebc..35ea71079 100644 --- a/src/all/mangadex/assets/i18n/messages_en.properties +++ b/src/all/mangadex/assets/i18n/messages_en.properties @@ -76,6 +76,8 @@ original_language=Original language original_language_filter_chinese=%s (Manhua) original_language_filter_japanese=%s (Manga) original_language_filter_korean=%s (Manhwa) +prefer_title_in_extension_language=Use Alternate Titles +prefer_title_in_extension_language_summary=If there is an alternate title available which matches the extension language, it will be used publication_demographic=Publication demographic publication_demographic_josei=Josei publication_demographic_none=None diff --git a/src/all/mangadex/build.gradle b/src/all/mangadex/build.gradle index cb1e6571a..a1dd4d3b0 100644 --- a/src/all/mangadex/build.gradle +++ b/src/all/mangadex/build.gradle @@ -1,7 +1,7 @@ ext { extName = 'MangaDex' extClass = '.MangaDexFactory' - extVersionCode = 195 + extVersionCode = 196 isNsfw = true } diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MDConstants.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MDConstants.kt index 69f79d63b..23fdfcec6 100644 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MDConstants.kt +++ b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MDConstants.kt @@ -138,6 +138,11 @@ object MDConstants { return "${altTitlesInDescPref}_$dexLang" } + private const val preferExtensionLangTitlePref = "preferExtensionLangTitle" + fun getPreferExtensionLangTitlePrefKey(dexLang: String): String { + return "${preferExtensionLangTitlePref}_$dexLang" + } + private const val tagGroupContent = "content" private const val tagGroupFormat = "format" private const val tagGroupGenre = "genre" diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt index 17219399d..c6ea2ceb2 100644 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt +++ b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt @@ -113,7 +113,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St .firstInstanceOrNull() ?.attributes?.fileName } - helper.createBasicManga(mangaDataDto, fileName, coverSuffix, dexLang) + helper.createBasicManga(mangaDataDto, fileName, coverSuffix, dexLang, preferences.preferExtensionLangTitle) } return MangasPage(mangaList, mangaListDto.hasNextPage) @@ -177,7 +177,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St .firstInstanceOrNull() ?.attributes?.fileName } - helper.createBasicManga(mangaDataDto, fileName, coverSuffix, dexLang) + helper.createBasicManga(mangaDataDto, fileName, coverSuffix, dexLang, preferences.preferExtensionLangTitle) } return MangasPage(mangaList, chapterListDto.hasNextPage) @@ -360,7 +360,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St .firstInstanceOrNull() ?.attributes?.fileName } - helper.createBasicManga(mangaDataDto, fileName, coverSuffix, dexLang) + helper.createBasicManga(mangaDataDto, fileName, coverSuffix, dexLang, preferences.preferExtensionLangTitle) } return mangaList @@ -423,6 +423,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St dexLang, preferences.coverQuality, preferences.altTitlesInDesc, + preferences.preferExtensionLangTitle, ) } @@ -757,11 +758,27 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St } } + val preferExtensionLangTitlePref = SwitchPreferenceCompat(screen.context).apply { + key = MDConstants.getPreferExtensionLangTitlePrefKey(dexLang) + title = helper.intl["prefer_title_in_extension_language"] + summary = helper.intl["prefer_title_in_extension_language_summary"] + setDefaultValue(true) + + setOnPreferenceChangeListener { _, newValue -> + val checkValue = newValue as Boolean + + preferences.edit() + .putBoolean(MDConstants.getPreferExtensionLangTitlePrefKey(dexLang), checkValue) + .commit() + } + } + screen.addPreference(coverQualityPref) screen.addPreference(tryUsingFirstVolumeCoverPref) screen.addPreference(dataSaverPref) screen.addPreference(standardHttpsPortPref) screen.addPreference(altTitlesInDescPref) + screen.addPreference(preferExtensionLangTitlePref) screen.addPreference(contentRatingPref) screen.addPreference(originalLanguagePref) screen.addPreference(blockedGroupsPref) @@ -840,6 +857,9 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St private val SharedPreferences.altTitlesInDesc get() = getBoolean(MDConstants.getAltTitlesInDescPrefKey(dexLang), false) + private val SharedPreferences.preferExtensionLangTitle + get() = getBoolean(MDConstants.getPreferExtensionLangTitlePrefKey(dexLang), true) + /** * Previous versions of the extension allowed invalid UUID values to be stored in the * preferences. This method clear invalid UUIDs in case the user have updated from diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDexHelper.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDexHelper.kt index 1a72db3bb..6cfb7c19c 100644 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDexHelper.kt +++ b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDexHelper.kt @@ -275,6 +275,9 @@ class MangaDexHelper(lang: String) { return GET(tokenRequestUrl, headers, cacheControl) } + private fun List>.findTitleByLang(lang: String): String? = + firstOrNull { it[lang] != null }?.values?.singleOrNull() + /** * Create a [SManga] from the JSON element with only basic attributes filled. */ @@ -283,15 +286,24 @@ class MangaDexHelper(lang: String) { coverFileName: String?, coverSuffix: String?, lang: String, + preferExtensionLangTitle: Boolean, ): SManga = SManga.create().apply { url = "/manga/${mangaDataDto.id}" + val titleMap = mangaDataDto.attributes!!.title - val dirtyTitle = - titleMap.values.firstOrNull() // use literally anything from title as first resort - ?: mangaDataDto.attributes.altTitles - .find { (it[lang] ?: it["en"]) !== null } - ?.values?.singleOrNull() // find something else from alt titles - title = dirtyTitle?.removeEntities().orEmpty() + title = with(mangaDataDto.attributes) { + titleMap[lang] ?: altTitles.run { + val mainTitle = titleMap.values.firstOrNull() + val langTitle = findTitleByLang(lang) + val enTitle = findTitleByLang("en") + + if (preferExtensionLangTitle) { + listOf(langTitle, mainTitle, enTitle) + } else { + listOf(mainTitle, langTitle, enTitle) + }.firstNotNullOfOrNull { it } + } + }?.removeEntities().orEmpty() coverFileName?.let { thumbnail_url = when (!coverSuffix.isNullOrEmpty()) { @@ -311,6 +323,7 @@ class MangaDexHelper(lang: String) { lang: String, coverSuffix: String?, altTitlesInDesc: Boolean, + preferExtensionLangTitle: Boolean, ): SManga { val attr = mangaDataDto.attributes!! @@ -370,7 +383,7 @@ class MangaDexHelper(lang: String) { } } - return createBasicManga(mangaDataDto, coverFileName, coverSuffix, lang).apply { + return createBasicManga(mangaDataDto, coverFileName, coverSuffix, lang, preferExtensionLangTitle).apply { description = desc author = authors.joinToString() artist = artists.joinToString()