From 1f499d9c51f5fbcb43fed26c7d15552ae055c27a Mon Sep 17 00:00:00 2001 From: inotia00 <108592928+inotia00@users.noreply.github.com> Date: Thu, 16 Jan 2025 12:23:54 +0900 Subject: [PATCH] feat(Spoof streaming data): Remove strings not included in the patch option `String resources to keep` from App languages as well --- .../translations/BaseTranslationsPatch.kt | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/patches/src/main/kotlin/app/revanced/patches/shared/translations/BaseTranslationsPatch.kt b/patches/src/main/kotlin/app/revanced/patches/shared/translations/BaseTranslationsPatch.kt index 8554a4d62..3c9dc5ee8 100644 --- a/patches/src/main/kotlin/app/revanced/patches/shared/translations/BaseTranslationsPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/shared/translations/BaseTranslationsPatch.kt @@ -2,7 +2,9 @@ package app.revanced.patches.shared.translations import app.revanced.patcher.patch.PatchException import app.revanced.patcher.patch.ResourcePatchContext +import app.revanced.util.doRecursively import app.revanced.util.inputStreamFromBundledResource +import org.w3c.dom.Element import org.w3c.dom.Node import java.io.File import java.nio.file.Files @@ -59,7 +61,7 @@ fun ResourcePatchContext.baseTranslationsPatch( val destinationFile = valuesDirectory.resolve("strings.xml") updateStringsXml(customLangFile, destinationFile) - } catch (e: Exception) { + } catch (_: Exception) { // Exception is thrown if an invalid path is used in the patch option. throw PatchException("Invalid custom translations path: $customLang") } @@ -85,6 +87,33 @@ fun ResourcePatchContext.baseTranslationsPatch( resourceDirectory.resolve("values-$language").takeIf { it.exists() && it.isDirectory } ?.deleteRecursively() } + + // 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() + + // Remove unselected app languages from UI + document("res/xml/locales_config.xml").use { document -> + val nodesToRemove = mutableListOf() + + document.doRecursively { node -> + if (node is Element && node.tagName == "locale") { + node.getAttributeNode("android:name")?.let { attribute -> + if (attribute.textContent != "en" && attribute.textContent !in filteredAppLanguages) { + nodesToRemove.add(node) + } + } + } + } + + // Remove the collected nodes (avoids NullPointerException) + for (node in nodesToRemove) { + node.parentNode?.removeChild(node) + } + } } /**