Skip to content

Commit

Permalink
feat(translator): full text support vararg
Browse files Browse the repository at this point in the history
  • Loading branch information
1zun4 committed Nov 6, 2024
1 parent 4a85104 commit 06241c6
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions examples/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const InputStreamReader = Java.type("java.io.InputStreamReader");
const script = registerScript({
name: "Translator",
version: "1.0.8",
authors: ["Trikaes"],
authors: ["Trikaes", "1zuna"],
});

script.registerCommand({
Expand All @@ -28,17 +28,19 @@ script.registerCommand({
description: "The target language code (en, fr, de, etc...)",
},
{
name: "word",
name: "text",
type: "string",
required: true,
description: "The word to translate",
vararg: true,
description: "The text to translate",
},
],
onExecute(sourceLanguage, targetLanguage, word) {
onExecute(sourceLanguage, targetLanguage, text) {
try {
var text = text.join(" ");
const url = new URL(
`https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sourceLanguage}&tl=${targetLanguage}&dt=t&q=${encodeURIComponent(
word
text
)}`
); // finally got this to work

Expand Down Expand Up @@ -66,15 +68,15 @@ script.registerCommand({
const translatedText = jsonResponse[0][0][0];

Client.displayChatMessage(
`§cTranslation §7(§c${sourceLanguage} §7to §c${targetLanguage}§7): §c${word} §7-> §c${translatedText}`
`§cTranslation §7(§c${sourceLanguage} §7to §c${targetLanguage}§7): §c${text} §7-> §c${translatedText}`
); // cool colors
} else {
Client.displayChatMessage(
`§4failed to translate word '${word}'. response code: ${responseCode}`
`§4failed to translate text '${text}'. response code: ${responseCode}`
); // hope i dont get this
}
} catch (error) {
Client.displayChatMessage(`§4error translating word '${word}': ${error}`); // hope i dont get this
Client.displayChatMessage(`§4error translating text '${text}': ${error}`); // hope i dont get this
}
},
});

0 comments on commit 06241c6

Please sign in to comment.