all repos — python-meme-bot @ 881781f58f4949319ef6afcbd6421c43e8a6ae24

Telegram Bot that uses PIL to compute light image processing.

python_meme_bot/localization.py (view raw)

 1import logging
 2from telegram import InlineKeyboardButton, InlineKeyboardMarkup
 3from telegram.ext import ContextTypes
 4
 5localization = {
 6    'en': {
 7        'name' : "English",
 8        'emoji' : "🇬🇧",
 9        'welcome' : "Welcome to PILuAnimeBot!",
10        'sauce' : "Sauce 🍝",
11        'no_caption' : "No caption detected.",
12        'lewd_toggle' : "Lewd content was {} for this chat.",
13        'enabled' : "enabled",
14        'disabled' : "disabled",
15        'unknown' : "Sorry, I didn't understand that command.",
16        'error': "An error has occurred. Please retry.",
17        'failed_effect': "Couldn't apply effect.",
18        'not_enough_cash': "You don't have enough money! Type /cash to reset it.",
19        'you_lost': "You lost...",
20        'you_won': "You won {:0.2f}$!",
21        'cash_result': " Your money: {:0.2f}$.",
22        'reroll': "Reroll (-{:0.2f}$)",
23        'summary': "You bet {}$ and won a total of {}$.",
24        'current_bet': "{}, your current bet is {:0.2f}$.",
25        'current_cash': "{}, you currently have {:0.2f}$ in your account.",
26        'cash_reset': "{}, your cash has been reset to {:0.2f}$. You can do this once per day.",
27        'cash_reset_fail': "{}, you have {:0.2f}$ in your account and you cannot reset it today. Come back tomorrow.",
28        'no_autospin': "Sorry, multiple spins are disabled in group chats.",
29        'current_language': "Current language: {}.\nChoices: {}\nTo change it, type \"/lang <code>\" or use one of the buttons below.",
30        'invalid_language': "Invalid language.",
31        'language_set': "Language set: {}",
32        'none_callback': "This button does nothing.",
33        'fast_output': "Win: {:0.2f}$",
34        'repeat_autospin': "Spin {} times again (-{:0.2f}$)"
35    },
36    'it': {
37        'name': "Italiano",
38        'emoji' : "🇮🇹",
39        'welcome' : "Benvenuto da PILuAnimeBot!",
40        'sauce' : "Salsa 🍝",
41        'no_caption' : "Scrivi un testo per favore.",
42        'lewd_toggle' : "La roba lewd è stata {} per questa chat.",
43        'enabled' : "abilitata",
44        'disabled' : "disabilitata",
45        'unknown' : "Non ho capito.",
46        'error': "Qualcosa è andato storto, riprova.",
47        'failed_effect': "Impossibile applicare l'effetto.",
48        'not_enough_cash': "Saldo insufficiente! Usa /cash per ripristinarlo.",
49        'you_lost': "Hai perso...",
50        'you_won': "Hai vinto {:0.2f}€!",
51        'cash_result': " Saldo: {:0.2f}€.",
52        'reroll': "Riprova (-{:0.2f}€)",
53        'summary': "Hai giocato {}€ e vinto un totale di {}€.",
54        'current_bet': "{}, il tuo bet attuale è {:0.2f}€.",
55        'current_cash': "{}, il tuo saldo attuale è {:0.2f}€.",
56        'cash_reset': "{}, il tuo saldo è stato ripristinato a {:0.2f}€. Puoi farlo una volta al giorno.",
57        'cash_reset_fail': "{}, il tuo saldo è {:0.2f}€ e non puoi più resettarlo oggi. Riprova domani.",
58        'no_autospin': "Gli spin multipli sono disabilitati nelle chat di gruppo.",
59        'current_language': "Lingua attuale: {}.\nAltre lingue: {}\nPer cambiarla, scrivi \"/lang <codice>\" o usa uno dei tasti qui sotto.",
60        'invalid_language': "Questa lingua non esiste.",
61        'language_set': "Lingua impostata: {}",
62        'none_callback': "Questo tasto non fa nulla.",
63        'fast_output': "Vinto: {:0.2f}",
64        'repeat_autospin': "Altri {} spin (-{:0.2f}€)"
65    },
66}
67langs = localization.keys()
68default_lang = "en"
69
70def get_lang(context: ContextTypes.DEFAULT_TYPE):
71    try:
72        return context.chat_data["lang"]
73    except KeyError:
74        context.chat_data["lang"] = default_lang
75        return default_lang
76
77def get_localized_string(text:str, context: ContextTypes.DEFAULT_TYPE):
78    lang = get_lang(context)
79    
80    try:
81        return localization[lang][text]
82    except KeyError:
83        logging.error("No text was found.")
84        return "localization error {}{}{}{}{}{}"
85    
86def format_lang(lang: str):
87    try:
88        return f"{localization[lang]['name']} {localization[lang]['emoji']}"
89    except KeyError:
90        return 'Unknown'
91    
92_n_entries = len(localization[default_lang].keys())
93_buttons = []
94for i in langs:
95    assert(_n_entries == len(localization[i].keys()))
96    _buttons.append(InlineKeyboardButton(text=format_lang(i), callback_data=f"set_lang_{i}"))
97
98N = 2
99lang_markup = InlineKeyboardMarkup([_buttons[n:n+N] for n in range(0, len(_buttons), N)])