💫 rate support i18n

This commit is contained in:
Xtao_dada 2021-04-12 23:55:08 +08:00 committed by GitHub
parent 7d1f4c2a8d
commit ab728765ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

26
rate.py
View File

@ -2,7 +2,14 @@
import sys
import urllib.request
from pagermaid.listener import listener
from pagermaid.listener import listener, config
# i18n
## 默认语言
lang_rate = {"des": "货币汇率插件", "arg": "<FROM> <TO> <NUM>", "not": "请先安装依赖:`python3 -m pip install xmltodict`\n随后,请重启 pagermaid。", "help": "这是货币汇率插件\n\n使用方法: `-rate <FROM> <TO> <NB>`\n\n支持货币: \n", "nc": "不是支持的货币. \n\n支持货币: \n"}
## 其他语言
if config["application_language"] == "en":
lang_rate = {"des": "Currency exchange rate plugin", "arg": "<FROM> <TO> <NUM>", "not": "Please run: `python3 -m pip install xmltodict`\nand restart pagermaid。", "help": "Currency exchange rate plugin\n\nUsage: `-rate <FROM> <TO> <NB>`\n\nSupport: \n", "nc": "is not support\n\nSupport: \n"}
imported = True
API = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
@ -37,22 +44,19 @@ def logsync(message):
sys.stdout.writelines(f"{message}\n")
logsync("rate: loading... If failed, please install xmltodict first.")
@listener(is_plugin=True, outgoing=True, command="rate",
description="货币汇率插件",
parameters="<FROM> <TO> <NB>")
description=lang_rate["des"],
parameters=lang_rate["arg"])
async def rate(context):
if not imported:
await context.edit("请先安装依赖:`python3 -m pip install xmltodict`\n随后,请重启 pagermaid。")
await context.edit(lang_rate["not"])
return
if not context.parameter:
await context.edit(
f"这是货币汇率插件\n\n使用方法: `-rate <FROM> <TO> <NB>`\n\n支持货币: \n{', '.join(currencies)}")
f"{lang_rate["help"]}{', '.join(currencies)}")
return
if len(context.parameter) != 3:
await context.edit(f"使用方法: `-rate <FROM> <TO> <NB>`\n\n支持货币: \n{', '.join(currencies)}")
await context.edit(f"{lang_rate["help"]}{', '.join(currencies)}")
return
FROM = context.parameter[0].upper().strip()
TO = context.parameter[1].upper().strip()
@ -62,10 +66,10 @@ async def rate(context):
NB = 1.0
if currencies.count(FROM) == 0:
await context.edit(
f"{FROM}不是支持的货币. \n\n支持货币: \n{', '.join(currencies)}")
f"{FROM}{lang_rate["nc"]}{', '.join(currencies)}")
return
if currencies.count(TO) == 0:
await context.edit(f"{TO}不是支持的货币. \n\n支持货币: \n{', '.join(currencies)}")
await context.edit(f"{TO}{lang_rate["nc"]}{', '.join(currencies)}")
return
rate_num = round(rates[TO] / rates[FROM] * NB, 2)
await context.edit(f'{FROM} : {TO} = {NB} : {rate_num}')