Skip to content

Commit f962973

Browse files
authored
Prefer using Locale instead of DiscordLocale, and deprecate such usages (#260)
1 parent c730f4b commit f962973

File tree

19 files changed

+185
-68
lines changed

19 files changed

+185
-68
lines changed

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/api/localization/LocalizableAction.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.github.freya022.botcommands.api.localization
22

3-
import io.github.freya022.botcommands.api.core.config.BLocalizationConfig
43
import io.github.freya022.botcommands.api.core.messages.BotCommandsMessages
54
import io.github.freya022.botcommands.api.core.messages.BotCommandsMessagesFactory
65
import io.github.freya022.botcommands.api.localization.context.LocalizationContext
@@ -102,12 +101,15 @@ interface LocalizableAction {
102101
* - No [registered bundle][BLocalizationConfig.responseBundles] containing the path could be found
103102
* - If the template requires an argument that was not passed to [entries]
104103
*/
104+
@Deprecated("Pass a Locale instead")
105105
fun getLocalizedMessage(locale: DiscordLocale, localizationPath: String, vararg entries: Localization.Entry): String =
106106
getLocalizedMessage(locale.toLocale(), localizationPath, *entries)
107107
}
108108

109109
fun LocalizableAction.getLocalizedMessage(locale: Locale, localizationPath: String, vararg entries: PairEntry): String =
110110
getLocalizedMessage(locale, localizationPath, *entries.mapToEntries())
111111

112+
@Suppress("DEPRECATION")
113+
@Deprecated("Pass a Locale instead")
112114
fun LocalizableAction.getLocalizedMessage(locale: DiscordLocale, localizationPath: String, vararg entries: PairEntry): String =
113-
getLocalizedMessage(locale, localizationPath, *entries.mapToEntries())
115+
getLocalizedMessage(locale, localizationPath, *entries.mapToEntries())

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/api/localization/context/AppLocalizationContext.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:Suppress("DEPRECATION")
2+
13
package io.github.freya022.botcommands.api.localization.context
24

35
import io.github.freya022.botcommands.api.localization.Localization
@@ -6,6 +8,7 @@ import io.github.freya022.botcommands.api.localization.interaction.GuildLocalePr
68
import io.github.freya022.botcommands.api.localization.interaction.UserLocaleProvider
79
import net.dv8tion.jda.api.interactions.DiscordLocale
810
import net.dv8tion.jda.api.interactions.Interaction
11+
import java.util.*
912
import javax.annotation.CheckReturnValue
1013

1114
/**
@@ -40,11 +43,15 @@ interface AppLocalizationContext : TextLocalizationContext {
4043
* @see withUserLocale
4144
*/
4245
//User locale is always provided in interactions
43-
val userLocale: DiscordLocale
46+
val userLocale: Locale
4447

48+
@Deprecated("Use the Locale overload")
4549
@CheckReturnValue
4650
override fun withGuildLocale(guildLocale: DiscordLocale?): AppLocalizationContext
4751

52+
@CheckReturnValue
53+
override fun withGuildLocale(guildLocale: Locale?): AppLocalizationContext
54+
4855
@CheckReturnValue
4956
override fun withBundle(localizationBundle: String): AppLocalizationContext
5057

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/api/localization/context/LocalizationContext.kt

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:Suppress("DEPRECATION")
2+
13
package io.github.freya022.botcommands.api.localization.context
24

35
import io.github.freya022.botcommands.api.commands.text.BaseCommandEvent
@@ -26,6 +28,7 @@ import net.dv8tion.jda.api.requests.restaction.WebhookMessageCreateAction
2628
import net.dv8tion.jda.api.requests.restaction.WebhookMessageEditAction
2729
import net.dv8tion.jda.api.requests.restaction.interactions.MessageEditCallbackAction
2830
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction
31+
import java.util.*
2932
import javax.annotation.CheckReturnValue
3033

3134
typealias PairEntry = Pair<String, Any>
@@ -45,9 +48,9 @@ interface LocalizationContext {
4548
* The locale used when no locale is specified, the best locale is picked in this order:
4649
* - The [user locale][Interaction.getUserLocale]
4750
* - The [guild locale][Guild.getLocale]
48-
* - The default locale ([DiscordLocale.ENGLISH_US])
51+
* - The default locale ([Locale.US])
4952
*/
50-
val effectiveLocale: DiscordLocale
53+
val effectiveLocale: Locale
5154

5255
/**
5356
* Returns the localization bundle of the current context.
@@ -74,17 +77,35 @@ interface LocalizationContext {
7477
*
7578
* @param guildLocale The guild locale to use, or `null` to remove it
7679
*/
80+
@Deprecated("Use the Locale overload")
7781
@CheckReturnValue
7882
fun withGuildLocale(guildLocale: DiscordLocale?): TextLocalizationContext
7983

84+
/**
85+
* Returns a new [TextLocalizationContext] with the specified guild locale.
86+
*
87+
* @param guildLocale The guild locale to use, or `null` to remove it
88+
*/
89+
@CheckReturnValue
90+
fun withGuildLocale(guildLocale: Locale?): TextLocalizationContext
91+
8092
/**
8193
* Returns a new [AppLocalizationContext] with the specified user locale.
8294
*
8395
* @param userLocale The user locale to use, or `null` to remove it
8496
*/
97+
@Deprecated("Use the Locale overload")
8598
@CheckReturnValue
8699
fun withUserLocale(userLocale: DiscordLocale?): AppLocalizationContext
87100

101+
/**
102+
* Returns a new [AppLocalizationContext] with the specified user locale.
103+
*
104+
* @param userLocale The user locale to use, or `null` to remove it
105+
*/
106+
@CheckReturnValue
107+
fun withUserLocale(userLocale: Locale?): AppLocalizationContext
108+
88109
/**
89110
* Returns a new localization context with the specified localization bundle.
90111
*
@@ -118,7 +139,19 @@ interface LocalizationContext {
118139
* prefixed with [localizationPrefix][LocalizationContext.localizationPrefix] unless starting with `/`
119140
* @param entries The entries to fill the template with
120141
*/
142+
@Deprecated("Use the Locale overload")
121143
fun localize(locale: DiscordLocale, localizationPath: String, vararg entries: Localization.Entry): String
144+
= localize(locale.toLocale(), localizationPath, *entries)
145+
146+
/**
147+
* Localizes the provided path, with the provided locale.
148+
*
149+
* @param locale The [Locale] to use when fetching the localization bundle
150+
* @param localizationPath The path of the localization template,
151+
* prefixed with [localizationPrefix][LocalizationContext.localizationPrefix] unless starting with `/`
152+
* @param entries The entries to fill the template with
153+
*/
154+
fun localize(locale: Locale, localizationPath: String, vararg entries: Localization.Entry): String
122155

123156
/**
124157
* Localizes the provided path, with the provided locale, or returns `null` if the path does not exist.
@@ -128,7 +161,19 @@ interface LocalizationContext {
128161
* prefixed with [localizationPrefix][LocalizationContext.localizationPrefix] unless starting with `/`
129162
* @param entries The entries to fill the template with
130163
*/
164+
@Deprecated("Use the Locale overload")
131165
fun localizeOrNull(locale: DiscordLocale, localizationPath: String, vararg entries: Localization.Entry): String?
166+
= localizeOrNull(locale.toLocale(), localizationPath, *entries)
167+
168+
/**
169+
* Localizes the provided path, with the provided locale, or returns `null` if the path does not exist.
170+
*
171+
* @param locale The [Locale] to use when fetching the localization bundle
172+
* @param localizationPath The path of the localization template,
173+
* prefixed with [localizationPrefix][LocalizationContext.localizationPrefix] unless starting with `/`
174+
* @param entries The entries to fill the template with
175+
*/
176+
fun localizeOrNull(locale: Locale, localizationPath: String, vararg entries: Localization.Entry): String?
132177

133178
/**
134179
* Localizes the provided path, with the [best locale][effectiveLocale] available.
@@ -153,8 +198,8 @@ interface LocalizationContext {
153198

154199
class Builder internal constructor(private val localizationService: LocalizationService, private val bundleName: String) {
155200
private var prefix: String? = null
156-
private var guildLocaleProvider: Lazy<DiscordLocale>? = null
157-
private var userLocaleProvider: Lazy<DiscordLocale>? = null
201+
private var guildLocaleProvider: Lazy<Locale>? = null
202+
private var userLocaleProvider: Lazy<Locale>? = null
158203

159204
/**
160205
* Sets the prefix of the context.
@@ -170,24 +215,24 @@ interface LocalizationContext {
170215
* Sets the guild locale to be provided by the passed [GuildLocaleProvider].
171216
*/
172217
fun setGuildLocaleProvider(provider: GuildLocaleProvider, interaction: Interaction): Builder {
173-
return setGuildLocaleProvider(lazy { provider.getDiscordLocale(interaction) })
218+
return setGuildLocaleProvider(lazy { provider.getLocale(interaction) })
174219
}
175220

176221
/**
177222
* Sets the guild locale to be provided by the passed [TextCommandLocaleProvider].
178223
*/
179224
fun setGuildLocaleProvider(provider: TextCommandLocaleProvider, event: MessageReceivedEvent): Builder {
180-
return setGuildLocaleProvider(lazy { provider.getDiscordLocale(event) })
225+
return setGuildLocaleProvider(lazy { provider.getLocale(event) })
181226
}
182227

183228
/**
184229
* Sets the guild locale to the provided one.
185230
*/
186-
fun setGuildLocale(locale: DiscordLocale): Builder {
231+
fun setGuildLocale(locale: Locale): Builder {
187232
return setGuildLocaleProvider(lazyOf(locale))
188233
}
189234

190-
private fun setGuildLocaleProvider(provider: Lazy<DiscordLocale>): Builder {
235+
private fun setGuildLocaleProvider(provider: Lazy<Locale>): Builder {
191236
this.guildLocaleProvider = provider
192237
return this
193238
}
@@ -196,17 +241,17 @@ interface LocalizationContext {
196241
* Sets the user locale to be provided by the passed [UserLocaleProvider].
197242
*/
198243
fun setUserLocaleProvider(provider: UserLocaleProvider, interaction: Interaction): Builder {
199-
return setUserLocaleProvider(lazy { provider.getDiscordLocale(interaction) })
244+
return setUserLocaleProvider(lazy { provider.getLocale(interaction) })
200245
}
201246

202247
/**
203248
* Sets the user locale to the provided one.
204249
*/
205-
fun setUserLocale(locale: DiscordLocale): Builder {
250+
fun setUserLocale(locale: Locale): Builder {
206251
return setUserLocaleProvider(lazyOf(locale))
207252
}
208253

209-
private fun setUserLocaleProvider(provider: Lazy<DiscordLocale>): Builder {
254+
private fun setUserLocaleProvider(provider: Lazy<Locale>): Builder {
210255
this.userLocaleProvider = provider
211256
return this
212257
}
@@ -273,8 +318,8 @@ interface LocalizationContext {
273318
context.getService<LocalizationService>(),
274319
localizationBundle,
275320
localizationPrefix,
276-
lazy { context.getService<GuildLocaleProvider>().getDiscordLocale(event) },
277-
lazy { context.getService<UserLocaleProvider>().getDiscordLocale(event) },
321+
lazy { context.getService<GuildLocaleProvider>().getLocale(event) },
322+
lazy { context.getService<UserLocaleProvider>().getLocale(event) },
278323
)
279324
}
280325

@@ -292,7 +337,7 @@ interface LocalizationContext {
292337
context.getService<LocalizationService>(),
293338
localizationBundle,
294339
localizationPrefix,
295-
lazy { context.getService<TextCommandLocaleProvider>().getDiscordLocale(event) },
340+
lazy { context.getService<TextCommandLocaleProvider>().getLocale(event) },
296341
userLocale?.toProvider(),
297342
)
298343
}
@@ -315,7 +360,7 @@ interface LocalizationContext {
315360
return Builder(localizationService, localizationBundle)
316361
}
317362

318-
private fun DiscordLocale.toProvider(): Lazy<DiscordLocale> = lazyOf(this)
363+
private fun DiscordLocale.toProvider(): Lazy<Locale> = lazyOf(this.toLocale())
319364
}
320365
}
321366

@@ -331,9 +376,21 @@ internal fun Array<out PairEntry>.mapToEntries() = Array(this.size) {
331376
* prefixed with [localizationPrefix][LocalizationContext.localizationPrefix] unless starting with `/`
332377
* @param entries The entries to fill the template with
333378
*/
379+
@Deprecated("Use the Locale overload")
334380
fun LocalizationContext.localize(locale: DiscordLocale, localizationPath: String, vararg entries: PairEntry): String =
335381
localize(locale, localizationPath, *entries.mapToEntries())
336382

383+
/**
384+
* Localizes the provided path, with the provided locale.
385+
*
386+
* @param locale The [Locale] to use when fetching the localization bundle
387+
* @param localizationPath The path of the localization template,
388+
* prefixed with [localizationPrefix][LocalizationContext.localizationPrefix] unless starting with `/`
389+
* @param entries The entries to fill the template with
390+
*/
391+
fun LocalizationContext.localize(locale: Locale, localizationPath: String, vararg entries: PairEntry): String =
392+
localize(locale, localizationPath, *entries.mapToEntries())
393+
337394
/**
338395
* Localizes the provided path, with the provided locale, or returns `null` if the path does not exist.
339396
*
@@ -342,9 +399,21 @@ fun LocalizationContext.localize(locale: DiscordLocale, localizationPath: String
342399
* prefixed with [localizationPrefix][LocalizationContext.localizationPrefix] unless starting with `/`
343400
* @param entries The entries to fill the template with
344401
*/
402+
@Deprecated("Use the Locale overload")
345403
fun LocalizationContext.localizeOrNull(locale: DiscordLocale, localizationPath: String, vararg entries: PairEntry): String? =
346404
localizeOrNull(locale, localizationPath, *entries.mapToEntries())
347405

406+
/**
407+
* Localizes the provided path, with the provided locale, or returns `null` if the path does not exist.
408+
*
409+
* @param locale The [Locale] to use when fetching the localization bundle
410+
* @param localizationPath The path of the localization template,
411+
* prefixed with [localizationPrefix][LocalizationContext.localizationPrefix] unless starting with `/`
412+
* @param entries The entries to fill the template with
413+
*/
414+
fun LocalizationContext.localizeOrNull(locale: Locale, localizationPath: String, vararg entries: PairEntry): String? =
415+
localizeOrNull(locale, localizationPath, *entries.mapToEntries())
416+
348417
/**
349418
* Localizes the provided path, with the [best locale][LocalizationContext.effectiveLocale] available.
350419
*

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/api/localization/context/TextLocalizationContext.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import io.github.freya022.botcommands.api.localization.annotations.LocalizationB
55
import io.github.freya022.botcommands.api.localization.interaction.GuildLocaleProvider
66
import net.dv8tion.jda.api.entities.Guild
77
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
8-
import net.dv8tion.jda.api.interactions.DiscordLocale
98
import net.dv8tion.jda.api.interactions.Interaction
9+
import java.util.*
1010
import javax.annotation.CheckReturnValue
1111

1212
/**
@@ -43,18 +43,18 @@ interface TextLocalizationContext : LocalizationContext {
4343
fun hasGuildLocale(): Boolean
4444

4545
/**
46-
* Returns the [DiscordLocale] of the guild.
46+
* Returns the [Locale] of the guild.
4747
*
4848
* The locale can either come from the [GuildLocaleProvider] or from a [withGuildLocale].
4949
*
50-
* @return the [DiscordLocale] of the guild
50+
* @return the [Locale] of the guild
5151
*
5252
* @throws IllegalStateException If the event did not happen in a Guild and the guild locale was not supplied
5353
*
5454
* @see hasGuildLocale
5555
* @see withGuildLocale
5656
*/
57-
val guildLocale: DiscordLocale
57+
val guildLocale: Locale
5858

5959
@CheckReturnValue
6060
override fun withBundle(localizationBundle: String): TextLocalizationContext

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/api/localization/interaction/GuildLocaleProvider.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ import java.util.*
2626
*/
2727
@InterfacedService(acceptMultiple = false)
2828
interface GuildLocaleProvider {
29+
@Deprecated("Use 'getLocale' instead, use 'DiscordLocale.from' / 'DiscordLocale#toLocale' if necessary")
2930
fun getDiscordLocale(interaction: Interaction): DiscordLocale
3031

32+
@Suppress("DEPRECATION")
3133
fun getLocale(interaction: Interaction): Locale =
3234
getDiscordLocale(interaction).toLocale()
33-
}
35+
}

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/api/localization/interaction/LocalizableEditCallback.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.github.freya022.botcommands.api.localization.interaction
22

3-
import io.github.freya022.botcommands.api.core.config.BLocalizationConfig
43
import io.github.freya022.botcommands.api.localization.Localization
54
import io.github.freya022.botcommands.api.localization.context.PairEntry
65
import io.github.freya022.botcommands.api.localization.context.mapToEntries
@@ -95,6 +94,7 @@ interface LocalizableEditCallback {
9594
* - No [registered bundle][BLocalizationConfig.responseBundles] containing the path could be found
9695
* - If the template requires an argument that was not passed to [entries]
9796
*/
97+
@Deprecated("Pass a Locale instead")
9898
@CheckReturnValue
9999
fun editLocalized(locale: DiscordLocale, localizationPath: String, vararg entries: Localization.Entry): MessageEditCallbackAction =
100100
editLocalized(locale.toLocale(), localizationPath, *entries)
@@ -201,6 +201,8 @@ fun LocalizableEditCallback.editGuild(localizationPath: String, vararg entries:
201201
* - No [registered bundle][BLocalizationConfig.responseBundles] containing the path could be found
202202
* - If the template requires an argument that was not passed to [entries]
203203
*/
204+
@Suppress("DEPRECATION")
205+
@Deprecated("Pass a Locale instead")
204206
fun LocalizableEditCallback.editLocalized(locale: DiscordLocale, localizationPath: String, vararg entries: PairEntry) =
205207
editLocalized(locale, localizationPath, *entries.mapToEntries())
206208

@@ -226,4 +228,4 @@ fun LocalizableEditCallback.editLocalized(locale: DiscordLocale, localizationPat
226228
* - If the template requires an argument that was not passed to [entries]
227229
*/
228230
fun LocalizableEditCallback.editLocalized(locale: Locale, localizationPath: String, vararg entries: PairEntry) =
229-
editLocalized(locale, localizationPath, *entries.mapToEntries())
231+
editLocalized(locale, localizationPath, *entries.mapToEntries())

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/api/localization/interaction/LocalizableInteraction.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.github.freya022.botcommands.api.localization.interaction
22

3-
import io.github.freya022.botcommands.api.core.config.BLocalizationConfig
43
import io.github.freya022.botcommands.api.localization.LocalizableAction
54
import io.github.freya022.botcommands.api.localization.Localization
65
import io.github.freya022.botcommands.api.localization.context.AppLocalizationContext
@@ -33,7 +32,7 @@ interface LocalizableInteraction : LocalizableAction {
3332

3433
/**
3534
* Returns a localization context for the provided bundle name and path prefix,
36-
* using the locales from [UserLocaleProvider.getDiscordLocale] and [GuildLocaleProvider.getDiscordLocale].
35+
* using the locales from [UserLocaleProvider] and [GuildLocaleProvider].
3736
*/
3837
override fun getLocalizationContext(bundleName: String, pathPrefix: String?): AppLocalizationContext
3938

@@ -146,4 +145,4 @@ fun LocalizableInteraction.getUserMessage(localizationPath: String, vararg entri
146145
* - If the template requires an argument that was not passed to [entries]
147146
*/
148147
fun LocalizableInteraction.getGuildMessage(localizationPath: String, vararg entries: PairEntry): String =
149-
getGuildMessage(localizationPath, *entries.mapToEntries())
148+
getGuildMessage(localizationPath, *entries.mapToEntries())

0 commit comments

Comments
 (0)