Skip to content

Commit

Permalink
Standardise translation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Aug 31, 2024
1 parent 5ac9d59 commit c559b66
Show file tree
Hide file tree
Showing 23 changed files with 147 additions and 184 deletions.
4 changes: 1 addition & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#dokka will run out of memory with the default metaspace
org.gradle.jvmargs=-XX:MaxMetaspaceSize=1024m
org.gradle.parallel=true

kotlin.incremental=true
ksp.incremental=false
ksp.useKSP2=true

projectVersion=2.2.0-SNAPSHOT
projectVersion=2.3.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Section(public val name: String, public val description: String) :
}

public fun translate(key: String, locale: Locale, replacements: Array<Any?> = arrayOf()): String =
translations.translate(key, translationBundle, locale, replacements = replacements)
translations.translate(key = key, bundleName = translationBundle, locale = locale, replacements = replacements)

public fun validate() {
if (!::builder.isInitialized) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public open class CheckContext<out T : Event>(

replacements: Array<Any?> = arrayOf(),
): String =
translations.translate(key, locale, bundleName = bundle, replacements = replacements)
translations.translate(key = key, bundleName = bundle, locale = locale, replacements = replacements)

/** Quick access to translate strings using this check context's [locale]. **/
public fun translate(
Expand All @@ -180,7 +180,7 @@ public open class CheckContext<out T : Event>(

replacements: Array<Any?> = arrayOf(),
): String =
translations.translate(key, locale, bundleName = defaultBundle, replacements = replacements)
translations.translate(key = key, bundleName = defaultBundle, locale = locale, replacements = replacements)

/** Quick access to translate strings using this check context's [locale]. **/
public fun translate(
Expand All @@ -189,7 +189,7 @@ public open class CheckContext<out T : Event>(

replacements: Map<String, Any?>,
): String =
translations.translate(key, locale, bundleName = defaultBundle, replacements = replacements)
translations.translate(key = key, bundleName = defaultBundle, locale = locale, replacements = replacements)

/** Quick access to translate strings using this check context's [locale]. **/
public fun translate(
Expand All @@ -200,7 +200,7 @@ public open class CheckContext<out T : Event>(
bundle: String?,
replacements: Map<String, Any?>,
): String =
translations.translate(key, locale, bundleName = bundle, replacements = replacements)
translations.translate(key = key, bundleName = bundle, locale = locale, replacements = replacements)

/**
* If this check has failed and a message is set, throw a [DiscordRelayedException] with the translated message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ public data class Argument<T : Any?>(
}

internal fun Argument<*>.getDefaultTranslatedDisplayName(provider: TranslationsProvider, command: Command): String =
provider.translate(displayName, provider.defaultLocale, command.resolvedBundle ?: converter.bundle)
provider.translate(
key = displayName,
bundleName = command.resolvedBundle ?: converter.bundle,
locale = provider.defaultLocale
)
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ public abstract class CommandContext(
): String {
val locale = getLocale()

return translationsProvider.translate(key, locale, bundleName, replacements)
return translationsProvider.translate(
key = key,
bundleName = bundleName,
locale = locale,
replacements = replacements
)
}

public override suspend fun translate(
Expand All @@ -111,6 +116,11 @@ public abstract class CommandContext(
): String {
val locale = getLocale()

return translationsProvider.translate(key, locale, bundleName, replacements)
return translationsProvider.translate(
key = key,
bundleName = bundleName,
locale = locale,
replacements = replacements
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ public abstract class ApplicationCommand<E : InteractionCreateEvent>(
lowerCase: Boolean = false,
): Localized<String> {
var default = translationsProvider.translate(
key,
this.resolvedBundle,
translationsProvider.defaultLocale
key = key,
bundleName = this.resolvedBundle,
locale = translationsProvider.defaultLocale
)

if (lowerCase) {
Expand All @@ -133,9 +133,9 @@ public abstract class ApplicationCommand<E : InteractionCreateEvent>(
val translations = bot.settings.i18nBuilder.applicationCommandLocales
.associateWith { locale ->
val result = translationsProvider.translate(
key,
this.resolvedBundle,
locale.asJavaLocale()
key = key,
bundleName = this.resolvedBundle,
locale = locale.asJavaLocale()
)

if (lowerCase) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public class SlashGroup(

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ public open class ChatCommand<T : Arguments>(
if (!signatureCache.containsKey(locale)) {
if (signature != null) {
signatureCache[locale] = translationsProvider.translate(
signature!!,
resolvedBundle,
locale
key = signature!!,
bundleName = resolvedBundle,
locale = locale
)
} else {
signatureCache[locale] = registry.parser.signature(arguments!!, locale)
Expand All @@ -153,9 +153,9 @@ public open class ChatCommand<T : Arguments>(
public open fun getTranslatedName(locale: Locale): String {
if (!nameTranslationCache.containsKey(locale)) {
nameTranslationCache[locale] = translationsProvider.translate(
this.name,
this.resolvedBundle,
locale
key = this.name,
bundleName = this.resolvedBundle,
locale = locale
).lowercase()
}

Expand All @@ -166,15 +166,15 @@ public open class ChatCommand<T : Arguments>(
public open fun getTranslatedAliases(locale: Locale): Set<String> {
if (!aliasTranslationCache.containsKey(locale)) {
val translations = if (aliasKey != null) {
translationsProvider.translate(aliasKey!!, resolvedBundle, locale)
translationsProvider.translate(key = aliasKey!!, bundleName = resolvedBundle, locale = locale)
.lowercase()
.split(",")
.map { it.trim() }
.filter { it != EMPTY_VALUE_STRING }
.toSortedSet()
} else {
this.aliases.map {
translationsProvider.translate(it, resolvedBundle, locale).lowercase()
translationsProvider.translate(key = it, bundleName = resolvedBundle, locale = locale).lowercase()
}.toSortedSet()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,9 @@ public open class ChatCommandParser : KordExKoinComponent {

append(
translationsProvider.translate(
it.converter.signatureTypeString,
it.converter.bundle,
locale
key = it.converter.signatureTypeString,
bundleName = it.converter.bundle,
locale = locale
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public open class ChatSubCommand<T : Arguments>(
override fun getTranslatedName(locale: Locale): String {
if (!nameTranslationCache.containsKey(locale)) {
nameTranslationCache[locale] = translationsProvider.translate(
this.name,
this.resolvedBundle,
locale
key = this.name,
bundleName = this.resolvedBundle,
locale = locale
).lowercase()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ public class TagConverter(
if (channel == null) {
throw DiscordRelayedException(
translationsProvider.translate(
if (getter == null) {
key = if (getter == null) {
"converters.tag.error.wrongChannelType"
} else {
"converters.tag.error.wrongChannelTypeWithGetter"
},

event.getLocale(),
DEFAULT_KORDEX_BUNDLE
bundleName = DEFAULT_KORDEX_BUNDLE,
locale = event.getLocale(),
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ public abstract class ComponentContext<E : ComponentInteractionCreateEvent>(
): String {
val locale = getLocale()

return translationsProvider.translate(key, locale, bundleName, replacements)
return translationsProvider.translate(
key = key,
bundleName = bundleName,
locale = locale,
replacements = replacements
)
}

/**
Expand All @@ -161,7 +166,12 @@ public abstract class ComponentContext<E : ComponentInteractionCreateEvent>(
): String {
val locale = getLocale()

return translationsProvider.translate(key, locale, bundleName, replacements)
return translationsProvider.translate(
key = key,
bundleName = bundleName,
locale = locale,
replacements = replacements
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public abstract class ModalForm : Form(), KordExKoinComponent {

/** Return a translated modal title using the given locale, and the given bundle if the modal doesn't have one. **/
public fun translateTitle(locale: Locale, otherBundle: String?): String =
translations.translate(title, locale, bundle ?: otherBundle)
translations.translate(key = title, bundleName = bundle ?: otherBundle, locale = locale)

/**
* Convenience function to send this modal to the given [interaction] and await its completion, running the provided
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public abstract class TextInputWidget<T : TextInputWidget<T>> : Widget<String?>(
}

override suspend fun apply(builder: ActionRowBuilder, locale: Locale, bundle: String?) {
val translatedLabel = translations.translate(label, locale, bundle)
val translatedLabel = translations.translate(key = label, bundleName = bundle, locale = locale)

if (translatedLabel.length > LABEL_LENGTH) {
error(
Expand All @@ -123,12 +123,12 @@ public abstract class TextInputWidget<T : TextInputWidget<T>> : Widget<String?>(
this.required = this@TextInputWidget.required

this.placeholder = this@TextInputWidget.placeholder?.let {
translations.translate(it, locale, bundle)
translations.translate(key = it, bundleName = bundle, locale = locale)
}

this.value = this@TextInputWidget.initialValue?.let {
if (translateInitialValue) {
translations.translate(it, locale, bundle)
translations.translate(key = it, bundleName = bundle, locale = locale)
} else {
it
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ public open class EventContext<T : Event>(
): String {
val locale: Locale = getLocale()

return translationsProvider.translate(key, locale, bundleName, replacements)
return translationsProvider.translate(
key = key,
bundleName = bundleName,
locale = locale,
replacements = replacements
)
}

/**
Expand All @@ -70,7 +75,12 @@ public open class EventContext<T : Event>(
): String {
val locale: Locale = getLocale()

return translationsProvider.translate(key, locale, bundleName, replacements)
return translationsProvider.translate(
key = key,
bundleName = bundleName,
locale = locale,
replacements = replacements
)
}

override suspend fun getLocale(): Locale {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,12 @@ public open class EventHandler<T : Event>(
): String {
val locale = getLocale()

return translationsProvider.translate(key, locale, bundleName, replacements)
return translationsProvider.translate(
key = key,
bundleName = bundleName,
locale = locale,
replacements = replacements
)
}

/**
Expand All @@ -265,7 +270,12 @@ public open class EventHandler<T : Event>(
): String {
val locale = getLocale()

return translationsProvider.translate(key, locale, bundleName, replacements)
return translationsProvider.translate(
key = key,
bundleName = bundleName,
locale = locale,
replacements = replacements
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,20 @@ public class HelpExtension : HelpProvider, Extension() {

val description = buildString {
if (longDescription) {
append(translationsProvider.translate(command.description, command.extension.bundle, locale))
append(
translationsProvider.translate(
key = command.description,
bundleName = command.extension.bundle,
locale = locale
)
)
} else {
append(
translationsProvider.translate(command.description, command.extension.bundle, locale)
translationsProvider.translate(
key = command.description,
bundleName = command.extension.bundle,
locale = locale
)
.trim()
.takeWhile { it != '\n' }
)
Expand Down Expand Up @@ -383,17 +393,23 @@ public class HelpExtension : HelpProvider, Extension() {

append(
translationsProvider.translate(
it.converter.signatureTypeString,
it.converter.bundle,
locale
key = it.converter.signatureTypeString,
bundleName = it.converter.bundle,
locale = locale
)
)

append(")")
}

append("`: ")
append(translationsProvider.translate(it.description, command.extension.bundle, locale))
append(
translationsProvider.translate(
key = it.description,
bundleName = command.extension.bundle,
locale = locale
)
)
}
}
)
Expand Down
Loading

0 comments on commit c559b66

Please sign in to comment.