From 92fb40e351f221803fce1e4c74b085f21940c811 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Sat, 31 Aug 2024 13:06:48 +0100 Subject: [PATCH] [i18n] Search plugins for bundles too --- .../core/i18n/ResourceBundleTranslations.kt | 39 ++++++++++++++++++- .../kordex/core/i18n/TranslationsProvider.kt | 3 +- .../test/bot/extensions/ModalTestExtension.kt | 2 +- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/kord-extensions/src/main/kotlin/dev/kordex/core/i18n/ResourceBundleTranslations.kt b/kord-extensions/src/main/kotlin/dev/kordex/core/i18n/ResourceBundleTranslations.kt index 8b3746a168..c645ff2ac2 100644 --- a/kord-extensions/src/main/kotlin/dev/kordex/core/i18n/ResourceBundleTranslations.kt +++ b/kord-extensions/src/main/kotlin/dev/kordex/core/i18n/ResourceBundleTranslations.kt @@ -11,6 +11,7 @@ package dev.kordex.core.i18n import com.ibm.icu.text.MessageFormat import dev.kordex.core.builders.ExtensibleBotBuilder import dev.kordex.core.koin.KordExKoinComponent +import dev.kordex.core.plugins.PluginManager import io.github.oshai.kotlinlogging.KLogger import io.github.oshai.kotlinlogging.KotlinLogging import org.koin.core.component.inject @@ -33,6 +34,9 @@ public open class ResourceBundleTranslations( defaultLocaleBuilder: () -> Locale, ) : TranslationsProvider(defaultLocaleBuilder) { private val logger: KLogger = KotlinLogging.logger { } + + private val pluginManager: PluginManager by inject() + private val bundles: MutableMap, ResourceBundle> = mutableMapOf() private val overrideBundles: MutableMap, ResourceBundle> = mutableMapOf() @@ -58,7 +62,40 @@ public open class ResourceBundleTranslations( bundle: String, locale: Locale, control: ResourceBundle.Control, - ): ResourceBundle = ResourceBundle.getBundle(bundle, locale, control) + ): ResourceBundle { + val defaultBundlePath = bundle.replace(".", "/") + ".properties" + + val classLoaders: MutableList> = mutableListOf( + "default class-loader" to ResourceBundleTranslations::class.java.classLoader, + "system class-loader" to ClassLoader.getSystemClassLoader(), + ) + + if (pluginManager.enabled) { + pluginManager.plugins.forEach { plugin -> + classLoaders.add(plugin.pluginId to plugin.pluginClassLoader) + } + } + + classLoaders.forEach { (plugin, loader) -> + logger.trace { "Trying to find $bundle with ${locale.toLanguageTag()} in $plugin" } + + try { + if (loader.getResource(defaultBundlePath) != null) { + val result = ResourceBundle.getBundle(bundle, locale, loader, control) + + logger.debug { "Found bundle $bundle with ${locale.toLanguageTag()} in $plugin" } + + return result + } + } catch (_: MissingResourceException) { + null // Do nothing, we expect this to happen. + } + } + + logger.debug { "Couldn't find bundle $bundle with ${locale.toLanguageTag()}; falling back to default strategy" } + + return ResourceBundle.getBundle(bundle, locale, control) + } /** * Retrieves a pair of the [ResourceBundle] and the override resource bundle for [bundleName] in locale. diff --git a/kord-extensions/src/main/kotlin/dev/kordex/core/i18n/TranslationsProvider.kt b/kord-extensions/src/main/kotlin/dev/kordex/core/i18n/TranslationsProvider.kt index 5e7a3188ff..932dce1924 100644 --- a/kord-extensions/src/main/kotlin/dev/kordex/core/i18n/TranslationsProvider.kt +++ b/kord-extensions/src/main/kotlin/dev/kordex/core/i18n/TranslationsProvider.kt @@ -8,6 +8,7 @@ package dev.kordex.core.i18n +import dev.kordex.core.koin.KordExKoinComponent import java.util.* /** @@ -18,7 +19,7 @@ import java.util.* */ public abstract class TranslationsProvider( public open val defaultLocaleBuilder: () -> Locale, -) { +) : KordExKoinComponent { /** * Default locale, resolved via [defaultLocaleBuilder]. Avoid accessing this outside of your [get] functions, as * accessing it too early will prevent the user from configuring it properly. diff --git a/test-bot/src/main/kotlin/dev/kordex/test/bot/extensions/ModalTestExtension.kt b/test-bot/src/main/kotlin/dev/kordex/test/bot/extensions/ModalTestExtension.kt index 910d50d675..0dfd73be15 100644 --- a/test-bot/src/main/kotlin/dev/kordex/test/bot/extensions/ModalTestExtension.kt +++ b/test-bot/src/main/kotlin/dev/kordex/test/bot/extensions/ModalTestExtension.kt @@ -23,7 +23,7 @@ import dev.kordex.core.extensions.publicUserCommand public class ModalTestExtension : Extension() { override val name: String = "kordex.modals" - override val bundle: String = "test-strings" + override val bundle: String = "test.strings" @Suppress("StringLiteralDuplication") override suspend fun setup() {