Skip to content

Commit

Permalink
i18n: More logging, register Bundle class-loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Dec 8, 2024
1 parent 0790584 commit 2a67b09
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
4 changes: 4 additions & 0 deletions i18n/src/main/kotlin/dev/kordex/i18n/Bundle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public data class Bundle(
@Transient
val classLoader: ClassLoader = ClassLoaderRegistry.getForBundle(name),
) {
init {
ClassLoaderRegistry.register(this)
}

public fun getResourceBundleControl(): ResourceBundle.Control =
FileFormatRegistry.getOrError(fileFormat)

Expand Down
15 changes: 8 additions & 7 deletions i18n/src/main/kotlin/dev/kordex/i18n/Translations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ public object Translations {
private val bundleLocaleCache: MutableMap<Pair<String, Locale>, ResourceBundle> = mutableMapOf()

/** Check whether the given [Key] exists. **/
public fun hasKey(key: Key): Boolean {
val (key, bundle, locale) = key
public fun hasKey(keyObj: Key): Boolean {
val (key, bundle, locale) = keyObj

return try {
val (bundle, _) = getBundles(
val (rootBundle, _) = getBundles(
bundle ?: I18n.defaultBundle,
locale ?: I18n.defaultLocale,
)

bundle.keySet().contains(key)
rootBundle.keySet().contains(key)
} catch (e: MissingResourceException) {
logger.trace(e) { "Failed to get $bundle for locale $locale" }
logger.trace(e) { "Failed to find $key" }

false
}
}

/** Get the untranslated string for the given [Key]. **/
public fun get(key: Key): String {
val (key, bundle, locale) = key
public fun get(keyObj: Key): String {
val (key, bundle, locale) = keyObj

val (baseBundle, overrideBundle) = getBundles(
bundle ?: I18n.defaultBundle,
Expand Down Expand Up @@ -144,6 +144,7 @@ public object Translations {
bundle.getResourceBundleControl()
)

@Suppress("DEPRECATION") // Locale constructor, for compatibility with Java versions older than 19.
private fun getBundles(bundle: Bundle, locale: Locale): Pair<ResourceBundle, ResourceBundle?> {
// First, let's make sure the bundle is valid. If these throw, it isn't valid.
bundle.getResourceBundleControl()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,31 @@
package dev.kordex.i18n.registries

import dev.kordex.i18n.Bundle
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging

public object ClassLoaderRegistry {
private val logger: KLogger = KotlinLogging.logger { }
private val bundleCache = mutableMapOf<String, ClassLoader>()

public fun getForBundle(bundle: String): ClassLoader =
bundleCache.getOrPut(bundle) { ClassLoader.getSystemClassLoader() }
bundleCache[bundle] ?: ClassLoader.getSystemClassLoader()

public fun register(bundle: String, classLoader: ClassLoader) {
bundleCache.put(bundle, classLoader)
}
public fun getForBundle(bundle: Bundle): ClassLoader =
getForBundle(bundle.name)

public fun register(bundle: String, classLoader: ClassLoader): Boolean {
if (bundle !in bundleCache) {
bundleCache[bundle] = classLoader

logger.trace { "Registered classloader $classLoader for bundle $bundle" }

public fun register(bundle: Bundle) {
bundleCache.put(bundle.name, bundle.classLoader)
return true
}

return false
}

public fun register(bundle: Bundle): Boolean =
register(bundle.name, bundle.classLoader)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ package dev.kordex.i18n.registries

import dev.akkinoc.util.YamlResourceBundle
import dev.kordex.i18n.files.PropertiesControl
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging
import java.util.ResourceBundle

public object FileFormatRegistry {
private val logger: KLogger = KotlinLogging.logger { }
private val formats: MutableMap<String, ResourceBundle.Control> = mutableMapOf()

init {
Expand All @@ -27,5 +30,7 @@ public object FileFormatRegistry {

public fun register(identifier: String, control: ResourceBundle.Control) {
formats[identifier] = control

logger.trace { "Registered file format \"$identifier\" to control $control" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ package dev.kordex.i18n.registries
import dev.kordex.i18n.messages.MessageFormat
import dev.kordex.i18n.messages.formats.ICUFormatV1
import dev.kordex.i18n.messages.formats.ICUFormatV2
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging

public object MessageFormatRegistry {
private val logger: KLogger = KotlinLogging.logger { }
private val formats: MutableMap<String, MessageFormat> = mutableMapOf()

init {
Expand All @@ -27,5 +30,7 @@ public object MessageFormatRegistry {

public fun register(format: MessageFormat) {
formats[format.identifier] = format

logger.trace { "Registered message format \"${format.identifier}\" - $format" }
}
}

0 comments on commit 2a67b09

Please sign in to comment.