Improved Translation
This commit is contained in:
parent
937dd76fe7
commit
b23fa1aa60
3 changed files with 48 additions and 9 deletions
|
@ -15,12 +15,31 @@ def name_to_ISO(name: str) -> str:
|
||||||
alpha = ""
|
alpha = ""
|
||||||
for language in langs:
|
for language in langs:
|
||||||
if language.name.lower() == name.lower():
|
if language.name.lower() == name.lower():
|
||||||
alpha = language.alpha_2
|
try:
|
||||||
break
|
alpha = language.alpha_2
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
logging.warning(f"Tried to get alpha2 code of unknown language {language}")
|
||||||
|
|
||||||
return alpha
|
return alpha
|
||||||
|
|
||||||
def translate(text: str, lang: str) -> str:
|
def ISO_to_name(ISO: str) -> str:
|
||||||
|
name = ""
|
||||||
|
for language in langs:
|
||||||
|
try:
|
||||||
|
if language.alpha_2.lower() == ISO.lower():
|
||||||
|
try:
|
||||||
|
name = language.name
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
logging.warning(f"Tried to get name of unknown language code {ISO}")
|
||||||
|
except:
|
||||||
|
# i dont care tbh
|
||||||
|
pass
|
||||||
|
|
||||||
|
return name
|
||||||
|
|
||||||
|
def translate(text: str, lang: str) -> (str, str):
|
||||||
url_encoded_text = urllib.parse.quote(text)
|
url_encoded_text = urllib.parse.quote(text)
|
||||||
url = f"https://translate.yandex.net/api/v1.5/tr.json/translate?key={api_key}&text={url_encoded_text}&lang={lang}"
|
url = f"https://translate.yandex.net/api/v1.5/tr.json/translate?key={api_key}&text={url_encoded_text}&lang={lang}"
|
||||||
|
|
||||||
|
@ -29,4 +48,5 @@ def translate(text: str, lang: str) -> str:
|
||||||
logging.error(f"Failed to contact Yandex API: {response.status_code}")
|
logging.error(f"Failed to contact Yandex API: {response.status_code}")
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
return response.json()["text"][0]
|
print(response.json())
|
||||||
|
return (response.json()["text"][0], response.json()["lang"])
|
|
@ -1,6 +1,6 @@
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from util import checking
|
from util import checking, embed
|
||||||
from api import translation
|
from api import translation
|
||||||
|
|
||||||
class Translation(commands.Cog):
|
class Translation(commands.Cog):
|
||||||
|
@ -8,18 +8,22 @@ class Translation(commands.Cog):
|
||||||
def __init__(self, client: discord.Client):
|
def __init__(self, client: discord.Client):
|
||||||
self.client = client
|
self.client = client
|
||||||
|
|
||||||
@commands.command(name="translate", description="Translates the given text", usage="translate <ISO CountryCode>", aliases=["tl"])
|
@commands.command(name="translate", description="Translates the given text", usage="translate <Language> <Text>", aliases=["tl"])
|
||||||
@commands.check(checking.is_author_bot)
|
@commands.check(checking.is_author_bot)
|
||||||
async def translate(self, ctx: commands.Context, language: str, *message: str):
|
async def translate(self, ctx: commands.Context, language: str, *message: str):
|
||||||
# Get language code
|
# Get language code
|
||||||
code = translation.name_to_ISO(language)
|
code = translation.name_to_ISO(language)
|
||||||
text = ' '.join(message)
|
text = ' '.join(message)
|
||||||
if code == "":
|
if code == "":
|
||||||
await ctx.send(f"There is no language named {language}.")
|
await ctx.send(embed=embed.make_error_embed(f"There is no language named **{language}**."))
|
||||||
return
|
return
|
||||||
|
|
||||||
translated = translation.translate(text, code)
|
response = translation.translate(text, code)
|
||||||
await ctx.send(translated)
|
translated = response[0]
|
||||||
|
direction = response[1].split("-")
|
||||||
|
_from = translation.ISO_to_name(direction[0])
|
||||||
|
_to = translation.ISO_to_name(direction[1])
|
||||||
|
await ctx.send(embed=embed.make_embed_field(f"{_from} -> {_to}", None, text, translated))
|
||||||
|
|
||||||
def setup(client: discord.Client):
|
def setup(client: discord.Client):
|
||||||
client.add_cog(Translation(client))
|
client.add_cog(Translation(client))
|
15
util/embed.py
Normal file
15
util/embed.py
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import discord
|
||||||
|
from util import config
|
||||||
|
|
||||||
|
def make_error_embed(message: str) -> discord.Embed:
|
||||||
|
embed = discord.Embed(title="Error", description=message, colour=int(config.settings["err_color"], 16))
|
||||||
|
return embed
|
||||||
|
|
||||||
|
def make_embed(title: str, desc: str) -> discord.Embed:
|
||||||
|
embed = discord.Embed(title=title, description=desc, colour=int(config.settings["color"], 16))
|
||||||
|
return embed
|
||||||
|
|
||||||
|
def make_embed_field(title: str, desc: str, field_name: str, field_val: str) -> discord.Embed:
|
||||||
|
embed = discord.Embed(title=title, description=desc, colour=int(config.settings["color"], 16))
|
||||||
|
embed.add_field(name=field_name, value=field_val)
|
||||||
|
return embed
|
Loading…
Add table
Add a link
Reference in a new issue