1+ @file:Suppress(" DEPRECATION" )
2+
13package io.github.freya022.botcommands.api.localization.context
24
35import io.github.freya022.botcommands.api.commands.text.BaseCommandEvent
@@ -26,6 +28,7 @@ import net.dv8tion.jda.api.requests.restaction.WebhookMessageCreateAction
2628import net.dv8tion.jda.api.requests.restaction.WebhookMessageEditAction
2729import net.dv8tion.jda.api.requests.restaction.interactions.MessageEditCallbackAction
2830import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction
31+ import java.util.*
2932import javax.annotation.CheckReturnValue
3033
3134typealias 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" )
334380fun 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" )
345403fun 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 *
0 commit comments