Optional multi-translations

This commit is contained in:
Jannes Höke 2016-05-22 17:02:27 +02:00
parent 005445c4dd
commit 4cdffffa5f
8 changed files with 229 additions and 74 deletions

130
bot.py
View file

@ -41,6 +41,7 @@ from utils import _, __, send_async, answer_async, user_locale, game_locales, \
from shared_vars import botan, gm, updater, dispatcher from shared_vars import botan, gm, updater, dispatcher
import simple_commands, settings import simple_commands, settings
from simple_commands import help
logging.basicConfig( logging.basicConfig(
@ -134,11 +135,11 @@ def leave_game(bot, update):
except NotEnoughPlayersError: except NotEnoughPlayersError:
gm.end_game(chat, user) gm.end_game(chat, user)
send_async(bot, chat.id, text=__("Game ended!")) send_async(bot, chat.id, text=__("Game ended!", game.translate))
else: else:
send_async(bot, chat.id, send_async(bot, chat.id,
text=__("Okay. Next Player: {name}").format( text=__("Okay. Next Player: {name}", game.translate).format(
name=display_name(game.current_player.user)), name=display_name(game.current_player.user)),
reply_to_message_id=update.message.message_id) reply_to_message_id=update.message.message_id)
@ -185,20 +186,20 @@ def status_update(bot, update):
chat = update.message.chat chat = update.message.chat
if update.message.left_chat_member: if update.message.left_chat_member:
try: user = update.message.left_chat_member
user = update.message.left_chat_member
except KeyError:
return
try: try:
gm.leave_game(user, chat) gm.leave_game(user, chat)
game = gm.player_for_user_in_chat(user, chat).game
except NoGameInChatError: except NoGameInChatError:
pass pass
except NotEnoughPlayersError: except NotEnoughPlayersError:
gm.end_game(chat, user) gm.end_game(chat, user)
send_async(bot, chat.id, text=__("Game ended!")) send_async(bot, chat.id, text=__("Game ended!", game.translate))
else: else:
send_async(bot, chat.id, text=__("Removing {name} from the game") send_async(bot, chat.id, text=__("Removing {name} from the game",
game.translate)
.format(name=display_name(user))) .format(name=display_name(user)))
@ -230,6 +231,13 @@ def start_game(bot, update, args):
game.play_card(game.last_card) game.play_card(game.last_card)
game.started = True game.started = True
first_message = (
__("First player: {name}\n"
"Use /close to stop people from joining the game.\n"
"Enable multi-translations with /enable_translations",
game.translate)
.format(name=display_name(game.current_player.user)))
@run_async @run_async
def send_first(): def send_first():
"""Send the first card and player""" """Send the first card and player"""
@ -239,12 +247,7 @@ def start_game(bot, update, args):
timeout=TIMEOUT) timeout=TIMEOUT)
bot.sendMessage(chat.id, bot.sendMessage(chat.id,
text=__("First player: {name}\n" text=first_message,
"Use /close to stop people from "
"joining the game.")
.format(
name=display_name(game.current_player.user)
),
timeout=TIMEOUT) timeout=TIMEOUT)
send_first() send_first()
@ -327,6 +330,63 @@ def open_game(bot, update):
return return
@user_locale
def enable_translations(bot, update):
"""Handler for the /enable_translations command"""
chat = update.message.chat
user = update.message.from_user
games = gm.chatid_games.get(chat.id)
if not games:
send_async(bot, chat.id,
text=_("There is no running game in this chat."))
return
game = games[-1]
if game.owner.id == user.id:
game.translate = True
send_async(bot, chat.id, text=_("Enabled multi-translations. "
"Disable with /disable_translations"))
return
else:
send_async(bot, chat.id,
text=_("Only the game creator ({name}) can do that")
.format(name=game.owner.first_name),
reply_to_message_id=update.message.message_id)
return
@user_locale
def disable_translations(bot, update):
"""Handler for the /disable_translations command"""
chat = update.message.chat
user = update.message.from_user
games = gm.chatid_games.get(chat.id)
if not games:
send_async(bot, chat.id,
text=_("There is no running game in this chat."))
return
game = games[-1]
if game.owner.id == user.id:
game.translate = False
send_async(bot, chat.id, text=_("Disabled multi-translations. "
"Enable them again with "
"/enable_translations"))
return
else:
send_async(bot, chat.id,
text=_("Only the game creator ({name}) can do that")
.format(name=game.owner.first_name),
reply_to_message_id=update.message.message_id)
return
@game_locales @game_locales
@user_locale @user_locale
def skip_player(bot, update): def skip_player(bot, update):
@ -365,7 +425,7 @@ def skip_player(bot, update):
send_async(bot, chat.id, send_async(bot, chat.id,
text=__("Waiting time to skip this player has " text=__("Waiting time to skip this player has "
"been reduced to {time} seconds.\n" "been reduced to {time} seconds.\n"
"Next player: {name}") "Next player: {name}", game.translate)
.format(time=skipped_player.waiting_time, .format(time=skipped_player.waiting_time,
name=display_name(next_player.user))) name=display_name(next_player.user)))
game.turn() game.turn()
@ -376,7 +436,7 @@ def skip_player(bot, update):
send_async(bot, chat.id, send_async(bot, chat.id,
text=__("{name1} was skipped four times in a row " text=__("{name1} was skipped four times in a row "
"and has been removed from the game.\n" "and has been removed from the game.\n"
"Next player: {name2}") "Next player: {name2}", game.translate)
.format(name1=display_name(skipped_player.user), .format(name1=display_name(skipped_player.user),
name2=display_name(next_player.user))) name2=display_name(next_player.user)))
@ -384,11 +444,12 @@ def skip_player(bot, update):
send_async(bot, chat.id, send_async(bot, chat.id,
text=__("{name} was skipped four times in a row " text=__("{name} was skipped four times in a row "
"and has been removed from the game.\n" "and has been removed from the game.\n"
"The game ended.") "The game ended.", game.translate)
.format(name=display_name(skipped_player.user))) .format(name=display_name(skipped_player.user)))
gm.end_game(chat.id, skipped_player.user) gm.end_game(chat.id, skipped_player.user)
@game_locales @game_locales
@user_locale @user_locale
def reply_to_query(bot, update): def reply_to_query(bot, update):
@ -420,10 +481,10 @@ def reply_to_query(bot, update):
add_draw(player, results) add_draw(player, results)
else: else:
add_pass(results) add_pass(results, game)
if game.last_card.special == c.DRAW_FOUR and game.draw_counter: if game.last_card.special == c.DRAW_FOUR and game.draw_counter:
add_call_bluff(results) add_call_bluff(results, game)
playable = player.playable_cards() playable = player.playable_cards()
added_ids = list() # Duplicates are not allowed added_ids = list() # Duplicates are not allowed
@ -479,7 +540,7 @@ def process_result(bot, update):
return return
elif int(anti_cheat) != last_anti_cheat: elif int(anti_cheat) != last_anti_cheat:
send_async(bot, chat.id, send_async(bot, chat.id,
text=__("Cheat attempt by {name}") text=__("Cheat attempt by {name}", game.translate)
.format(name=display_name(player.user))) .format(name=display_name(player.user)))
return return
elif result_id == 'call_bluff': elif result_id == 'call_bluff':
@ -498,7 +559,7 @@ def process_result(bot, update):
if game in gm.chatid_games.get(chat.id, list()): if game in gm.chatid_games.get(chat.id, list()):
send_async(bot, chat.id, send_async(bot, chat.id,
text=__("Next player: {name}") text=__("Next player: {name}", game.translate)
.format(name=display_name(game.current_player.user))) .format(name=display_name(game.current_player.user)))
@ -510,7 +571,8 @@ def reset_waiting_time(bot, player):
player.waiting_time = 90 player.waiting_time = 90
send_async(bot, chat.id, send_async(bot, chat.id,
text=__("Waiting time for {name} has been reset to 90 " text=__("Waiting time for {name} has been reset to 90 "
"seconds").format(name=display_name(player.user))) "seconds", player.game.translate)
.format(name=display_name(player.user)))
def do_play_card(bot, player, result_id): def do_play_card(bot, player, result_id):
@ -529,11 +591,12 @@ def do_play_card(bot, player, result_id):
if len(player.cards) == 0: if len(player.cards) == 0:
send_async(bot, chat.id, send_async(bot, chat.id,
text=__("{name} won!").format(name=user.first_name)) text=__("{name} won!", game.translate)
.format(name=user.first_name))
try: try:
gm.leave_game(user, chat) gm.leave_game(user, chat)
except NotEnoughPlayersError: except NotEnoughPlayersError:
send_async(bot, chat.id, text=__("Game ended!")) send_async(bot, chat.id, text=__("Game ended!", game.translate))
gm.end_game(chat, user) gm.end_game(chat, user)
if botan: if botan:
@ -551,7 +614,8 @@ def do_draw(bot, player):
player.draw() player.draw()
except DeckEmptyError: except DeckEmptyError:
send_async(bot, player.game.chat.id, send_async(bot, player.game.chat.id,
text=__("There are no more cards in the deck.")) text=__("There are no more cards in the deck.",
game.translate))
if (game.last_card.value == c.DRAW_TWO or if (game.last_card.value == c.DRAW_TWO or
game.last_card.special == c.DRAW_FOUR) and \ game.last_card.special == c.DRAW_FOUR) and \
@ -566,26 +630,30 @@ def do_call_bluff(bot, player):
if player.prev.bluffing: if player.prev.bluffing:
send_async(bot, chat.id, send_async(bot, chat.id,
text=__("Bluff called! Giving 4 cards to {name}") text=__("Bluff called! Giving 4 cards to {name}",
game.translate)
.format(name=player.prev.user.first_name)) .format(name=player.prev.user.first_name))
try: try:
player.prev.draw() player.prev.draw()
except DeckEmptyError: except DeckEmptyError:
send_async(bot, player.game.chat.id, send_async(bot, player.game.chat.id,
text=__("There are no more cards in the deck.")) text=__("There are no more cards in the deck.",
game.translate))
else: else:
game.draw_counter += 2 game.draw_counter += 2
send_async(bot, chat.id, send_async(bot, chat.id,
text=__("{name1} didn't bluff! Giving 6 cards to {name2}") text=__("{name1} didn't bluff! Giving 6 cards to {name2}",
game.translate)
.format(name1=player.prev.user.first_name, .format(name1=player.prev.user.first_name,
name2=player.user.first_name)) name2=player.user.first_name))
try: try:
player.draw() player.draw()
except DeckEmptyError: except DeckEmptyError:
send_async(bot, player.game.chat.id, send_async(bot, player.game.chat.id,
text=__("There are no more cards in the deck.")) text=__("There are no more cards in the deck.",
game.translate))
game.turn() game.turn()
@ -600,6 +668,10 @@ dispatcher.add_handler(CommandHandler('join', join_game))
dispatcher.add_handler(CommandHandler('leave', leave_game)) dispatcher.add_handler(CommandHandler('leave', leave_game))
dispatcher.add_handler(CommandHandler('open', open_game)) dispatcher.add_handler(CommandHandler('open', open_game))
dispatcher.add_handler(CommandHandler('close', close_game)) dispatcher.add_handler(CommandHandler('close', close_game))
dispatcher.add_handler(CommandHandler('enable_translations',
enable_translations))
dispatcher.add_handler(CommandHandler('disable_translations',
disable_translations))
dispatcher.add_handler(CommandHandler('skip', skip_player)) dispatcher.add_handler(CommandHandler('skip', skip_player))
dispatcher.add_handler(MessageHandler([Filters.status_update], status_update)) dispatcher.add_handler(MessageHandler([Filters.status_update], status_update))
dispatcher.add_error_handler(error) dispatcher.add_error_handler(error)

View file

@ -34,6 +34,7 @@ class Game(object):
started = False started = False
owner = None owner = None
open = True open = True
translate = False
def __init__(self, chat): def __init__(self, chat):
self.chat = chat self.chat = chat

View file

@ -77,6 +77,8 @@ class GameManager(object):
self.leave_game(user, chat) self.leave_game(user, chat)
except NoGameInChatError: except NoGameInChatError:
pass pass
except NotEnoughPlayersError:
self.end_game(chat, user)
player = Player(game, user) player = Player(game, user)

View file

@ -18,7 +18,7 @@
#: bot.py:224 #: bot.py:224
msgid "" msgid ""
msgstr "" msgstr ""
"Project-Id-Version: mau_mau_bot 0.1\n" "Project-Id-Version: uno_bot 0.1\n"
"Report-Msgid-Bugs-To: uno@jhoeke.de\n" "Report-Msgid-Bugs-To: uno@jhoeke.de\n"
"POT-Creation-Date: 2016-05-19 22:38+0200\n" "POT-Creation-Date: 2016-05-19 22:38+0200\n"
"PO-Revision-Date: 2016-05-21 21:16+0200\n" "PO-Revision-Date: 2016-05-21 21:16+0200\n"
@ -32,8 +32,7 @@ msgstr ""
"X-Generator: Gtranslator 2.91.6\n" "X-Generator: Gtranslator 2.91.6\n"
#: bot.py:60 #: bot.py:60
msgid "" msgid "Follow these steps:\n"
"Follow these steps:\n"
"\n" "\n"
"1. Add this bot to a group\n" "1. Add this bot to a group\n"
"2. In the group, start a new game with /new or join an already running game " "2. In the group, start a new game with /new or join an already running game "
@ -49,9 +48,13 @@ msgid ""
"player takes more than 90 seconds to play, you can use /skip to skip that " "player takes more than 90 seconds to play, you can use /skip to skip that "
"player.\n" "player.\n"
"\n" "\n"
"<b>Language</b> and other settings: /settings\n"
"Other commands (only game creator):\n" "Other commands (only game creator):\n"
"/close - Close lobby\n" "/close - Close lobby\n"
"/open - Open lobby\n" "/open - Open lobby\n"
"/enable_translations - Translate relevant texts into all "
"languages spoken in a game\n"
"/disable_translations - Use English for those texts\n"
"\n" "\n"
"<b>Experimental:</b> Play in multiple groups at the same time. Press the " "<b>Experimental:</b> Play in multiple groups at the same time. Press the "
"<code>Current game: ...</code> button and select the group you want to play " "<code>Current game: ...</code> button and select the group you want to play "
@ -78,9 +81,13 @@ msgstr ""
"benutze /leave. Wenn ein Spieler länger als 90 Sekunden braucht, kannst du " "benutze /leave. Wenn ein Spieler länger als 90 Sekunden braucht, kannst du "
"ihn mit /skip überspringen.\n" "ihn mit /skip überspringen.\n"
"\n" "\n"
"<b>Sprache</b> und andere Einstellungen: /settings\n"
"Weitere Kommandos (nur Spiel-Ersteller):\n" "Weitere Kommandos (nur Spiel-Ersteller):\n"
"/close - Lobby schließen\n" "/close - Lobby schließen\n"
"/open - Lobby öffnen\n" "/open - Lobby öffnen\n"
"/enable_translations - Übersetze relevante Texte in alle im Spiel gesprochenen"
" Sprachen\n"
"/disable_translations - Verwende Englisch für diese Texte\n"
"\n" "\n"
"<b>Experimentell</b>: Spiele in mehreren Gruppen gleichzeitig. Um die " "<b>Experimentell</b>: Spiele in mehreren Gruppen gleichzeitig. Um die "
"Gruppe, in der du deine Karte spielen willst, auszuwählen, tippe auf den " "Gruppe, in der du deine Karte spielen willst, auszuwählen, tippe auf den "
@ -170,15 +177,14 @@ msgstr "Das Spiel hat bereits begonnen"
#: bot.py:281 #: bot.py:281
msgid "At least two players must /join the game before you can start it" msgid "At least two players must /join the game before you can start it"
msgstr "" msgstr "Es müssen mindestens zwei Spieler dem Spiel beitreten, bevor du es "
"Es müssen mindestens zwei Spieler dem Spiel beitreten, bevor du es starten " "starten kannst"
"kannst"
#: bot.py:297 #: bot.py:297
#, python-format #, python-format, fuzzy
msgid "" msgid "First player: {name}\n"
"First player: {name}\n" "Use /close to stop people from joining the game.\n"
"Use /close to stop people from joining the game." "Enable multi-translations with /enable_translations"
msgstr "" msgstr ""
"Erster Spieler: {name}\n" "Erster Spieler: {name}\n"
"Benutze /close, um zu verhindern, dass weitere Spieler beitreten." "Benutze /close, um zu verhindern, dass weitere Spieler beitreten."
@ -201,6 +207,17 @@ msgstr ""
msgid "Only the game creator ({name}) can do that." msgid "Only the game creator ({name}) can do that."
msgstr "Dies kann nur der Ersteller des Spiels ({name}) tun." msgstr "Dies kann nur der Ersteller des Spiels ({name}) tun."
#: bot.py:349
#, python-format
msgid "Enabled multi-translations. Disable with /disable_translations"
msgstr "Multi-Übersetzungen aktiviert. Deaktivieren mit /disable_translations"
#: bot.py:377
#, python-format
msgid "Disabled multi-translations. Enable them again with /enable_translations"
msgstr "Multi-Übersetzungen deaktiviert. Aktiviere sie wieder mit "
"/enable_translations"
#: bot.py:368 #: bot.py:368
msgid "Opened the lobby. New players may /join the game." msgid "Opened the lobby. New players may /join the game."
msgstr "Lobby geöffnet. Neue Spieler können nun beitreten." msgstr "Lobby geöffnet. Neue Spieler können nun beitreten."
@ -259,7 +276,7 @@ msgid "Cheat attempt by %s"
msgstr "{name} hat versucht zu schummeln!" msgstr "{name} hat versucht zu schummeln!"
#: bot.py:562 #: bot.py:562
msgid "Next player: " msgid "Next player: {name}"
msgstr "Nächster Spieler: {name}" msgstr "Nächster Spieler: {name}"
#: bot.py:572 #: bot.py:572

View file

@ -19,7 +19,7 @@
#, fuzzy #, fuzzy
msgid "" msgid ""
msgstr "" msgstr ""
"Project-Id-Version: mau_mau_bot 0.1\n" "Project-Id-Version: uno_bot 0.1\n"
"Report-Msgid-Bugs-To: uno@jhoeke.de\n" "Report-Msgid-Bugs-To: uno@jhoeke.de\n"
"POT-Creation-Date: 2016-05-19 22:38+0200\n" "POT-Creation-Date: 2016-05-19 22:38+0200\n"
"PO-Revision-Date: 2016-05-19 22:38+0200\n" "PO-Revision-Date: 2016-05-19 22:38+0200\n"
@ -32,6 +32,7 @@ msgstr ""
#: bot.py:60 #: bot.py:60
#, fuzzy
msgid "Follow these steps:\n" msgid "Follow these steps:\n"
"\n" "\n"
"1. Add this bot to a group\n" "1. Add this bot to a group\n"
@ -48,10 +49,13 @@ msgid "Follow these steps:\n"
"player takes more than 90 seconds to play, you can use /skip to skip that " "player takes more than 90 seconds to play, you can use /skip to skip that "
"player.\n" "player.\n"
"\n" "\n"
"<b>Language</b> and other settings: /settings\n"
"Other commands (only game creator):\n" "Other commands (only game creator):\n"
"/close - Close lobby\n" "/close - Close lobby\n"
"/open - Open lobby\n" "/open - Open lobby\n"
"\n" "/enable_translations - Translate relevant texts into all "
"languages spoken in a game\n"
"/disable_translations - Use English for those texts\n\n"
"<b>Experimental:</b> Play in multiple groups at the same time. Press the " "<b>Experimental:</b> Play in multiple groups at the same time. Press the "
"<code>Current game: ...</code> button and select the group you want to play " "<code>Current game: ...</code> button and select the group you want to play "
"a card in.\n" "a card in.\n"
@ -136,9 +140,10 @@ msgstr ""
#: bot.py:297 #: bot.py:297
#, python-format #, python-format, fuzzy
msgid "First player: {name}\n" msgid "First player: {name}\n"
"Use /close to stop people from joining the game." "Use /close to stop people from joining the game.\n"
"Enable multi-translations with /enable_translations"
msgstr "" msgstr ""
#: bot.py:321 #: bot.py:321
@ -159,6 +164,16 @@ msgstr ""
msgid "Only the game creator ({name}) can do that." msgid "Only the game creator ({name}) can do that."
msgstr "" msgstr ""
#: bot.py:349
#, python-format
msgid "Enabled multi-translations. Disable with /disable_translations"
msgstr ""
#: bot.py:377
#, python-format
msgid "Disabled multi-translations. Enable them again with /enable_translations"
msgstr ""
#: bot.py:368 #: bot.py:368
msgid "Opened the lobby. New players may /join the game." msgid "Opened the lobby. New players may /join the game."
msgstr "" msgstr ""

View file

@ -100,9 +100,10 @@ def add_draw(player, results):
Sticker( Sticker(
"draw", sticker_file_id=c.STICKERS['option_draw'], "draw", sticker_file_id=c.STICKERS['option_draw'],
input_message_content= input_message_content=
InputTextMessageContent(__('Drawing 1 card') InputTextMessageContent(__('Drawing 1 card', player.game.translate)
if n == 1 else if n == 1 else
__('Drawing {number} cards') __('Drawing {number} cards',
player.game.translate)
.format(number=n)) .format(number=n))
) )
) )
@ -120,24 +121,26 @@ def add_gameinfo(game, results):
) )
def add_pass(results): def add_pass(results, game):
"""Add option to pass""" """Add option to pass"""
results.append( results.append(
Sticker( Sticker(
"pass", sticker_file_id=c.STICKERS['option_pass'], "pass", sticker_file_id=c.STICKERS['option_pass'],
input_message_content=InputTextMessageContent(__('Pass')) input_message_content=InputTextMessageContent(__('Pass',
game.translate))
) )
) )
def add_call_bluff(results): def add_call_bluff(results, game):
"""Add option to call a bluff""" """Add option to call a bluff"""
results.append( results.append(
Sticker( Sticker(
"call_bluff", "call_bluff",
sticker_file_id=c.STICKERS['option_bluff'], sticker_file_id=c.STICKERS['option_bluff'],
input_message_content= input_message_content=
InputTextMessageContent(__("I'm calling your bluff!")) InputTextMessageContent(__("I'm calling your bluff!",
game.translate))
) )
) )

View file

@ -39,9 +39,13 @@ help_text = ("Follow these steps:\n\n"
"Players can join the game at any time. To leave a game, " "Players can join the game at any time. To leave a game, "
"use /leave. If a player takes more than 90 seconds to play, " "use /leave. If a player takes more than 90 seconds to play, "
"you can use /skip to skip that player.\n\n" "you can use /skip to skip that player.\n\n"
"<b>Language</b> and other settings: /settings\n"
"Other commands (only game creator):\n" "Other commands (only game creator):\n"
"/close - Close lobby\n" "/close - Close lobby\n"
"/open - Open lobby\n\n" "/open - Open lobby\n"
"/enable_translations - Translate relevant texts into all "
"languages spoken in a game\n"
"/disable_translations - Use English for those texts\n\n"
"<b>Experimental:</b> Play in multiple groups at the same time. " "<b>Experimental:</b> Play in multiple groups at the same time. "
"Press the <code>Current game: ...</code> button and select the " "Press the <code>Current game: ...</code> button and select the "
"group you want to play a card in.\n" "group you want to play a card in.\n"

View file

@ -29,6 +29,7 @@ from telegram.ext.dispatcher import run_async
import locales import locales
from database import db_session from database import db_session
from user_setting import UserSetting from user_setting import UserSetting
from shared_vars import gm
strategy = PackageStrategy('unobot', locales) strategy = PackageStrategy('unobot', locales)
application = registry.register(strategy) application = registry.register(strategy)
@ -38,27 +39,28 @@ logger = logging.getLogger(__name__)
TIMEOUT = 2.5 TIMEOUT = 2.5
def __(string): def __(string, multi_translate):
"""Translates text into all locales on the stack""" """Translates text into all locales on the stack"""
translations = list() translations = list()
locales = list() locales = list()
while True: if not multi_translate:
translation = _(string) _.push('en_US')
translations.append(_(string))
if translation not in translations:
translations.append(translation)
l = _.code
_.pop() _.pop()
if l is None: else:
break while _.code:
else: translation = _(string)
locales.append(l)
for l in reversed(locales): if translation not in translations:
_.push(l) translations.append(translation)
locales.append(_.code)
_.pop()
for l in reversed(locales):
_.push(l)
return '\n'.join(translations) # TODO return '\n'.join(translations) # TODO
@ -126,12 +128,16 @@ def user_locale(func):
@wraps(func) @wraps(func)
@db_session @db_session
def wrapped(bot, update, *pargs, **kwargs): def wrapped(bot, update, *pargs, **kwargs):
user, chat = _user_chat_from_update(update)
with db_session: with db_session:
us = UserSetting.get(id=update.message.from_user.id) us = UserSetting.get(id=user.id)
if us: if us:
_.push(us.lang) _.push(us.lang)
else: else:
_.push('en_US') _.push('en_US')
result = func(bot, update, *pargs, **kwargs) result = func(bot, update, *pargs, **kwargs)
_.pop() _.pop()
return result return result
@ -141,15 +147,50 @@ def user_locale(func):
def game_locales(func): def game_locales(func):
@wraps(func) @wraps(func)
@db_session @db_session
def wrapped(*pargs, **kwargs): def wrapped(bot, update, *pargs, **kwargs):
num_locales = 0 user, chat = _user_chat_from_update(update)
for loc in ('en_US', 'de_DE'): # TODO: Get user locales from Database player = gm.player_for_user_in_chat(user, chat)
_.push(loc) locales = list()
num_locales += 1
result = func(*pargs, **kwargs) if player:
for player in player.game.players:
us = UserSetting.get(id=player.user.id)
for i in range(num_locales): if us:
loc = us.lang
else:
loc = 'en_US'
if loc in locales:
continue
_.push(loc)
locales.append(loc)
result = func(bot, update, *pargs, **kwargs)
for i in locales:
_.pop() _.pop()
return result return result
return wrapped return wrapped
def _user_chat_from_update(update):
try:
user = update.message.from_user
chat = update.message.chat
except (NameError, AttributeError):
try:
user = update.inline_query.from_user
chat = gm.userid_current[user.id].game.chat
except KeyError:
chat = None
except (NameError, AttributeError):
try:
user = update.chosen_inline_result.from_user
chat = gm.userid_current[user.id].game.chat
except (NameError, AttributeError):
chat = None
return user, chat