From d33dd334e1227c7ce761c2f8665cb6f76a031efb Mon Sep 17 00:00:00 2001 From: R0rt1z2 Date: Wed, 18 Sep 2024 20:40:16 +0200 Subject: [PATCH] GrindrPlus: Methods to support multi languages --- .../com/grindrplus/bridge/IBridgeService.aidl | 1 + .../main/java/com/grindrplus/GrindrPlus.kt | 20 +++++++++++++++++-- .../com/grindrplus/bridge/BridgeClient.kt | 13 ++++++++++++ .../com/grindrplus/bridge/BridgeService.kt | 10 ++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/app/src/main/aidl/com/grindrplus/bridge/IBridgeService.aidl b/app/src/main/aidl/com/grindrplus/bridge/IBridgeService.aidl index 012b6d0e..dac4dbe5 100644 --- a/app/src/main/aidl/com/grindrplus/bridge/IBridgeService.aidl +++ b/app/src/main/aidl/com/grindrplus/bridge/IBridgeService.aidl @@ -2,4 +2,5 @@ package com.grindrplus.bridge; interface IBridgeService { String getTranslation(String locale); + List getAvailableTranslations(); } \ No newline at end of file diff --git a/app/src/main/java/com/grindrplus/GrindrPlus.kt b/app/src/main/java/com/grindrplus/GrindrPlus.kt index 1130b152..221dd02d 100644 --- a/app/src/main/java/com/grindrplus/GrindrPlus.kt +++ b/app/src/main/java/com/grindrplus/GrindrPlus.kt @@ -39,6 +39,7 @@ object GrindrPlus { lateinit var hookManager: HookManager lateinit var translations: JSONObject + lateinit var localeTag: String var currentActivity: Activity? = null private set @@ -100,7 +101,7 @@ object GrindrPlus { bridgeClient = BridgeClient(context).apply { connect { - val localeTag = Config.get("locale", "") as String? + localeTag = Config.get("locale", "") as String? ?: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { context.resources.configuration.locales.get(0).toLanguageTag() } else { @@ -148,7 +149,22 @@ object GrindrPlus { return classLoader.loadClass(name) } + fun getTranslation(key: String, vararg placeholders: Pair): String { + var translation = translations.optString(key, key) + + placeholders.forEach { (placeholder, value) -> + translation = translation.replace("{$placeholder}", value) + } + + return translation + } + + fun getTranslation(key: String): String { + return translations.optString(key, key) + } + fun reloadTranslations(locale: String) { - translations = bridgeClient.getTranslation(locale) ?: JSONObject() + localeTag = locale + translations = bridgeClient.getTranslation(localeTag) ?: JSONObject() } } \ No newline at end of file diff --git a/app/src/main/java/com/grindrplus/bridge/BridgeClient.kt b/app/src/main/java/com/grindrplus/bridge/BridgeClient.kt index c63196d5..e443becf 100644 --- a/app/src/main/java/com/grindrplus/bridge/BridgeClient.kt +++ b/app/src/main/java/com/grindrplus/bridge/BridgeClient.kt @@ -87,4 +87,17 @@ class BridgeClient(private val context: Context) : ServiceConnection { JSONObject(it) } } + + /** + * Get list of available translations ("en_US", "es_ES", etc). + * @return List of available translations. + */ + fun getAvailableTranslations(): List { + if (!isBound) { + GrindrPlus.logger.log("Cannot get available translations, service is not bound!") + return emptyList() + } + + return bridgeService?.getAvailableTranslations() ?: emptyList() + } } diff --git a/app/src/main/java/com/grindrplus/bridge/BridgeService.kt b/app/src/main/java/com/grindrplus/bridge/BridgeService.kt index 9029c911..88f13322 100644 --- a/app/src/main/java/com/grindrplus/bridge/BridgeService.kt +++ b/app/src/main/java/com/grindrplus/bridge/BridgeService.kt @@ -29,5 +29,15 @@ class BridgeService : Service() { "{\"error\": \"Translation file not found or failed to load\"}" } } + + @Throws(RemoteException::class) + override fun getAvailableTranslations(): List { + return try { + assets.list("translations")?.map { it.removeSuffix(".json") } ?: emptyList() + } catch (e: Exception) { + Log.e("BridgeService", "Error listing translation files", e) + emptyList() + } + } } }