diff options
author | kali <kali@leap.se> | 2012-12-21 10:28:46 +0900 |
---|---|---|
committer | kali <kali@leap.se> | 2012-12-21 10:28:46 +0900 |
commit | ec0fc05e3918782dbb29f9f6901c0de22419134d (patch) | |
tree | e5e49bc7601e08cc604b54c1388c0fd0241f980b /src/leap/util | |
parent | ea4a3b3396caeeaa2be4dc46aecf386a56a522c9 (diff) |
magic translatable objects
Diffstat (limited to 'src/leap/util')
-rw-r--r-- | src/leap/util/tests/test_translations.py | 22 | ||||
-rw-r--r-- | src/leap/util/translations.py | 43 |
2 files changed, 56 insertions, 9 deletions
diff --git a/src/leap/util/tests/test_translations.py b/src/leap/util/tests/test_translations.py new file mode 100644 index 00000000..794daeba --- /dev/null +++ b/src/leap/util/tests/test_translations.py @@ -0,0 +1,22 @@ +import unittest + +from leap.util import translations + + +class TrasnlationsTestCase(unittest.TestCase): + """ + tests for translation functions and classes + """ + + def setUp(self): + self.trClass = translations.LEAPTranslatable + + def test_trasnlatable(self): + tr = self.trClass({"en": "house", "es": "casa"}) + eq = self.assertEqual + eq(tr.tr(to="es"), "casa") + eq(tr.tr(to="en"), "house") + + +if __name__ == "__main__": + unittest.main() diff --git a/src/leap/util/translations.py b/src/leap/util/translations.py index c06aa947..14b8c020 100644 --- a/src/leap/util/translations.py +++ b/src/leap/util/translations.py @@ -1,6 +1,10 @@ import inspect +import logging from PyQt4.QtCore import QCoreApplication +from PyQt4.QtCore import QLocale + +logger = logging.getLogger(__name__) """ here I could not do all that I wanted. @@ -20,15 +24,12 @@ I guess we could generate the xml for ourselves as a last recourse. qtTranslate = QCoreApplication.translate -class LEAPTr: - pass - - -def translate(*args): +def translate(*args, **kwargs): """ + our magic function. translate(Context, text, comment) """ - print 'translating...' + #print 'translating...' klsname = None try: # get class value from instance @@ -37,7 +38,7 @@ def translate(*args): self = inspect.getargvalues(prev_frame).locals.get('self') if self: # XXX will this work with QObject wrapper?? - if isinstance(LEAPTr, self) and hasattr(self, 'tr'): + if isinstance(LEAPTranslatable, self) and hasattr(self, 'tr'): print "we got a self in base class" return self.tr(*args) @@ -45,9 +46,10 @@ def translate(*args): # but this is useless, the parser # has already got the context. klsname = self.__class__.__name__ - print 'KLSNAME -- ', klsname + #print 'KLSNAME -- ', klsname except: - print 'error getting stack frame' + logger.error('error getting stack frame') + #print 'error getting stack frame' if klsname: nargs = (klsname,) + args @@ -56,3 +58,26 @@ def translate(*args): else: nargs = ('default', ) + args return qtTranslate(*nargs) + + +class LEAPTranslatable(dict): + """ + An extended dict that implements a .tr method + so it can be translated on the fly by our + magic translate method + """ + + try: + locale = str(QLocale.system().name()).split('_')[0] + except: + logger.warning("could not get system locale!") + print "could not get system locale!" + locale = "en" + + def tr(self, to=None): + if not to: + to = self.locale + _tr = self.get(to, None) + if not _tr: + _tr = self.get("en", None) + return _tr |