Skip to content
This repository has been archived by the owner on Nov 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #245 from book000/feat/translate-koja
Browse files Browse the repository at this point in the history
feat: 日本語->韓国語->日本語をサポート
  • Loading branch information
book000 authored Apr 9, 2022
2 parents cd03885 + 90f97d7 commit 28aaf55
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/main/java/com/jaoafa/javajaotan2/command/Cmd_ToKoJa.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* jaoLicense
*
* Copyright (c) 2022 jao Minecraft Server
*
* The following license applies to this project: jaoLicense
*
* Japanese: https://github.com/jaoafa/jao-Minecraft-Server/blob/master/jaoLICENSE.md
* English: https://github.com/jaoafa/jao-Minecraft-Server/blob/master/jaoLICENSE-en.md
*/

package com.jaoafa.javajaotan2.command;

import cloud.commandframework.Command;
import cloud.commandframework.arguments.standard.StringArgument;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.jda.JDACommandSender;
import cloud.commandframework.meta.CommandMeta;
import com.jaoafa.javajaotan2.lib.CommandPremise;
import com.jaoafa.javajaotan2.lib.JavajaotanCommand;
import com.jaoafa.javajaotan2.lib.JavajaotanLibrary;
import com.jaoafa.javajaotan2.lib.Translate;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.time.Instant;

public class Cmd_ToKoJa implements CommandPremise {
@Override
public JavajaotanCommand.Detail details() {
return new JavajaotanCommand.Detail(
"tokoja",
"Google翻訳を用いて韓国語へ翻訳をしたあと、日本語へ翻訳を行います。"
);
}

@Override
public JavajaotanCommand.Cmd register(Command.Builder<JDACommandSender> builder) {
return new JavajaotanCommand.Cmd(
builder
.meta(CommandMeta.DESCRIPTION, "Google翻訳を用いて韓国語へ翻訳をしたあと、日本語へ翻訳を行います。")
.argument(StringArgument.greedy("text"))
.handler(context -> execute(context, this::translateKoJa))
.build()
);
}

private void translateKoJa(@NotNull Guild guild, @NotNull MessageChannel channel, @NotNull Member member, @NotNull Message message, @NotNull CommandContext<JDACommandSender> context) {
String text = context.get("text");
String displayText = JavajaotanLibrary.getContentDisplay(message, text);

Translate.Language lang1 = Translate.Language.KO;
Translate.Language lang2 = Translate.Language.JA;

Translate.TranslateResult result1 = Translate.translate(
Translate.Language.UNKNOWN,
lang1,
displayText
);
if (result1 == null) {
message.reply("翻訳に失敗しました。").queue();
return;
}

Translate.TranslateResult result2 = Translate.translate(
lang1,
lang2,
result1.result()
);
if (result2 == null) {
message.reply("翻訳に失敗しました。").queue();
return;
}

EmbedBuilder embed = new EmbedBuilder()
.setTitle("翻訳が成功しました:clap:")
.addField("`%s` -> `%s`".formatted(result1.from().toString(), lang1.toString()),
"```%s```".formatted(result1.result()),
true)
.addField("`%s` -> `%s`".formatted(lang1.toString(), lang2.toString()),
"```%s```".formatted(result2.result()),
true)
.setColor(Color.PINK)
.setTimestamp(Instant.now());
message.replyEmbeds(embed.build()).queue();
}
}

0 comments on commit 28aaf55

Please sign in to comment.