Skip to content

Commit

Permalink
GrindrPlus: Methods to support multi languages
Browse files Browse the repository at this point in the history
  • Loading branch information
R0rt1z2 committed Sep 18, 2024
1 parent 4079b76 commit d33dd33
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package com.grindrplus.bridge;

interface IBridgeService {
String getTranslation(String locale);
List<String> getAvailableTranslations();
}
20 changes: 18 additions & 2 deletions app/src/main/java/com/grindrplus/GrindrPlus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ object GrindrPlus {

lateinit var hookManager: HookManager
lateinit var translations: JSONObject
lateinit var localeTag: String

var currentActivity: Activity? = null
private set
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -148,7 +149,22 @@ object GrindrPlus {
return classLoader.loadClass(name)
}

fun getTranslation(key: String, vararg placeholders: Pair<String, String>): 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()
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/com/grindrplus/bridge/BridgeClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
if (!isBound) {
GrindrPlus.logger.log("Cannot get available translations, service is not bound!")
return emptyList()
}

return bridgeService?.getAvailableTranslations() ?: emptyList()
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/com/grindrplus/bridge/BridgeService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,15 @@ class BridgeService : Service() {
"{\"error\": \"Translation file not found or failed to load\"}"
}
}

@Throws(RemoteException::class)
override fun getAvailableTranslations(): List<String> {
return try {
assets.list("translations")?.map { it.removeSuffix(".json") } ?: emptyList()
} catch (e: Exception) {
Log.e("BridgeService", "Error listing translation files", e)
emptyList()
}
}
}
}

0 comments on commit d33dd33

Please sign in to comment.