WIP: Locale strings

This commit is contained in:
2023-08-29 16:32:37 +02:00
parent 029a965860
commit 9cda8859da
14 changed files with 214 additions and 78 deletions

View File

@@ -3,7 +3,7 @@ from typing import List, Mapping, Union
from convopyro import listen_message
from pyrogram import filters
from pyrogram.types import Message
from pyrogram.types import ForceReply, Message, ReplyKeyboardRemove
from ujson import loads
from classes.pyroclient import PyroClient
@@ -15,18 +15,28 @@ from modules.database import col_entries
~filters.scheduled & filters.private & custom_filters.owner & filters.command(["import"], prefixes=["/"]) # type: ignore
)
async def command_import(app: PyroClient, message: Message):
await message.reply_text("Alright. Send me a valid JSON.")
user = await app.find_user(message.from_user)
await message.reply_text(
app._("import", "messages", locale=user.locale),
reply_markup=ForceReply(placeholder=""),
)
while True:
answer = await listen_message(app, message.chat.id, 300)
if answer is None or answer.text == "/cancel":
await message.reply_text("Cancelled.")
await message.reply_text(
app._("cancelled", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
if answer.document is None or answer.document.mime_type != "application/json":
await answer.reply_text(
"Invalid input. Please, send me a JSON file with entries."
app._("import_invalid_filetype", "messages", locale=user.locale).format(
cancel_notice=app._("cancel", "messages", locale=user.locale)
)
)
continue
@@ -38,7 +48,10 @@ async def command_import(app: PyroClient, message: Message):
for entry in entries:
if not isinstance(entries, list):
await answer.reply_text("This is not a valid garbage collection JSON.")
await answer.reply_text(
app._("import_invalid", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
for key in ("locations", "garbage_type", "date"):
@@ -47,14 +60,18 @@ async def command_import(app: PyroClient, message: Message):
or (key == "garbage_type" and not isinstance(entry[key], int))
or (key == "locations" and not isinstance(entry[key], list))
):
await answer.reply_text("This is not a valid garbage collection JSON.")
await answer.reply_text(
app._("import_invalid", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
if key == "date":
try:
datetime.fromisoformat(str(entry[key]))
except (ValueError, TypeError):
await answer.reply_text(
"Entries contain invalid date formats. Use **ISO 8601** date format."
app._("import_invalid_date", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
@@ -70,5 +87,8 @@ async def command_import(app: PyroClient, message: Message):
await col_entries.insert_many(entries_clean)
await answer.reply_text(
f"You have successfully inserted {len(entries_clean)} entries."
app._("import_finished", "messages", locale=user.locale).format(
count=len(entries_clean)
),
reply_markup=ReplyKeyboardRemove(),
)