Browse files

Bug 57: Added preference to control language setting, radio buttons f…

…or player to set pref
  • Loading branch information...
1 parent 680aa20 commit b7479e0fa2471e9cf95efc80b9422626777a60af @jonoxia committed Jan 16, 2012
Showing with 94 additions and 17 deletions.
  1. +1 −0 database_tables.py
  2. +26 −4 l10n.py
  3. +15 −9 listlevels.py
  4. +3 −0 set-player-data.py
  5. +6 −2 strings.json
  6. +1 −0 templates/lang-radio-button.html
  7. +1 −0 templates/list-levels.html
  8. +41 −2 webserver_utils.py
View
1 database_tables.py
@@ -12,6 +12,7 @@ class Player(SQLObject):
name = StringCol()
session = StringCol()
avatarURL = StringCol()
+ langPref = StringCol()
class Level(SQLObject):
name = StringCol()
View
30 l10n.py
@@ -2,12 +2,34 @@
m_all_strings = False
+def _loadStringDict():
+ global m_all_strings
+ # Localization - read srings.json only once
+ string_file = open( "strings.json", "r")
+ m_all_strings= simplejson.loads(string_file.read().decode("utf-8"))
+ string_file.close()
+
def getStrings():
global m_all_strings
if m_all_strings == False:
- # Localization - read srings.json only once
- string_file = open( "strings.json", "r")
- m_all_strings= simplejson.loads(string_file.read().decode("utf-8"))
- string_file.close()
+ _loadStringDict()
+
return m_all_strings["jp"] # TODO get current language from settings
+def makeLangSettings(selectedLang):
+ global m_all_strings
+ if m_all_strings == False:
+ _loadStringDict()
+
+ settingsHtml = ""
+
+ for key in m_all_strinngs.keys():
+ dict = {"lang_code": key,
+ "checked": "",
+ "language": m_all_strings[selectedLang]["language_%s" % key]}
+ if selectedLang == key:
+ dict["checked"] = "checked=\"checked\""
+
+ settingsHtml += render_template_file("lang-radio-button.html", dict)
+
+ return settingsHtml
View
24 listlevels.py
@@ -8,7 +8,7 @@
import math
from database_tables import Level, LevelObject, Score
-from webserver_utils import render_template_file, verify_id
+from webserver_utils import render_template_file, verify_id, make_lang_settings
from sqlobject import OR
def formatTime(ms):
@@ -19,6 +19,7 @@ def formatTime(ms):
return "%d:%02d.%02d" % (m, s, ms)
+
def printList(player):
print "Content-type: text/html"
print
@@ -34,28 +35,30 @@ def printList(player):
edit_link = ""
if level.creator != None:
if level.creator == player:
- creator = "You"
+ creator = "You" # TODO l10n
else:
creator = level.creator.name
else:
- creator = "Nobody"
+ creator = "Nobody" # TODO l10n
scores = Score.selectBy(level = level)
best = ""
if (scores.count() > 0):
+ # TODO l10n
best = "%s by %s with %d trinkets" % (formatTime(scores[0].completionTime),
scores[0].player.name,
scores[0].trinkets)
else:
- best = "Nobody Yet!"
+ best = "Nobody Yet!" # TODO l10n
scores = Score.selectBy(player = player, level = level)
your_time = ""
if (scores.count() > 0):
+ # TODO l10n
your_time = "%s with %d trinkets" % (formatTime(scores[0].completionTime),
scores[0].trinkets)
if level.creator == player:
if level.published:
- published = "Yes"
+ published = "Yes" # TODO l10n
else:
published = "No"
your_list += render_template_file( "list-my-level-row.html",
@@ -71,11 +74,14 @@ def printList(player):
"creator": creator,
"best": best,
"yourtime": your_time} )
+
+ sub_words = {"published_list": published_list,
+ "your_list": your_list,
+ "player": player.name,
+ "avatarURL": player.avatarURL,
+ "lang_radio_buttons": make_lang_settings(player.langPref)}
- print render_template_file( "list-levels.html", {"published_list": published_list,
- "your_list": your_list,
- "player": player.name,
- "avatarURL": player.avatarURL})
+ print render_template_file( "list-levels.html", sub_words)
if __name__ == "__main__":
cgitb.enable()
View
3 set-player-data.py
@@ -8,9 +8,12 @@
q = cgi.FieldStorage()
playerName = q.getfirst("playerName", "")
avatarURL = q.getfirst("avatarURL", "")
+langPref = q.getfirst("langPref", "")
player = verify_id()
if (playerName != "" and avatarURL != "" and player != None):
player.name = playerName
player.avatarURL = avatarURL
+ if langPref != "":
+ player.langPref = langPref
print_redirect("listlevels.py")
View
8 strings.json
@@ -51,7 +51,9 @@
"_scroll_tool_exp": "(drag to scroll level)",
"_start_loc_tool": "Start Location Tool",
"_goal_loc_tool": "Goal Location Tool",
- "_publish": "Make this level visible to others"
+ "_publish": "Make this level visible to others",
+ "_lang_pref": "Language",
+ "_this_language": "English"
},
"jp": {
"_runjumpbuild": "ランジャンプブイルド",
@@ -105,7 +107,9 @@
"_scroll_tool_exp": "(レベルをスクロールするためにドラッグして)",
"_start_loc_tool": "始めを置くツール",
"_goal_loc_tool": "目的を置くツール",
- "_publish": "このレベルを公開する"
+ "_publish": "このレベルを公開する",
+ "_lang_pref": "Kotoba",
+ "_this_language": "Nihongo"
}
}
View
1 templates/lang-radio-button.html
@@ -0,0 +1 @@
+<input type ="radio" name="langPref" value="${lang_code}" ${checked}/> ${language}
View
1 templates/list-levels.html
@@ -27,6 +27,7 @@
<form action="set-player-data.py" method="post">
<h2>${_your_player_data}:</h2>
${_player_name}: <input type="text" name="playerName" value="${player}"/><br/>
+ ${_lang_pref}: ${lang_radio_buttons}<br/>
${_your_avatar}: <input type="text" name="avatarURL" size="60" value="${avatarURL}"/><br/>
<img src="${avatarURL}"/>
<input type="submit" value="${_save_changes}"></input>
View
43 webserver_utils.py
@@ -6,7 +6,6 @@
import string
import simplejson
import re
-import l10n
from platformer_config import TEMPLATE_DIR
from database_tables import Player
@@ -18,7 +17,8 @@ def render_template_file( filename, substitutionDict ):
localizations = re.findall(r"\$\{(\_[^}]*)\}", template_file_contents)
- loc_strings = l10n.getStrings()
+ player = verify_id()
+ loc_strings = getStrings(player)
for key in localizations:
substitutionDict[key] = loc_strings[key].encode("utf-8")
@@ -58,3 +58,42 @@ def verify_id():
# If verification fails, kick 'em back out to index.html
print_redirect("index.html")
sys.exit(1)
+
+m_all_strings = False
+
+def _loadStringDict():
+ global m_all_strings
+ # Localization - read srings.json only once
+ string_file = open( "strings.json", "r")
+ m_all_strings= simplejson.loads(string_file.read().decode("utf-8"))
+ string_file.close()
+
+
+def getStrings(player):
+ global m_all_strings
+ if m_all_strings == False:
+ _loadStringDict()
+
+ lang = player.langPref
+ if lang == "" or (not lang in m_all_strings.keys()):
+ lang = "en"
+
+ return m_all_strings[lang]
+
+def make_lang_settings(selectedLang):
+ global m_all_strings
+ if m_all_strings == False:
+ _loadStringDict()
+
+ settingsHtml = ""
+
+ for key in m_all_strings.keys():
+ dict = {"lang_code": key.encode("utf-8"),
+ "checked": "",
+ "language": (m_all_strings[key]["_this_language"]).encode("utf-8")}
+ if selectedLang == key.encode("utf-8"):
+ dict["checked"] = "checked=\"checked\""
+
+ settingsHtml += render_template_file("lang-radio-button.html", dict)
+
+ return settingsHtml

0 comments on commit b7479e0

Please sign in to comment.