diff --git a/src/discord_api/kotlin/me/shedaniel/linkie/discord/lang/I18n.kt b/src/discord_api/kotlin/me/shedaniel/linkie/discord/lang/I18n.kt new file mode 100644 index 0000000..8c120a2 --- /dev/null +++ b/src/discord_api/kotlin/me/shedaniel/linkie/discord/lang/I18n.kt @@ -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() + + 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, + ) +} + +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) +} \ No newline at end of file diff --git a/src/main/kotlin/me/shedaniel/linkie/discord/tricks/TrickBasedCommand.kt b/src/main/kotlin/me/shedaniel/linkie/discord/tricks/TrickBasedCommand.kt new file mode 100644 index 0000000..0970b1a --- /dev/null +++ b/src/main/kotlin/me/shedaniel/linkie/discord/tricks/TrickBasedCommand.kt @@ -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) { + 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) + } + } + } + } +} \ No newline at end of file diff --git a/src/main/resources/lang/en-US.properties b/src/main/resources/lang/en-US.properties new file mode 100644 index 0000000..eec2c48 --- /dev/null +++ b/src/main/resources/lang/en-US.properties @@ -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__: ` \ No newline at end of file diff --git a/src/main/resources/lang/zh-TW.properties b/src/main/resources/lang/zh-TW.properties new file mode 100644 index 0000000..eb66595 --- /dev/null +++ b/src/main/resources/lang/zh-TW.properties @@ -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 目標__: ` \ No newline at end of file