commit dad5531d676b97e6b8f6bb4727e8aa55cf6ac57c
parent f5896353855e775c7b6f61fd090e86eedaf0fd2a
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sun, 15 Jul 2012 15:17:03 +0200
make translation work for web and irc
Diffstat:
4 files changed, 28 insertions(+), 20 deletions(-)
diff --git a/localization.py b/localization.py
@@ -0,0 +1,22 @@
+import gettext
+import locale
+import logging
+
+def init_locale(loc=None):
+  locale.setlocale(locale.LC_ALL, '') # use user's preferred locale
+  # take first two characters of country code
+  if not loc:
+    loc = locale.getlocale()
+  else:
+    loc = [loc]
+  filename = "res/messages_%s.mo" % loc[0][0:2]
+
+  try:
+    logging.debug("Opening message file %s for locale %s", filename, loc[0])
+    trans = gettext.GNUTranslations(open(filename, "rb"))
+  except IOError:
+    logging.debug("Locale not found. Using default messages")
+    trans = gettext.NullTranslations()
+
+  trans.install()
+
diff --git a/plint.py b/plint.py
@@ -1,8 +1,6 @@
 #!/usr/bin/python3 -uO
 
-import gettext
-import locale
-import logging
+import localization
 import sys
 import template
 
@@ -21,24 +19,8 @@ def run():
       ok = False
   return ok
 
-def init_localization():
-  '''prepare l10n'''
-  locale.setlocale(locale.LC_ALL, '') # use user's preferred locale
-  # take first two characters of country code
-  loc = locale.getlocale()
-  filename = "res/messages_%s.mo" % locale.getlocale()[0][0:2]
-
-  try:
-    logging.debug("Opening message file %s for locale %s", filename, loc[0])
-    trans = gettext.GNUTranslations(open(filename, "rb"))
-  except IOError:
-    logging.debug("Locale not found. Using default messages")
-    trans = gettext.NullTranslations()
-
-  trans.install()
-
 if __name__ == '__main__':
-  init_localization()
+  localization.init_locale()
   if len(sys.argv) < 2 or len(sys.argv) > 3:
     print(_("Usage: %s TEMPLATE [OCONTEXT]") % sys.argv[0], file=sys.stderr)
     print(_("Check stdin according to TEMPLATE, report errors on stdout"),
diff --git a/plint_irc.py b/plint_irc.py
@@ -1,5 +1,6 @@
 #!/usr/bin/python3 -uO
 
+import localization
 import re
 import sys
 import rhyme
@@ -114,6 +115,7 @@ f.close()
 print("ready", file=sys.stderr)
 
 def run():
+  localization.init_locale()
   global lbuf
   while True:
     line = sys.stdin.readline()
diff --git a/plint_web.py b/plint_web.py
@@ -1,6 +1,7 @@
 #!/usr/bin/python3 -O
 #encoding: utf8
 
+import localization
 import re
 import template
 from bottle import run, Bottle, request, static_file
@@ -80,6 +81,7 @@ def q():
       'template': request.forms.get('template'),
       'lang': get_locale(),
     }
+  localization.init_locale(get_locale())
   d['poem'] = re.sub(r'<>&', '', d['poem'])
   poem = check(d['poem'])
   if not poem: