From 2a67b096d4f945ec2c7dc2eafc11b7b7660bd751 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Sun, 8 Dec 2024 17:56:55 +0000 Subject: [PATCH] i18n: More logging, register Bundle class-loaders --- .../src/main/kotlin/dev/kordex/i18n/Bundle.kt | 4 +++ .../kotlin/dev/kordex/i18n/Translations.kt | 15 +++++------ .../i18n/registries/ClassLoaderRegistry.kt | 25 ++++++++++++++----- .../i18n/registries/FileFormatRegistry.kt | 5 ++++ .../i18n/registries/MessageFormatRegistry.kt | 5 ++++ 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/i18n/src/main/kotlin/dev/kordex/i18n/Bundle.kt b/i18n/src/main/kotlin/dev/kordex/i18n/Bundle.kt index 5dc80b3..36c2f80 100644 --- a/i18n/src/main/kotlin/dev/kordex/i18n/Bundle.kt +++ b/i18n/src/main/kotlin/dev/kordex/i18n/Bundle.kt @@ -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) diff --git a/i18n/src/main/kotlin/dev/kordex/i18n/Translations.kt b/i18n/src/main/kotlin/dev/kordex/i18n/Translations.kt index 270c7e9..aa7e90a 100644 --- a/i18n/src/main/kotlin/dev/kordex/i18n/Translations.kt +++ b/i18n/src/main/kotlin/dev/kordex/i18n/Translations.kt @@ -17,26 +17,26 @@ public object Translations { private val bundleLocaleCache: MutableMap, 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, @@ -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 { // First, let's make sure the bundle is valid. If these throw, it isn't valid. bundle.getResourceBundleControl() diff --git a/i18n/src/main/kotlin/dev/kordex/i18n/registries/ClassLoaderRegistry.kt b/i18n/src/main/kotlin/dev/kordex/i18n/registries/ClassLoaderRegistry.kt index 356de9e..3df39f5 100644 --- a/i18n/src/main/kotlin/dev/kordex/i18n/registries/ClassLoaderRegistry.kt +++ b/i18n/src/main/kotlin/dev/kordex/i18n/registries/ClassLoaderRegistry.kt @@ -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() 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) } diff --git a/i18n/src/main/kotlin/dev/kordex/i18n/registries/FileFormatRegistry.kt b/i18n/src/main/kotlin/dev/kordex/i18n/registries/FileFormatRegistry.kt index 83ad2f3..42e7c45 100644 --- a/i18n/src/main/kotlin/dev/kordex/i18n/registries/FileFormatRegistry.kt +++ b/i18n/src/main/kotlin/dev/kordex/i18n/registries/FileFormatRegistry.kt @@ -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 = mutableMapOf() init { @@ -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" } } } diff --git a/i18n/src/main/kotlin/dev/kordex/i18n/registries/MessageFormatRegistry.kt b/i18n/src/main/kotlin/dev/kordex/i18n/registries/MessageFormatRegistry.kt index 870f8ec..9c7771a 100644 --- a/i18n/src/main/kotlin/dev/kordex/i18n/registries/MessageFormatRegistry.kt +++ b/i18n/src/main/kotlin/dev/kordex/i18n/registries/MessageFormatRegistry.kt @@ -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 = mutableMapOf() init { @@ -27,5 +30,7 @@ public object MessageFormatRegistry { public fun register(format: MessageFormat) { formats[format.identifier] = format + + logger.trace { "Registered message format \"${format.identifier}\" - $format" } } }