Skip to content

Commit

Permalink
Continued work on new translations system
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Sep 14, 2024
1 parent 2fc36bb commit 50dc76a
Show file tree
Hide file tree
Showing 20 changed files with 146 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import dev.kordex.core.commands.chat.ChatCommand
import dev.kordex.core.commands.converters.builders.ConverterBuilder
import dev.kordex.core.events.EventHandler
import dev.kordex.core.extensions.Extension
import dev.kordex.core.i18n.toKey
import dev.kordex.core.i18n.types.Bundle
import dev.kordex.core.i18n.types.Key
import dev.kordex.parser.StringParser
Expand Down Expand Up @@ -93,7 +92,7 @@ public class EventHandlerRegistrationException(public val reason: String) : Kord
* @param name The command name
* @param reason Why this command is considered invalid.
*/
public class InvalidCommandException(public val name: String?, public val reason: String) : KordExException() {
public class InvalidCommandException(public val name: Key?, public val reason: String) : KordExException() {
override val message: String = toString()

override fun toString(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import dev.kordex.core.builders.ExtensibleBotBuilder
import dev.kordex.core.commands.events.CommandEvent
import dev.kordex.core.extensions.Extension
import dev.kordex.core.i18n.TranslationsProvider
import dev.kordex.core.i18n.generated.CoreTranslations
import dev.kordex.core.i18n.types.Bundle
import dev.kordex.core.i18n.types.Key
import dev.kordex.core.koin.KordExKoinComponent
Expand Down Expand Up @@ -88,7 +89,7 @@ public abstract class Command(public val extension: Extension) : Lockable, KordE
*/
@Throws(InvalidCommandException::class)
public open fun validate() {
if (!::name.isInitialized || name.isEmpty()) {
if (!::name.isInitialized || name.key.isEmpty()) {
throw InvalidCommandException(null, "No command name given.")
}

Expand Down Expand Up @@ -120,16 +121,13 @@ public abstract class Command(public val extension: Extension) : Lockable, KordE

if (missingPerms.isNotEmpty()) {
throw DiscordRelayedException(
context.translate(
"commands.error.missingBotPermissions",
null,

replacements = arrayOf(
CoreTranslations.Commands.Error.missingBotPermissions
.withLocale(context.getLocale())
.translate(
missingPerms
.map { it.translate(context.getLocale()) }
.joinToString()
)
)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import dev.kordex.core.checks.interactionFor
import dev.kordex.core.checks.userFor
import dev.kordex.core.i18n.TranslationsProvider
import dev.kordex.core.i18n.types.Bundle
import dev.kordex.core.i18n.types.Key
import dev.kordex.core.koin.KordExKoinComponent
import dev.kordex.core.sentry.SentryContext
import dev.kordex.core.types.TranslatableContext
Expand All @@ -41,7 +42,7 @@ import java.util.*
public abstract class CommandContext(
public open val command: Command,
public open val eventObj: Event,
public open val commandName: String,
public open val commandName: Key,
public open val cache: MutableStringKeyedMap<Any>,
) : KordExKoinComponent, TranslatableContext {
/** Translations provider, for retrieving translations. **/
Expand Down Expand Up @@ -96,32 +97,26 @@ public abstract class CommandContext(
}

public override suspend fun translate(
key: String,
bundleName: String?,
key: Key,
bundle: Bundle?,
replacements: Array<Any?>,
): String {
val locale = getLocale()

return translationsProvider.translate(
key = key,
bundleName = bundleName,
locale = locale,
replacements = replacements
)
return key.withBundle(bundle)
.withLocale(locale)
.translateArray(replacements)
}

public override suspend fun translate(
key: String,
bundleName: String?,
key: Key,
bundle: Bundle?,
replacements: Map<String, Any?>,
): String {
val locale = getLocale()

return translationsProvider.translate(
key = key,
bundleName = bundleName,
locale = locale,
replacements = replacements
)
return key.withBundle(bundle)
.withLocale(locale)
.translateNamed(replacements)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import dev.kordex.core.checks.types.CheckWithCache
import dev.kordex.core.commands.Command
import dev.kordex.core.commands.application.slash.SlashCommand
import dev.kordex.core.extensions.Extension
import dev.kordex.core.i18n.types.Key
import dev.kordex.core.koin.KordExKoinComponent
import dev.kordex.core.utils.MutableStringKeyedMap
import dev.kordex.core.utils.getLocale
Expand Down Expand Up @@ -117,26 +118,24 @@ public abstract class ApplicationCommand<E : InteractionCreateEvent>(
* @param lowerCase Provide `true` to lower-case all the translations. Discord requires this for some fields.
*/
public fun localize(
key: String,
key: Key,
lowerCase: Boolean = false,
): Localized<String> {
var default = translationsProvider.translate(
key = key,
bundleName = this.resolvedBundle,
locale = translationsProvider.defaultLocale
)
val bundledKey = key.withBundle(resolvedBundle)

var default = bundledKey
.withLocale(translationsProvider.defaultLocale)
.translate()

if (lowerCase) {
default = default.lowercase(translationsProvider.defaultLocale)
}

val translations = bot.settings.i18nBuilder.applicationCommandLocales
.associateWith { locale ->
val result = translationsProvider.translate(
key = key,
bundleName = this.resolvedBundle,
locale = locale.asJavaLocale()
)
val result = bundledKey
.withLocale(locale.asJavaLocale())
.translate()

if (lowerCase) {
result.lowercase(locale.asJavaLocale())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import dev.kord.core.entity.interaction.GuildAutoCompleteInteraction
import dev.kord.core.event.interaction.AutoCompleteInteractionCreateEvent
import dev.kordex.core.commands.Command
import dev.kordex.core.commands.CommandContext
import dev.kordex.core.i18n.types.Key

public class DummyAutocompleteCommandContext(
command: Command,
private val event: AutoCompleteInteractionCreateEvent,
commandName: String,
commandName: Key,
) : CommandContext(command, event, commandName, mutableMapOf()) {
override suspend fun populate() {
error("This should never be called.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import dev.kordex.core.components.ComponentRegistry
import dev.kordex.core.components.forms.ModalForm
import dev.kordex.core.extensions.Extension
import dev.kordex.core.extensions.impl.SENTRY_EXTENSION_NAME
import dev.kordex.core.i18n.generated.CoreTranslations
import dev.kordex.core.sentry.BreadcrumbType
import dev.kordex.core.types.FailureReason
import dev.kordex.core.utils.MutableStringKeyedMap
Expand Down Expand Up @@ -139,19 +140,29 @@ public abstract class MessageCommand<C : MessageCommandContext<C, M>, M : ModalF
logger.info { "Error submitted to Sentry: $sentryId" }

if (extension.bot.extensions.containsKey(SENTRY_EXTENSION_NAME)) {
context.translate("commands.error.user.sentry.slash", null, replacements = arrayOf(sentryId))
CoreTranslations.Commands.Error.User.Sentry.slash
.withLocale(context.getLocale())
.translate(sentryId)
} else {
context.translate("commands.error.user", null)
CoreTranslations.Commands.Error.user
.withLocale(context.getLocale())
.translate()
}
} else {
context.translate("commands.error.user", null)
CoreTranslations.Commands.Error.user
.withLocale(context.getLocale())
.translate()
}

respondText(context, errorMessage, FailureReason.ExecutionError(t))
} else {
respondText(
context,
context.translate("commands.error.user", null),

CoreTranslations.Commands.Error.user
.withLocale(context.getLocale())
.translate(),

FailureReason.ExecutionError(t)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import dev.kordex.core.components.ComponentRegistry
import dev.kordex.core.components.forms.ModalForm
import dev.kordex.core.extensions.Extension
import dev.kordex.core.extensions.impl.SENTRY_EXTENSION_NAME
import dev.kordex.core.i18n.generated.CoreTranslations
import dev.kordex.core.i18n.types.Key
import dev.kordex.core.sentry.BreadcrumbType
import dev.kordex.core.types.FailureReason
import dev.kordex.core.utils.MutableStringKeyedMap
Expand Down Expand Up @@ -55,7 +57,7 @@ public abstract class SlashCommand<C : SlashCommandContext<*, A, M>, A : Argumen
public val componentRegistry: ComponentRegistry by inject()

/** Command description, as displayed on Discord. **/
public open lateinit var description: String
public open lateinit var description: Key

/** Command body, to be called when the command is executed. **/
public lateinit var body: suspend C.(M?) -> Unit
Expand Down Expand Up @@ -321,19 +323,29 @@ public abstract class SlashCommand<C : SlashCommandContext<*, A, M>, A : Argumen
kxLogger.info { "Error submitted to Sentry: $sentryId" }

if (extension.bot.extensions.containsKey(SENTRY_EXTENSION_NAME)) {
context.translate("commands.error.user.sentry.slash", null, replacements = arrayOf(sentryId))
CoreTranslations.Commands.Error.User.Sentry.slash
.withLocale(context.getLocale())
.translate(sentryId)
} else {
context.translate("commands.error.user", null)
CoreTranslations.Commands.Error.user
.withLocale(context.getLocale())
.translate()
}
} else {
context.translate("commands.error.user", null)
CoreTranslations.Commands.Error.user
.withLocale(context.getLocale())
.translate()
}

respondText(context, errorMessage, FailureReason.ExecutionError(t))
} else {
respondText(
context,
context.translate("commands.error.user", null),

CoreTranslations.Commands.Error.user
.withLocale(context.getLocale())
.translate(),

FailureReason.ExecutionError(t)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package dev.kordex.core.commands.application.slash
import dev.kordex.core.InvalidCommandException
import dev.kordex.core.commands.application.Localized
import dev.kordex.core.i18n.TranslationsProvider
import dev.kordex.core.i18n.types.Key
import dev.kordex.core.koin.KordExKoinComponent
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging
Expand All @@ -24,7 +25,7 @@ import java.util.*
* @param parent Parent slash command that this group belongs to
*/
public class SlashGroup(
public val name: String,
public val name: Key,
public val parent: SlashCommand<*, *, *>,
) : KordExKoinComponent {
/** Translations provider, for retrieving translations. **/
Expand All @@ -37,7 +38,7 @@ public class SlashGroup(
public val subCommands: MutableList<SlashCommand<*, *, *>> = mutableListOf()

/** Command group description, which is required and shown on Discord. **/
public lateinit var description: String
public lateinit var description: Key

/**
* A [Localized] version of [name].
Expand Down Expand Up @@ -66,11 +67,11 @@ public class SlashGroup(
// Only slash commands need this to be lower-cased.

if (!descriptionTranslationCache.containsKey(locale)) {
descriptionTranslationCache[locale] = translationsProvider.translate(
key = this.description,
bundleName = this.parent.resolvedBundle,
locale = locale
).lowercase()
descriptionTranslationCache[locale] = description
.withBundle(this.parent.resolvedBundle)
.withLocale(locale)
.translate()
.lowercase(locale)
}

return descriptionTranslationCache[locale]!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import dev.kordex.core.InvalidCommandException
import dev.kordex.core.annotations.ExtensionDSL
import dev.kordex.core.commands.Arguments
import dev.kordex.core.components.forms.ModalForm
import dev.kordex.core.i18n.types.Key

private const val SUBCOMMAND_AND_GROUP_LIMIT: Int = 25

Expand All @@ -30,7 +31,7 @@ private const val SUBCOMMAND_AND_GROUP_LIMIT: Int = 25
* @param body Lambda used to build the [SlashGroup] object.
*/
@ExtensionDSL
public suspend fun SlashCommand<*, *, *>.group(name: String, body: suspend SlashGroup.() -> Unit): SlashGroup {
public suspend fun SlashCommand<*, *, *>.group(name: Key, body: suspend SlashGroup.() -> Unit): SlashGroup {
if (parentCommand != null) {
error("Command groups may not be nested inside subcommands.")
}
Expand Down
Loading

0 comments on commit 50dc76a

Please sign in to comment.