From b0c3c9194447f20306111a31ee5a6d4828fed158 Mon Sep 17 00:00:00 2001 From: kali Date: Fri, 21 Dec 2012 07:43:16 +0900 Subject: readme typos, updated translation docs --- src/leap/util/translations.py | 58 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/leap/util/translations.py (limited to 'src/leap/util/translations.py') diff --git a/src/leap/util/translations.py b/src/leap/util/translations.py new file mode 100644 index 00000000..c06aa947 --- /dev/null +++ b/src/leap/util/translations.py @@ -0,0 +1,58 @@ +import inspect + +from PyQt4.QtCore import QCoreApplication + +""" +here I could not do all that I wanted. +the context is not getting passed to the xml file. +Looks like pylupdate4 is somehow a hack that does not +parse too well the python ast. +I guess we could generate the xml for ourselves as a last recourse. +""" + +# XXX BIG NOTE: +# RESIST the temptation to get the translate function +# more compact, or have the Context argument passed as a variable +# It HAS to be explicit due to how the pylupdate parser +# works. + + +qtTranslate = QCoreApplication.translate + + +class LEAPTr: + pass + + +def translate(*args): + """ + translate(Context, text, comment) + """ + print 'translating...' + klsname = None + try: + # get class value from instance + # using live object inspection + prev_frame = inspect.stack()[1][0] + 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'): + print "we got a self in base class" + return self.tr(*args) + + # Trying to get the class name + # but this is useless, the parser + # has already got the context. + klsname = self.__class__.__name__ + print 'KLSNAME -- ', klsname + except: + print 'error getting stack frame' + + if klsname: + nargs = (klsname,) + args + return qtTranslate(*nargs) + + else: + nargs = ('default', ) + args + return qtTranslate(*nargs) -- cgit v1.2.3 From ec0fc05e3918782dbb29f9f6901c0de22419134d Mon Sep 17 00:00:00 2001 From: kali Date: Fri, 21 Dec 2012 10:28:46 +0900 Subject: magic translatable objects --- src/leap/util/translations.py | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) (limited to 'src/leap/util/translations.py') 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 -- cgit v1.2.3 From 93d5a8cd1ec55c725d5931d86989ea11ac2db844 Mon Sep 17 00:00:00 2001 From: kali Date: Wed, 9 Jan 2013 04:20:15 +0900 Subject: fix provider label translation --- src/leap/util/translations.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/leap/util/translations.py') diff --git a/src/leap/util/translations.py b/src/leap/util/translations.py index 14b8c020..80daa10d 100644 --- a/src/leap/util/translations.py +++ b/src/leap/util/translations.py @@ -17,7 +17,7 @@ I guess we could generate the xml for ourselves as a last recourse. # XXX BIG NOTE: # RESIST the temptation to get the translate function # more compact, or have the Context argument passed as a variable -# It HAS to be explicit due to how the pylupdate parser +# Its name HAS to be explicit due to how the pylupdate parser # works. @@ -29,18 +29,19 @@ def translate(*args, **kwargs): our magic function. translate(Context, text, comment) """ - #print 'translating...' + if len(args) == 1: + obj = args[0] + if isinstance(obj, LEAPTranslatable) and hasattr(obj, 'tr'): + return obj.tr() + klsname = None try: # get class value from instance # using live object inspection prev_frame = inspect.stack()[1][0] - self = inspect.getargvalues(prev_frame).locals.get('self') + locals_ = inspect.getargvalues(prev_frame).locals + self = locals_.get('self') if self: - # XXX will this work with QObject wrapper?? - if isinstance(LEAPTranslatable, self) and hasattr(self, 'tr'): - print "we got a self in base class" - return self.tr(*args) # Trying to get the class name # but this is useless, the parser @@ -49,7 +50,6 @@ def translate(*args, **kwargs): #print 'KLSNAME -- ', klsname except: logger.error('error getting stack frame') - #print 'error getting stack frame' if klsname: nargs = (klsname,) + args -- cgit v1.2.3 From 239a95a65055a5b7128894faf30938496382fbe1 Mon Sep 17 00:00:00 2001 From: kali Date: Wed, 9 Jan 2013 05:39:28 +0900 Subject: fix exception i18n --- src/leap/util/translations.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/leap/util/translations.py') diff --git a/src/leap/util/translations.py b/src/leap/util/translations.py index 80daa10d..d782cfe4 100644 --- a/src/leap/util/translations.py +++ b/src/leap/util/translations.py @@ -51,13 +51,14 @@ def translate(*args, **kwargs): except: logger.error('error getting stack frame') - if klsname: + if klsname and len(args) == 1: nargs = (klsname,) + args return qtTranslate(*nargs) else: - nargs = ('default', ) + args - return qtTranslate(*nargs) + #nargs = ('default', ) + args + #import pdb4qt; pdb4qt.set_trace() + return qtTranslate(*args) class LEAPTranslatable(dict): -- cgit v1.2.3 From ff59da55ef9a176b36cef19d67e7ec363bf5d739 Mon Sep 17 00:00:00 2001 From: kali Date: Thu, 24 Jan 2013 02:30:00 +0900 Subject: wizard rephrasing & punctuation --- src/leap/util/translations.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/leap/util/translations.py') diff --git a/src/leap/util/translations.py b/src/leap/util/translations.py index d782cfe4..f55c8fba 100644 --- a/src/leap/util/translations.py +++ b/src/leap/util/translations.py @@ -56,8 +56,6 @@ def translate(*args, **kwargs): return qtTranslate(*nargs) else: - #nargs = ('default', ) + args - #import pdb4qt; pdb4qt.set_trace() return qtTranslate(*args) -- cgit v1.2.3