-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: shedaniel <[email protected]>
- Loading branch information
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/discord_api/kotlin/me/shedaniel/linkie/discord/lang/I18n.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (c) 2019, 2020, 2021 shedaniel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.shedaniel.linkie.discord.lang | ||
|
||
import me.shedaniel.linkie.discord.utils.CommandContext | ||
import java.util.* | ||
|
||
object I18n { | ||
var defaultLocale = "en-US" | ||
private val locales = mutableMapOf<String, Locale>() | ||
|
||
fun translate(locale: String, key: String, vararg args: Any): String { | ||
val s = _translate(locale, key) | ||
return try { | ||
s.format(*args) | ||
} catch (e: Exception) { | ||
s | ||
} | ||
} | ||
|
||
fun _translate(locale: String, key: String): String { | ||
return rawTranslate(locale, key) ?: rawTranslate(defaultLocale, key) ?: key | ||
} | ||
|
||
fun rawTranslate(locale: String, key: String): String? { | ||
return locales.getOrPut(locale) { | ||
I18n::class.java.classLoader.getResource("lang/$locale.properties")?.readText()?.let { | ||
Locale(locale, Properties().apply { load(it.reader()) }.let { | ||
it.entries.associate { it.key.toString() to it.value.toString() } | ||
}) | ||
} ?: Locale(locale, mapOf()) | ||
}.translations[key] | ||
} | ||
|
||
data class Locale( | ||
val name: String, | ||
val translations: Map<String, String>, | ||
) | ||
} | ||
|
||
fun String.i18n(ctx: CommandContext, vararg args: Any): String { | ||
return i18n(ctx.locale, *args) | ||
} | ||
|
||
fun String.i18n(locale: String?, vararg args: Any): String { | ||
return I18n.translate(locale ?: I18n.defaultLocale, this, *args) | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/kotlin/me/shedaniel/linkie/discord/tricks/TrickBasedCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2019, 2020, 2021 shedaniel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.shedaniel.linkie.discord.tricks | ||
|
||
import me.shedaniel.linkie.discord.Command | ||
import me.shedaniel.linkie.discord.scommands.SlashCommandBuilderInterface | ||
import me.shedaniel.linkie.discord.scommands.args | ||
import me.shedaniel.linkie.discord.scommands.optNullable | ||
import me.shedaniel.linkie.discord.scripting.ContextExtensions | ||
import me.shedaniel.linkie.discord.scripting.EvalContext | ||
import me.shedaniel.linkie.discord.scripting.LinkieScripting | ||
import me.shedaniel.linkie.discord.scripting.push | ||
import me.shedaniel.linkie.discord.utils.CommandContext | ||
import me.shedaniel.linkie.discord.utils.validateInGuild | ||
|
||
class TrickBasedCommand(val trick: Trick) : Command { | ||
override suspend fun SlashCommandBuilderInterface.buildCommand(slash: Boolean) { | ||
val args = args(required = false) | ||
executeCommandWithGetter { ctx, options -> execute(ctx, options.optNullable(args) ?: mutableListOf()) } | ||
} | ||
|
||
suspend fun execute(ctx: CommandContext, args: MutableList<String>) { | ||
ctx.validateInGuild { | ||
val evalContext = EvalContext( | ||
ctx, | ||
null, | ||
trick.flags, | ||
args, | ||
parent = false, | ||
) | ||
LinkieScripting.evalTrick(evalContext, message, trick) { | ||
LinkieScripting.simpleContext.push { | ||
ContextExtensions.commandContexts(evalContext, user, channel, message, this) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
text.about.title = About Linkie | ||
text.about.description = A mappings bot created by <@430615025066049538>. | ||
text.about.license = License | ||
text.about.links.core = Library Source | ||
text.about.links.bot = Bot Source | ||
text.about.links.invite = Bot Invite | ||
text.mappings.query.fuzzy_matched = **No results found for __%s__. Displaying related results.** | ||
text.mappings.query.title = List of %s Mappings for %s | ||
text.mappings.query.title.paged = List of %s Mappings for %s (Page %s/%s) | ||
text.mappings.query.class = **Class: __%s__** | ||
text.mappings.query.class.translate = **Class: %s => __%s__** | ||
text.mappings.query.field = **Field: %s#__%s__** | ||
text.mappings.query.field.translate = **Field: %s#%s => __%s#%s__** | ||
text.mappings.query.method = **Method: %s#__%s__** | ||
text.mappings.query.method.translate = **Method: %s#%s => __%s#%s__** | ||
text.mappings.query.name = __Name__: | ||
text.mappings.query.type = __Type__: | ||
text.mappings.query.mixin = __Mixin Target__: ` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
text.about.title = 關於 Linkie | ||
text.about.description = 一個由 <@430615025066049538> 開發的反混淆機器人。 | ||
text.about.license = 原始碼授權 | ||
text.about.links.core = Library Source | ||
text.about.links.bot = Bot Source | ||
text.about.links.invite = Bot Invite | ||
text.mappings.query.fuzzy_matched = **沒有符合您的搜尋 - __%s__. 目前顯示的是相關的搜尋結果。** | ||
text.mappings.query.title = %s (%s) 反混淆搜尋結果 | ||
text.mappings.query.title.paged = %s (%s) 反混淆搜尋結果 (第 %s / %s 頁) | ||
text.mappings.query.class = **類別: __%s__** | ||
text.mappings.query.class.translate = **類別: %s => __%s__** | ||
text.mappings.query.field = **屬性: %s#__%s__** | ||
text.mappings.query.field.translate = **屬性: %s#%s => __%s#%s__** | ||
text.mappings.query.method = **方法: %s#__%s__** | ||
text.mappings.query.method.translate = **方法: %s#%s => __%s#%s__** | ||
text.mappings.query.name = __名稱__: | ||
text.mappings.query.type = __類型__: | ||
text.mappings.query.mixin = __Mixin 目標__: ` |