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

@@ -32,12 +32,12 @@ async def command_checkout(app: PyroClient, message: Message):
row_width=1, resize_keyboard=True, one_time_keyboard=True
)
keyboard_delete.add(
ReplyButton("Yes, I want to delete it"),
ReplyButton("No, I don't want to delete it"),
ReplyButton(app._("delete_yes", "buttons", locale=user.locale)),
ReplyButton(app._("delete_no", "buttons", locale=user.locale)),
)
await message.reply_text(
"Here's pretty much all the data bot has. Please, use these buttons to choose whether you want to delete your data from the bot.",
app._("checkout", "messages", locale=user.locale),
reply_markup=keyboard_delete,
)
@@ -46,25 +46,25 @@ async def command_checkout(app: PyroClient, message: Message):
if answer_delete is None or answer_delete.text == "/cancel":
await message.reply_text(
"Cancelled.",
app._("cancelled", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
if answer_delete.text not in [
"Yes, I want to delete it",
"No, I don't want to delete it",
]:
if answer_delete.text not in app._(
"delete_yes", "buttons", locale=user.locale
) + app._("delete_no", "buttons", locale=user.locale):
await answer_delete.reply_text(
"Invalid answer provided. Use /cancel if you want to cancel this operation."
app._("selection_invalid", "messages", locale=user.locale).format(
cancel_notice=app._("cancel", "messages", locale=user.locale)
)
)
continue
if answer_delete.text in [
"No, I don't want to delete it",
]:
if answer_delete.text in app._("delete_no", "buttons", locale=user.locale):
await answer_delete.reply_text(
"Alright, cancelled.", reply_markup=ReplyKeyboardRemove()
app._("cancelled", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
@@ -74,10 +74,12 @@ async def command_checkout(app: PyroClient, message: Message):
keyboard_confirm = ReplyKeyboard(
row_width=1, resize_keyboard=True, one_time_keyboard=True
)
keyboard_confirm.add(ReplyButton("I agree and want to proceed"))
keyboard_confirm.add(
ReplyButton(app._("delete_confirm", "buttons", locale=user.locale))
)
await message.reply_text(
"Alright. Please, confirm that you want to delete your data from the bot.\n\nFollowing data will be deleted:\nSelected location, preferred language of the messages, notifications time and your notifications offset.",
app._("checkout_deletion", "messages", locale=user.locale),
reply_markup=keyboard_confirm,
)
@@ -86,14 +88,16 @@ async def command_checkout(app: PyroClient, message: Message):
if answer_confirm is None or answer_confirm.text == "/cancel":
await message.reply_text(
"Cancelled.",
app._("cancelled", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
if answer_confirm.text not in ["I agree and want to proceed"]:
if answer_confirm.text not in app.in_all_locales("delete_confirm", "buttons"):
await answer_confirm.reply_text(
"Invalid answer provided. Use /cancel if you want to cancel this operation."
app._("selection_invalid", "messages", locale=user.locale).format(
cancel_notice=app._("cancel", "messages", locale=user.locale)
)
)
continue
@@ -101,6 +105,6 @@ async def command_checkout(app: PyroClient, message: Message):
await user.delete()
await answer_confirm.reply_text(
"Your data has been deleted. If you want to start using this bot again, please use /setup command. Otherwise delete/block the bot and do not interact with it anymore.",
app._("checkout_deleted", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)

View File

@@ -11,5 +11,9 @@ async def command_help(app: PyroClient, message: Message):
user = await app.find_user(message.from_user)
await message.reply_text(
app._("help", "messages", locale=user.locale), disable_web_page_preview=True
app._("help", "messages", locale=user.locale).format(
url_contact=app.config["strings"]["url_contact"],
url_repo=app.config["strings"]["url_repo"],
),
disable_web_page_preview=True,
)

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(),
)

View File

@@ -25,7 +25,10 @@ async def command_set_offset(app: PyroClient, message: Message):
answer = await listen_message(app, message.chat.id, 300)
if answer is None or answer.text == "/cancel":
await message.reply_text("Cancelled.", reply_markup=ReplyKeyboardRemove())
await message.reply_text(
app._("cancelled", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
try:

View File

@@ -25,7 +25,10 @@ async def command_set_time(app: PyroClient, message: Message):
answer = await listen_message(app, message.chat.id, 300)
if answer is None or answer.text == "/cancel":
await message.reply_text("Cancelled.", reply_markup=ReplyKeyboardRemove())
await message.reply_text(
app._("cancelled", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
try:

View File

@@ -35,14 +35,19 @@ async def command_setup(app: PyroClient, message: Message):
answer_type = await listen_message(app, message.chat.id, 300)
if answer_type is None or answer_type.text == "/cancel":
await message.reply_text("Cancelled.", reply_markup=ReplyKeyboardRemove())
await message.reply_text(
app._("cancelled", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
if answer_type.location is None and answer_type.text not in [
"Search by location name",
]:
await answer_type.reply_text(
"Please, select a valid option using keyboard provided. Use /cancel if you want to cancel this operation."
app._("selection_invalid", "messages", locale=user.locale).format(
cancel_notice=app._("cancel", "messages", locale=user.locale)
)
)
continue

View File

@@ -27,21 +27,27 @@ async def command_start(app: PyroClient, message: Message):
location = await app.get_location(int(join_code))
except ValueError:
await message.reply_text(
"🚫 You have provided the location but it does not seem to be a valid one. Please, use the command /setup to manually configure the location."
app._("start_code_invalid", "messages", locale=user.locale)
)
return
keyboard = ReplyKeyboardMarkup(
[
[KeyboardButton("Yes, I want to use it")],
[KeyboardButton("No, I don't want to use it")],
[
KeyboardButton(
app._("start_code_yes", "buttons", locale=user.locale)
)
],
[KeyboardButton(app._("start_code_no", "buttons", locale=user.locale))],
],
resize_keyboard=True,
one_time_keyboard=True,
)
await message.reply_text(
f" You have started the bot by the link containing a location **{location.name}**.\n\nPlease, confirm whether you want to use it as your location.",
app._("start_code", "messages", locale=user.locale).format(
name=location.name
),
reply_markup=keyboard,
)
@@ -50,24 +56,24 @@ async def command_start(app: PyroClient, message: Message):
if answer is None or answer.text == "/cancel":
await message.reply_text(
"Cancelled.", reply_markup=ReplyKeyboardRemove()
app._("cancelled", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
if answer.text not in [
"Yes, I want to use it",
"No, I don't want to use it",
]:
if answer.text not in app.in_all_locales(
"start_code_yes", "buttons"
) + app.in_all_locales("start_code_no", "buttons"):
await answer.reply_text(
"Please, select a valid location using keyboard provided. Use /cancel if you want to cancel this operation."
app._("selection_invalid", "messages", locale=user.locale).format(
cancel_notice=app._("cancel", "messages", locale=user.locale)
)
)
continue
if answer.text in [
"No, I don't want to use it",
]:
if answer.text in app.in_all_locales("start_code_no", "buttons"):
await answer.reply_text(
"Alright, you're on your own now. Please, use the command /setup to configure your location and start receiving reminders.",
app._("start_selection_no", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
@@ -80,16 +86,24 @@ async def command_start(app: PyroClient, message: Message):
app._("time", "formats", locale=user.locale)
)
await answer.reply_text(
f"✅ Finished! Your location is now **{location.name}**. You will receive reminders about garbage collection {user.offset} d. in advance at {user_time}.\n\nPlease, visit /help if you want to know how to change notifications time or disable them.",
app._("start_selection_yes", "messages", locale=user.locale).format(
name=location.name, offset=user.offset, time=user_time
),
reply_markup=ReplyKeyboardRemove(),
)
return
if user.location is None:
await message.reply_text(
"📍 Let's configure your location. Press the button on pop-up keyboard to start the process.",
app._("start_configure", "messages", locale=user.locale),
reply_markup=ReplyKeyboardMarkup(
[[KeyboardButton(app._("configure", "buttons", locale=user.locale))]],
[
[
KeyboardButton(
app._("start_configure", "buttons", locale=user.locale)
)
]
],
resize_keyboard=True,
one_time_keyboard=True,
),

View File

@@ -15,15 +15,26 @@ async def command_toggle(app: PyroClient, message: Message):
await user.update_state(not user.enabled)
if user.enabled:
await message.reply_text("Notifications have been disabled.")
await message.reply_text(
app._("toggle_disabled", "messages", locale=user.locale)
)
return
user_time = datetime(1970, 1, 1, user.time_hour, user.time_minute).strftime("%H:%M")
if user.location is None:
await message.reply_text(
f"Notifications have been enabled {user.offset} d. before garbage collection at {datetime(1970, 1, 1, user.time_hour, user.time_minute).strftime('%H:%M')}. Use /setup to select your location."
app._("toggle_enabled", "messages", locale=user.locale).format(
offset=user.offset,
time=user_time,
),
)
return
await message.reply_text(
f"Notifications have been enabled {user.offset} d. before garbage collection at {datetime(1970, 1, 1, user.time_hour, user.time_minute).strftime('%H:%M')} at the **{user.location.name}**."
app._("toggle_enabled_location", "messages", locale=user.locale).format(
offset=user.offset,
time=user_time,
name=user.location.name,
)
)

View File

@@ -17,7 +17,7 @@ async def command_upcoming(app: PyroClient, message: Message):
if user.location is None:
await message.reply_text(
"You have no location set. Use /setup to select your location."
app._("upcoming_empty_location", "messages", locale=user.locale)
)
return
@@ -52,8 +52,12 @@ async def command_upcoming(app: PyroClient, message: Message):
if not entries:
await message.reply_text(
f"No garbage collection entries found for the next 30 days at **{user.location.name}**"
app._("upcoming_empty", "messages", locale=user.locale).format(
name=user.location.name
)
)
return
await message.reply_text(f"Upcoming garbage collection:\n\n{entries_text}")
await message.reply_text(
app._("upcoming", "messages", locale=user.locale).format(entries=entries_text)
)