Skip to content

Commit

Permalink
Put withContext in the right package
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Sep 26, 2024
1 parent 9312259 commit a48dc89
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import dev.kordex.core.commands.Arguments
import dev.kordex.core.commands.converters.*
import dev.kordex.core.commands.getDefaultTranslatedDisplayName
import dev.kordex.core.i18n.generated.CoreTranslations
import dev.kordex.core.utils.withContext
import dev.kordex.core.i18n.withContext
import io.github.oshai.kotlinlogging.KotlinLogging

private val logger = KotlinLogging.logger {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import dev.kordex.core.commands.converters.types.MultiNamedInputConverter
import dev.kordex.core.commands.converters.types.SingleNamedInputConverter
import dev.kordex.core.commands.getDefaultTranslatedDisplayName
import dev.kordex.core.i18n.generated.CoreTranslations
import dev.kordex.core.i18n.withContext
import dev.kordex.core.koin.KordExKoinComponent
import dev.kordex.core.utils.MutableStringKeyedMap
import dev.kordex.core.utils.withContext
import dev.kordex.parser.StringParser
import io.github.oshai.kotlinlogging.KotlinLogging
import org.koin.core.component.inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import dev.kordex.core.commands.CommandContext
import dev.kordex.core.commands.converters.builders.ConverterBuilder
import dev.kordex.core.commands.converters.builders.ValidationContext
import dev.kordex.core.i18n.types.Key
import dev.kordex.core.i18n.withContext
import dev.kordex.core.koin.KordExKoinComponent
import dev.kordex.core.utils.withContext
import dev.kordex.parser.StringParser
import org.koin.core.component.inject
import kotlin.reflect.KProperty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package dev.kordex.core.i18n
import dev.kordex.core.i18n.types.Bundle
import dev.kordex.core.i18n.types.Key
import dev.kordex.core.i18n.types.PlaceholderPosition
import dev.kordex.core.types.TranslatableContext
import java.util.Locale

private val translationKeyMap: MutableMap<String, Key> = mutableMapOf()
Expand Down Expand Up @@ -49,3 +50,6 @@ public fun String.toKey(bundle: Bundle): Key =

public fun String.toKey(bundle: String): Key =
toKey(Bundle(bundle))

public suspend fun Key.withContext(context: TranslatableContext) =
withLocale(context.getLocale())
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public data class Key(
@Serializable(with = LocaleSerializer::class)
public val locale: Locale? = null,

public val presetPlaceholderPosition: PlaceholderPosition = PlaceholderPosition.START,
public val presetPlaceholderPosition: PlaceholderPosition = PlaceholderPosition.FIRST,
public val translateNestedKeys: Boolean = true,

@Transient
Expand Down Expand Up @@ -58,6 +58,9 @@ public data class Key(
public fun withNamedPlaceholders(vararg placeholders: Pair<String, Any?>): Key =
copy(namedPlaceholders = namedPlaceholders + placeholders)

public fun withNamedPlaceholders(placeholders: Map<String, Any?>): Key =
copy(namedPlaceholders = placeholders + placeholders)

public fun withPresetPlaceholderPosition(position: PlaceholderPosition): Key =
copy(presetPlaceholderPosition = position)

Expand Down Expand Up @@ -131,8 +134,8 @@ public data class Key(

public fun translateArray(replacements: Array<Any?>): String {
val allReplacements = when (presetPlaceholderPosition) {
PlaceholderPosition.START -> ordinalPlaceholders + replacements
PlaceholderPosition.END -> replacements.asList() + ordinalPlaceholders
PlaceholderPosition.FIRST -> ordinalPlaceholders + replacements
PlaceholderPosition.LAST -> replacements.asList() + ordinalPlaceholders
}.map {
if (translateNestedKeys && it is Key) {
it
Expand All @@ -149,8 +152,8 @@ public data class Key(

public fun translateNamed(replacements: Map<String, Any?>): String {
val allReplacements = when (presetPlaceholderPosition) {
PlaceholderPosition.START -> namedPlaceholders + replacements
PlaceholderPosition.END -> replacements + namedPlaceholders
PlaceholderPosition.FIRST -> namedPlaceholders + replacements
PlaceholderPosition.LAST -> replacements + namedPlaceholders
}.mapValues {
if (translateNestedKeys && it.value is Key) {
(it.value as Key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ import kotlinx.serialization.Serializable

@Serializable
public enum class PlaceholderPosition {
START,
END
FIRST,
LAST
}
15 changes: 0 additions & 15 deletions kord-extensions/src/main/kotlin/dev/kordex/core/utils/_i18n.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import dev.kordex.core.commands.wrapOption
import dev.kordex.core.i18n.EMPTY_VALUE_STRING
import dev.kordex.core.i18n.generated.CoreTranslations
import dev.kordex.core.i18n.types.Key
import dev.kordex.core.i18n.withContext
import dev.kordex.core.parsers.DurationParserException
import dev.kordex.core.parsers.InvalidTimeUnitException
import dev.kordex.core.utils.withContext
import dev.kordex.parser.StringParser
import dev.kordex.parser.tokens.PositionalArgumentToken
import io.github.oshai.kotlinlogging.KLogger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import dev.kordex.core.commands.converters.Validator
import dev.kordex.core.commands.wrapOption
import dev.kordex.core.i18n.generated.CoreTranslations
import dev.kordex.core.i18n.types.Key
import dev.kordex.core.i18n.withContext
import dev.kordex.core.parsers.DurationParserException
import dev.kordex.core.parsers.InvalidTimeUnitException
import dev.kordex.core.utils.withContext
import dev.kordex.parser.StringParser
import java.time.Duration
import java.time.LocalDateTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import dev.kordex.core.commands.wrapOption
import dev.kordex.core.i18n.EMPTY_VALUE_STRING
import dev.kordex.core.i18n.generated.CoreTranslations
import dev.kordex.core.i18n.types.Key
import dev.kordex.core.i18n.withContext
import dev.kordex.core.parsers.DurationParserException
import dev.kordex.core.parsers.InvalidTimeUnitException
import dev.kordex.core.utils.withContext
import dev.kordex.parser.StringParser
import dev.kordex.parser.tokens.PositionalArgumentToken
import io.github.oshai.kotlinlogging.KLogger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import dev.kordex.core.commands.converters.Validator
import dev.kordex.core.commands.wrapOption
import dev.kordex.core.i18n.generated.CoreTranslations
import dev.kordex.core.i18n.types.Key
import dev.kordex.core.i18n.withContext
import dev.kordex.core.parsers.DurationParserException
import dev.kordex.core.parsers.InvalidTimeUnitException
import dev.kordex.core.utils.withContext
import dev.kordex.parser.StringParser
import net.time4j.Duration
import net.time4j.IsoUnit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import dev.kordex.core.commands.wrapOption
import dev.kordex.core.i18n.generated.CoreTranslations
import dev.kordex.core.i18n.toKey
import dev.kordex.core.i18n.types.Key
import dev.kordex.core.utils.withContext
import dev.kordex.core.i18n.withContext
import dev.kordex.modules.dev.unsafe.annotations.UnsafeAPI
import dev.kordex.parser.StringParser

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ import dev.kordex.core.extensions.publicSlashCommand
import dev.kordex.core.i18n.EMPTY_KEY
import dev.kordex.core.i18n.toKey
import dev.kordex.core.i18n.types.Key
import dev.kordex.core.i18n.withContext
import dev.kordex.core.pagination.EXPAND_EMOJI
import dev.kordex.core.pagination.PublicResponsePaginator
import dev.kordex.core.pagination.pages.Page
import dev.kordex.core.pagination.pages.Pages
import dev.kordex.core.sentry.BreadcrumbType
import dev.kordex.core.storage.StorageType
import dev.kordex.core.storage.StorageUnit
import dev.kordex.core.utils.withContext
import dev.kordex.modules.func.mappings.arguments.*
import dev.kordex.modules.func.mappings.builders.ExtMappingsBuilder
import dev.kordex.modules.func.mappings.enums.Channels
Expand Down

0 comments on commit a48dc89

Please sign in to comment.