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 7c48b82 commit 2fc36bb
Show file tree
Hide file tree
Showing 18 changed files with 511 additions and 719 deletions.
11 changes: 7 additions & 4 deletions kord-extensions/src/main/kotlin/dev/kordex/core/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ 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
import java.util.*
import kotlin.reflect.KClass
Expand Down Expand Up @@ -124,9 +127,9 @@ public class CommandRegistrationException(public val name: String, public val re
*/
public open class DiscordRelayedException(
public open val reason: String,
public open val translationKey: String? = null,
public open val translationKey: Key? = null,
) : KordExException() {
override val message: String by lazy { toString() }
override val message: String by lazy { reason }

public constructor(other: DiscordRelayedException) : this(other.reason)

Expand All @@ -144,9 +147,9 @@ public open class DiscordRelayedException(
*/
public open class ArgumentParsingException(
public override val reason: String,
public override val translationKey: String?,
public override val translationKey: Key?,
public val locale: Locale,
public val bundle: String?,
public val bundle: Bundle?,
public val argument: Argument<*>?,
public val arguments: Arguments,
public val parser: StringParser?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ package dev.kordex.core.commands

import dev.kordex.core.annotations.UnexpectedFunctionBehaviour
import dev.kordex.core.commands.converters.*
import dev.kordex.core.i18n.types.Key
import java.util.Locale

/**
* Abstract base class for a class containing a set of command arguments.
Expand Down Expand Up @@ -47,8 +49,8 @@ public open class Arguments {
*/
@UnexpectedFunctionBehaviour
public open fun <R : Any> arg(
displayName: String,
description: String,
displayName: Key,
description: Key,
converter: SingleConverter<R>,
): SingleConverter<R> {
args.add(Argument(displayName, description, converter))
Expand All @@ -69,8 +71,8 @@ public open class Arguments {
*/
@UnexpectedFunctionBehaviour
public open fun <R : Any> arg(
displayName: String,
description: String,
displayName: Key,
description: Key,
converter: DefaultingConverter<R>,
): DefaultingConverter<R> {
args.add(Argument(displayName, description, converter))
Expand All @@ -91,8 +93,8 @@ public open class Arguments {
*/
@UnexpectedFunctionBehaviour
public open fun <R : Any> arg(
displayName: String,
description: String,
displayName: Key,
description: Key,
converter: OptionalConverter<R>,
): OptionalConverter<R> {
args.add(Argument(displayName, description, converter))
Expand All @@ -113,8 +115,8 @@ public open class Arguments {
*/
@UnexpectedFunctionBehaviour
public open fun <R : Any> arg(
displayName: String,
description: String,
displayName: Key,
description: Key,
converter: ListConverter<R>,
): ListConverter<R> {
args.add(Argument(displayName, description, converter))
Expand All @@ -135,8 +137,8 @@ public open class Arguments {
*/
@UnexpectedFunctionBehaviour
public open fun <R : Any> arg(
displayName: String,
description: String,
displayName: Key,
description: Key,
converter: CoalescingConverter<R>,
): CoalescingConverter<R> {
args.add(Argument(displayName, description, converter))
Expand All @@ -157,8 +159,8 @@ public open class Arguments {
*/
@UnexpectedFunctionBehaviour
public open fun <R : Any> arg(
displayName: String,
description: String,
displayName: Key,
description: Key,
converter: DefaultingCoalescingConverter<R>,
): DefaultingCoalescingConverter<R> {
args.add(Argument(displayName, description, converter))
Expand All @@ -179,8 +181,8 @@ public open class Arguments {
*/
@UnexpectedFunctionBehaviour
public open fun <R : Any> arg(
displayName: String,
description: String,
displayName: Key,
description: Key,
converter: OptionalCoalescingConverter<R>,
): OptionalCoalescingConverter<R> {
args.add(Argument(displayName, description, converter))
Expand All @@ -189,14 +191,14 @@ public open class Arguments {
}

/** Validation function that will throw an error if there's a problem with this Arguments class/subclass. **/
public open fun validate() {
public open fun validate(locale: Locale) {
val names: MutableSet<String> = mutableSetOf()

args.forEach {
val name = it.displayName.lowercase()
val name = it.displayName.translateLocale(locale).lowercase()

if (name in names) {
error("Duplicate argument name: ${it.displayName}")
error("Duplicate argument name/key: ${it.displayName}")
}

names.add(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import dev.kordex.core.checks.guildFor
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.koin.KordExKoinComponent
import dev.kordex.core.sentry.SentryContext
import dev.kordex.core.types.TranslatableContext
Expand Down Expand Up @@ -51,7 +52,7 @@ public abstract class CommandContext(

public override var resolvedLocale: Locale? = null

override val bundle: String?
override val bundle: Bundle?
get() = command.resolvedBundle

/** Called before processing, used to populate any extra variables from event data. **/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public open class SlashCommandParser {
context: SlashCommandContext<*, *, *>,
): T {
val argumentsObj = builder.invoke()
argumentsObj.validate()
argumentsObj.validate(context.getLocale())

logger.trace { "Arguments object: $argumentsObj (${argumentsObj.args.size} args)" }

Expand Down
Loading

0 comments on commit 2fc36bb

Please sign in to comment.