feat(YouTube - Settings): Add RVX language setting https://github.com/inotia00/ReVanced_Extended/issues/2680

This commit is contained in:
inotia00
2025-01-17 14:43:57 +09:00
parent 244e792e23
commit b211df4288
8 changed files with 346 additions and 22 deletions

View File

@ -49,6 +49,7 @@ fun ResourcePatchContext.baseTranslationsPatch(
sourceDirectory: String,
) {
val resourceDirectory = get("res")
val isYouTube = sourceDirectory == "youtube"
// Check if the custom translation path is valid.
customTranslations?.takeIf { it.isNotEmpty() }?.let { customLang ->
@ -90,10 +91,10 @@ fun ResourcePatchContext.baseTranslationsPatch(
// Filter the app languages to include both versions of locales (with and without 'r', en-rGB and en-GB)
// and also handle locales with "b+" prefix
val filteredAppLanguages = selectedStringResourcesArray.flatMap { language ->
setOf(language, language.replace("-r", "-"),
language.replace("b+", "").replace("+", "-"))
}.toTypedArray()
var filteredAppLanguages = (selectedStringResourcesArray + arrayOf("en"))
.map { language ->
language.replace("-r", "-").replace("b+", "").replace("+", "-")
}.toHashSet().toTypedArray()
// Remove unselected app languages from UI
document("res/xml/locales_config.xml").use { document ->
@ -102,7 +103,7 @@ fun ResourcePatchContext.baseTranslationsPatch(
document.doRecursively { node ->
if (node is Element && node.tagName == "locale") {
node.getAttributeNode("android:name")?.let { attribute ->
if (attribute.textContent != "en" && attribute.textContent !in filteredAppLanguages) {
if (attribute.textContent !in filteredAppLanguages) {
nodesToRemove.add(node)
}
}
@ -114,6 +115,45 @@ fun ResourcePatchContext.baseTranslationsPatch(
node.parentNode?.removeChild(node)
}
}
if (!isYouTube) return
filteredAppLanguages = filteredAppLanguages.map { language ->
language.subSequence(0,2).toString().uppercase()
}.toHashSet().toTypedArray()
// Remove unselected app languages from RVX Settings
setOf(
"revanced_language_entries",
"revanced_language_entry_values",
).forEach { attributeName ->
document("res/values/arrays.xml").use { document ->
with(document) {
val nodesToRemove = mutableListOf<Node>()
val resourcesNode = getElementsByTagName("resources").item(0) as Element
for (i in 0 until resourcesNode.childNodes.length) {
val node = resourcesNode.childNodes.item(i) as? Element ?: continue
if (node.getAttribute("name") == attributeName) {
for (j in 0 until node.childNodes.length) {
val item = node.childNodes.item(j) as? Element ?: continue
val text = item.textContent
val length = text.length
if (!text.endsWith("DEFAULT") && text.subSequence(length - 2, length) !in filteredAppLanguages) {
nodesToRemove.add(item)
}
}
}
}
// Remove the collected nodes (avoids NullPointerException)
for (n in nodesToRemove) {
n.parentNode?.removeChild(n)
}
}
}
}
}
/**