Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added translation into any languages feature #81

Merged
merged 7 commits into from
Dec 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 62 additions & 14 deletions bot/extensions/translator_cog.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,81 @@
from discord.ext.commands import Cog, hybrid_command
from googletrans import Translator
from discord import Embed
from googletrans import Translator, LANGUAGES as language_code
from discord import Embed, Interaction
from discord.app_commands import Choice, autocomplete


class TranslatorCog(Cog, name="Translator", description="Translate sentences from any languages to English."):
def get_languages_available() -> list:
"""Return a list of all available language names, sorted in alphabetical order.

:return: A list of language names.
:rtype: list
"""
return [language_code[lang] for lang in language_code]


class TranslatorCog(Cog, name="Translator", description="Translate a sentence/word from any languages into any languages."):
def __init__(self, bot):
self.bot = bot

@hybrid_command(name='translator', help='Translate a sentence/word from any languages to English',
usage="sentence={sentence}")
async def translator(self, ctx, *, sentence):
"""Translate to English any user inputted sentence or word"""
async def language_autocomplete(self, interaction: Interaction, current: str) -> list[Choice[str]]:
"""Provide autocomplete suggestions for language names.

text_translator = Translator()
translated_text = text_translator.translate(sentence, dest='en')
:param interaction: The interaction object.
:type interaction: Interaction
:param current: The current value of the input field.
:type current: str
:return: A list of `Choice` objects containing language names.
:rtype: list[Choice[str]]
"""

embed = Embed(color=self.bot.default_color)
LANGUAGES = get_languages_available()
if not current:
return [
Choice(name=lang.capitalize(), value=lang.capitalize())
for lang in LANGUAGES[:25] if current.lower() in lang.lower()
]
else:
return [
Choice(name=lang.capitalize(), value=lang.capitalize())
for lang in LANGUAGES if current.lower() in lang.lower()
]

@hybrid_command(
name='translator',
help='Translate a sentence/word from any languages into any languages',
usage="sentence={sentence}"
)
@autocomplete(translate_into=language_autocomplete)
async def translator(self, ctx, *, sentence: str, translate_into: str):
"""Translate a sentence or word from any language into any languages.

:param ctx: The context object.
:type ctx: Context
:param sentence: The sentence or word to be translated.
:type sentence: str
:param translate_into: The language code for the target language.
:type tramslate_into: str
:return: Embed with original input and its translation
"""

text_translator = Translator()
translated_text = text_translator.translate(sentence, dest=translate_into)

embed = Embed(
color=self.bot.default_color
)

embed.add_field(
name="Original",
value=sentence,
name=f"{language_code[translated_text.src].capitalize()} Original",
value=sentence.capitalize(),
inline=False
)
embed.add_field(
name="Translated",
name=f"{translate_into} Translation",
value=translated_text.text,
inline=False
)

await ctx.send(embed=embed)


Expand Down