Skip to content

Commit

Permalink
feat(YT Music - Translations): Add compile time options to select cus…
Browse files Browse the repository at this point in the history
…tom language, RVX and app languages
  • Loading branch information
anddea committed May 17, 2024
1 parent 6356ddc commit 074d3e2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 27 deletions.
8 changes: 8 additions & 0 deletions api/revanced-patches.api
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ public final class app/revanced/patches/music/misc/translations/TranslationsPatc
public fun execute (Lapp/revanced/patcher/data/ResourceContext;)V
}

public final class app/revanced/patches/music/misc/translations/TranslationsPatchKt {
public static final fun getLANGUAGES ()[Ljava/lang/String;
}

public final class app/revanced/patches/music/navigation/components/NavigationBarComponentsPatch : app/revanced/util/patch/BaseBytecodePatch {
public static final field INSTANCE Lapp/revanced/patches/music/navigation/components/NavigationBarComponentsPatch;
public fun execute (Lapp/revanced/patcher/data/BytecodeContext;)V
Expand Down Expand Up @@ -845,6 +849,10 @@ public final class app/revanced/patches/shared/translations/TranslationsUtils {
public static final field INSTANCE Lapp/revanced/patches/shared/translations/TranslationsUtils;
}

public final class app/revanced/patches/shared/translations/TranslationsUtilsKt {
public static final fun getAPP_LANGUAGES ()[Ljava/lang/String;
}

public final class app/revanced/patches/shared/voicesearch/VoiceSearchUtils {
public static final field INSTANCE Lapp/revanced/patches/shared/voicesearch/VoiceSearchUtils;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,83 @@
package app.revanced.patches.music.misc.translations

import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.stringPatchOption
import app.revanced.patches.music.utils.compatibility.Constants.COMPATIBLE_PACKAGE
import app.revanced.patches.music.utils.settings.SettingsPatch
import app.revanced.patches.shared.translations.APP_LANGUAGES
import app.revanced.patches.shared.translations.TranslationsUtils.copyXml
import app.revanced.patches.shared.translations.TranslationsUtils.updateStringsXml
import app.revanced.util.patch.BaseResourcePatch
import java.io.File

@Suppress("unused")
// Array of supported RVX languages, each represented by its language code.
val LANGUAGES = arrayOf(
"bg-rBG", "bn", "cs-rCZ", "el-rGR", "es-rES", "fr-rFR", "id-rID", "in", "it-rIT",
"ja-rJP", "ko-rKR", "nl-rNL", "pl-rPL", "pt-rBR", "ro-rRO", "ru-rRU", "tr-rTR", "uk-rUA",
"vi-rVN", "zh-rCN", "zh-rTW"
)

@Suppress("DEPRECATION", "unused")
object TranslationsPatch : BaseResourcePatch(
name = "Translations",
description = "Adds Crowdin translations for YouTube Music.",
dependencies = setOf(SettingsPatch::class),
compatiblePackages = COMPATIBLE_PACKAGE
) {
private var CustomLanguage by stringPatchOption(
key = "CustomLanguage",
default = "",
title = "Custom language file",
description = """
The file path to the strings.xml file.
Please note that applying the strings.xml file will overwrite all existing language translations.
""".trimIndent()
)

private var SelectedLanguages by stringPatchOption(
key = "SelectedLanguages",
default = LANGUAGES.joinToString(", "),
title = "Selected RVX languages",
description = "Selected RVX languages that will be added."
)

private var SelectedAppLanguages by stringPatchOption(
key = "SelectedAppLanguages",
default = APP_LANGUAGES.joinToString(", "),
title = "Selected app languages",
description = "Selected app languages that will be kept, languages that are not in the list will be removed from the app."
)

override fun execute(context: ResourceContext) {
context.copyXml(
"music",
arrayOf(
"bg-rBG",
"bn",
"cs-rCZ",
"el-rGR",
"es-rES",
"fr-rFR",
"id-rID",
"in",
"it-rIT",
"ja-rJP",
"ko-rKR",
"nl-rNL",
"pl-rPL",
"pt-rBR",
"ro-rRO",
"ru-rRU",
"tr-rTR",
"uk-rUA",
"vi-rVN",
"zh-rCN",
"zh-rTW"
)
)
CustomLanguage?.takeIf { it.isNotEmpty() }?.let { customLang ->
try {
val customLangFile = File(customLang)
if (!customLangFile.exists() || !customLangFile.isFile || customLangFile.name != "strings.xml") {
throw PatchException("Invalid custom language file: $customLang")
}
val resourceDirectory = context["res"].resolve("values")
val destinationFile = resourceDirectory.resolve("strings.xml")

updateStringsXml(customLangFile, destinationFile)
} catch (e: Exception) {
throw PatchException("Error copying custom language file: ${e.message}")
}
} ?: run {
// Process selected RVX languages if no custom language file is set.
val selectedLanguagesArray = SelectedLanguages!!.split(",").map { it.trim() }.toTypedArray()
val filteredLanguages = LANGUAGES.filter { it in selectedLanguagesArray }.toTypedArray()
context.copyXml("youtube", filteredLanguages)
}

// Process selected app languages.
val selectedAppLanguagesArray = SelectedAppLanguages!!.split(",").map { it.trim() }.toTypedArray()
val filteredAppLanguages = APP_LANGUAGES.filter { it in selectedAppLanguagesArray }.toTypedArray()
val resourceDirectory = context["res"]

// Remove unselected app languages.
APP_LANGUAGES.filter { it !in filteredAppLanguages }.forEach { language ->
resourceDirectory.resolve("values-$language").takeIf { it.exists() && it.isDirectory }?.deleteRecursively()
}
}
}

0 comments on commit 074d3e2

Please sign in to comment.