mattinger
06/03/2025, 1:52 PMfun localizableString(
stringTable: StringTable,
name: String,
bundle: NSBundle
)
But i have yet to figure out how to determine the current app's locale, or even get the locale in the bundle instance. There's supposedly functions in NSLocale for that (NSLocale.currentLocale
) but this function seems to not be showing up in kotlin. Also, bundle.preferredLocalizations returns an Any type, and i can't really figure out what the real type should be.Pavlos
06/03/2025, 2:35 PMPavlos
06/03/2025, 2:36 PMenum class StringKey {
APP_NAME,
APP_DESCRIPTION,
ACTION_RETRY,
ACTION_ALLOW,
ACTION_DENY,
ACTION_CANCEL,
ACTION_CONFIRM,
ACTION_GO_BACK,
...
...
}
enum class SupportedLocale { EN, TR, PT_BR }
object Strings {
private var defaultLocale = SupportedLocale.EN
private var currentLocale = mutableStateOf(SupportedLocale.EN)
private val strings: Map<SupportedLocale, Map<StringKey, String>> = mapOf(
SupportedLocale.EN to stringsEN,
<http://SupportedLocale.TR|SupportedLocale.TR> to stringsTR,
SupportedLocale.PT_BR to stringsPT_BR,
)
fun getLocale() = currentLocale.value
fun setLocale(locale: String, region: String? = null) {
currentLocale.value = if (region != null) {
val localeAndRegion = locale + "_" + region
SupportedLocale.entries.find { it.name == localeAndRegion.uppercase() } ?: defaultLocale
} else {
SupportedLocale.entries.find { it.name == locale.uppercase() } ?: defaultLocale
}
}
fun get(key: StringKey): String {
return strings[currentLocale.value]?.get(key)
?: strings[defaultLocale]?.get(key)
?: key.name
}
}
Pavlos
06/03/2025, 2:36 PMval stringsEN = mapOf(
StringKey.APP_NAME to "Tracker - Manager for Bluesky",
StringKey.APP_DESCRIPTION to "Track your Bluesky followers, unfollowers, blocks, and posts in real-time — with post analytics and advanced filters to find inactive or stat-matching accounts.",
StringKey.ACTION_RETRY to "Retry",
StringKey.ACTION_ALLOW to "Allow",
StringKey.ACTION_DENY to "Deny",
...
...
)
Pavlos
06/03/2025, 2:37 PMprivate fun initLanguage() {
val savedLanguage = cache.getLanguage()
if (savedLanguage.isEmpty()) {
val region = if (Locale.current.region == "BR") {
Locale.current.region
} else {
null
}
Strings.setLocale(Locale.current.language, region)
} else {
Strings.setLocale(savedLanguage)
}
}
Pavlos
06/03/2025, 2:37 PMmattinger
06/03/2025, 3:00 PMmattinger
06/03/2025, 3:01 PMgildor
06/04/2025, 4:57 AMfun Strings.retry() = stringTable["retry"] ?: fallback()
David Nedrow
06/05/2025, 5:22 PMgildor
06/06/2025, 3:11 AMgildor
06/06/2025, 3:12 AM