diff --git a/bot.py b/bot.py
index 9e513b2..0566d21 100644
--- a/bot.py
+++ b/bot.py
@@ -20,6 +20,7 @@
import logging
from datetime import datetime
+from functools import wraps
from random import randint
from telegram import ParseMode, Message, Chat, InlineKeyboardMarkup, \
@@ -40,8 +41,7 @@ from utils import display_name
import card as c
from errors import (NoGameInChatError, LobbyClosedError, AlreadyJoinedError,
NotEnoughPlayersError, DeckEmptyError)
-from database import db_session, user_locale
-from utils import _
+from utils import _, __
TIMEOUT = 2.5
@@ -92,6 +92,32 @@ source_text = ("This bot is Free Software and licensed under the AGPL. "
"https://github.com/jh0ker/mau_mau_bot")
+def user_locale(func):
+ @wraps(func)
+ def wrapped(*pargs, **kwargs):
+ _.push('de_DE') # TODO: Get user locale from Database
+ result = func(*pargs, **kwargs)
+ _.pop()
+ return result
+ return wrapped
+
+
+def game_locales(func):
+ @wraps(func)
+ def wrapped(*pargs, **kwargs):
+ num_locales = 0
+ for loc in ('en_US', 'de_DE'): # TODO: Get user locales from Database
+ _.push(loc)
+ num_locales += 1
+
+ result = func(*pargs, **kwargs)
+
+ for i in range(num_locales):
+ _.pop()
+ return result
+ return wrapped
+
+
@run_async
def send_async(bot, *args, **kwargs):
"""Send a message asynchronously"""
@@ -169,8 +195,8 @@ def join_game(bot, update):
except DeckEmptyError:
send_async(bot, chat.id,
- text=_("There are not enough cards left in the deck for new "
- "players to join."),
+ text=_("There are not enough cards left in the deck for "
+ "new players to join."),
reply_to_message_id=update.message.message_id)
else:
@@ -206,11 +232,11 @@ def leave_game(bot, update):
except NotEnoughPlayersError:
gm.end_game(chat, user)
- send_async(bot, chat.id, text=_("Game ended!"))
+ send_async(bot, chat.id, text=__("Game ended!"))
else:
send_async(bot, chat.id,
- text=_("Okay. Next Player: {name}").format(
+ text=__("Okay. Next Player: {name}").format(
name=display_name(game.current_player.user)),
reply_to_message_id=update.message.message_id)
@@ -251,7 +277,7 @@ def select_game(bot, update):
timeout=TIMEOUT)
-@user_locale
+@game_locales
def status_update(bot, update):
"""Remove player from game if user leaves the group"""
chat = update.message.chat
@@ -268,12 +294,13 @@ def status_update(bot, update):
pass
except NotEnoughPlayersError:
gm.end_game(chat, user)
- send_async(bot, chat.id, text=_("Game ended!"))
+ send_async(bot, chat.id, text=__("Game ended!"))
else:
- send_async(bot, chat.id, text=_("Removing {name} from the game")
+ send_async(bot, chat.id, text=__("Removing {name} from the game")
.format(name=display_name(user)))
+@game_locales
@user_locale
def start_game(bot, update, args):
"""Handler for the /start command"""
@@ -310,9 +337,9 @@ def start_game(bot, update, args):
timeout=TIMEOUT)
bot.sendMessage(chat.id,
- text=_("First player: {name}\n"
- "Use /close to stop people from "
- "joining the game.")
+ text=__("First player: {name}\n"
+ "Use /close to stop people from "
+ "joining the game.")
.format(
name=display_name(game.current_player.user)
),
@@ -398,6 +425,7 @@ def open_game(bot, update):
return
+@game_locales
@user_locale
def skip_player(bot, update):
"""Handler for the /skip command"""
@@ -433,9 +461,9 @@ def skip_player(bot, update):
pass
send_async(bot, chat.id,
- text=_("Waiting time to skip this player has "
- "been reduced to {time} seconds.\n"
- "Next player: {name}")
+ text=__("Waiting time to skip this player has "
+ "been reduced to {time} seconds.\n"
+ "Next player: {name}")
.format(time=skipped_player.waiting_time,
name=display_name(next_player.user)))
game.turn()
@@ -444,17 +472,17 @@ def skip_player(bot, update):
try:
gm.leave_game(skipped_player.user, chat)
send_async(bot, chat.id,
- text=_("{name1} was skipped four times in a row "
- "and has been removed from the game.\n"
- "Next player: {name2}")
+ text=__("{name1} was skipped four times in a row "
+ "and has been removed from the game.\n"
+ "Next player: {name2}")
.format(name1=display_name(skipped_player.user),
name2=display_name(next_player.user)))
except NotEnoughPlayersError:
send_async(bot, chat.id,
- text=_("{name} was skipped four times in a row "
- "and has been removed from the game.\n"
- "The game ended.")
+ text=__("{name} was skipped four times in a row "
+ "and has been removed from the game.\n"
+ "The game ended.")
.format(name=display_name(skipped_player.user)))
gm.end_game(chat.id, skipped_player.user)
@@ -482,6 +510,7 @@ def news(bot, update):
disable_web_page_preview=True)
+@game_locales
@user_locale
def reply_to_query(bot, update):
"""
@@ -543,6 +572,7 @@ def reply_to_query(bot, update):
switch_pm_text=switch, switch_pm_parameter='select')
+@game_locales
@user_locale
def process_result(bot, update):
"""
@@ -570,7 +600,7 @@ def process_result(bot, update):
return
elif int(anti_cheat) != last_anti_cheat:
send_async(bot, chat.id,
- text=_("Cheat attempt by {name}")
+ text=__("Cheat attempt by {name}")
.format(name=display_name(player.user)))
return
elif result_id == 'call_bluff':
@@ -589,7 +619,7 @@ def process_result(bot, update):
if game in gm.chatid_games.get(chat.id, list()):
send_async(bot, chat.id,
- text=_("Next player: {name}")
+ text=__("Next player: {name}")
.format(name=display_name(game.current_player.user)))
@@ -600,8 +630,8 @@ def reset_waiting_time(bot, player):
if player.waiting_time < 90:
player.waiting_time = 90
send_async(bot, chat.id,
- text=_("Waiting time for {name} has been reset to 90 "
- "seconds").format(name=display_name(player.user)))
+ text=__("Waiting time for {name} has been reset to 90 "
+ "seconds").format(name=display_name(player.user)))
def do_play_card(bot, player, result_id):
@@ -616,15 +646,15 @@ def do_play_card(bot, player, result_id):
send_async(bot, chat.id, text=_("Please choose a color"))
if len(player.cards) == 1:
- send_async(bot, chat.id, text=_("UNO!"))
+ send_async(bot, chat.id, text="UNO!")
if len(player.cards) == 0:
send_async(bot, chat.id,
- text=_("{name} won!").format(name=user.first_name))
+ text=__("{name} won!").format(name=user.first_name))
try:
gm.leave_game(user, chat)
except NotEnoughPlayersError:
- send_async(bot, chat.id, text=_("Game ended!"))
+ send_async(bot, chat.id, text=__("Game ended!"))
gm.end_game(chat, user)
if botan:
@@ -642,7 +672,7 @@ def do_draw(bot, player):
player.draw()
except DeckEmptyError:
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."))
if (game.last_card.value == c.DRAW_TWO or
game.last_card.special == c.DRAW_FOUR) and \
@@ -657,26 +687,26 @@ def do_call_bluff(bot, player):
if player.prev.bluffing:
send_async(bot, chat.id,
- text=_("Bluff called! Giving 4 cards to {name}")
+ text=__("Bluff called! Giving 4 cards to {name}")
.format(name=player.prev.user.first_name))
try:
player.prev.draw()
except DeckEmptyError:
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."))
else:
game.draw_counter += 2
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}")
.format(name1=player.prev.user.first_name,
name2=player.user.first_name))
try:
player.draw()
except DeckEmptyError:
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.turn()
diff --git a/database.py b/database.py
index 509dbbe..38b5cca 100644
--- a/database.py
+++ b/database.py
@@ -18,21 +18,7 @@
# along with this program. If not, see .
-from functools import wraps
-
from pony.orm import Database, db_session, Optional, Required, Set, PrimaryKey
-from utils import _
-
# Database singleton
db = Database()
-
-
-def user_locale(func):
- @wraps(func)
- def wrapped(*pargs, **kwargs):
- _.push('de_DE') # TODO: Get user locale from Database
- result = func(*pargs, **kwargs)
- _.pop()
- return result
- return wrapped
diff --git a/results.py b/results.py
index 69c81e9..47fa31b 100644
--- a/results.py
+++ b/results.py
@@ -26,7 +26,7 @@ from telegram import InlineQueryResultArticle, InputTextMessageContent, \
InlineQueryResultCachedSticker as Sticker
import card as c
-from utils import display_color, display_name, list_subtract, _
+from utils import display_color, display_name, list_subtract, _, __
def add_choose_color(results):
@@ -61,7 +61,7 @@ def add_other_cards(playable, player, results, game):
def player_list(game):
"""Generate list of player strings"""
- return ["{name} ({number} cards)"
+ return [_("{name} ({number} cards)")
.format(name=player.user.first_name, number=len(player.cards))
for player in game.players]
@@ -100,9 +100,9 @@ def add_draw(player, results):
Sticker(
"draw", sticker_file_id=c.STICKERS['option_draw'],
input_message_content=
- InputTextMessageContent(_('Drawing 1 card')
+ InputTextMessageContent(__('Drawing 1 card')
if n == 1 else
- _('Drawing {number} cards')
+ __('Drawing {number} cards')
.format(number=n))
)
)
@@ -125,7 +125,7 @@ def add_pass(results):
results.append(
Sticker(
"pass", sticker_file_id=c.STICKERS['option_pass'],
- input_message_content=InputTextMessageContent(_('Pass'))
+ input_message_content=InputTextMessageContent(__('Pass'))
)
)
@@ -137,7 +137,7 @@ def add_call_bluff(results):
"call_bluff",
sticker_file_id=c.STICKERS['option_bluff'],
input_message_content=
- InputTextMessageContent(_("I'm calling your bluff!"))
+ InputTextMessageContent(__("I'm calling your bluff!"))
)
)
diff --git a/utils.py b/utils.py
index cb4b998..66fa333 100644
--- a/utils.py
+++ b/utils.py
@@ -18,6 +18,8 @@
# along with this program. If not, see .
+import logging
+
from flufl.i18n import registry
from flufl.i18n import PackageStrategy
@@ -27,6 +29,32 @@ import locales
strategy = PackageStrategy('unobot', locales)
application = registry.register(strategy)
_ = application._
+logger = logging.getLogger(__name__)
+
+
+def __(string):
+ """Translates text into all locales on the stack"""
+ translations = list()
+ locales = list()
+
+ while True:
+ translation = _(string)
+
+ if translation not in translations:
+ translations.append(translation)
+
+ l = _.code
+ _.pop()
+
+ if l is None:
+ break
+ else:
+ locales.append(l)
+
+ for l in reversed(locales):
+ _.push(l)
+
+ return '\n'.join(translations) # TODO
def list_subtract(list1, list2):