Source

trac-dvbcronrecording-plugin / src / dvbcronrecording / translate.py

Full commit
# -*- encoding: utf-8 -*-
import pkg
import os.path

def translation(self, domain, req):
    return Translate(domain, req.locale)

class Translate:
    def __init__(self, domain, locale = None, languages = None):
        self.locale = locale
        langs = []
        if self.locale: langs = [ self.locale.language ]
        if not langs: langs = [ "C" ]
        if languages: langs = languages
        import gettext
        self.t = gettext.NullTranslations()
        try: 
            self.t = gettext.translation(domain, pkg.resource_filename(__name__, "locale"), languages = langs)
            self.problem = "[%s]" % langs
        except Exception, e:
            self.problem = str(e)
    def __call__(self, msg, **kwargs):
        if msg == "?problem": return self.problem 
        if msg == "**weekdaynames": return self.weekdaynames()
        if msg == "**prioritynames": return self.prioritynames()
        if msg == "**statusnames": return self.statusnames()
        trans = self.get(msg)
        if kwargs: return trans % kwargs
        return trans
    def gettext(self, msg):
        return self.get(msg)
    def get(self, msg, default = None):
        trans = self.t.ugettext(msg)
        if trans in [ msg ] and default is not None: return default
        if isinstance(trans, str): return unicode(trans, "utf-8")
        return trans
    def nget(self, singular, plural, n, default = None):
        trans = self.t.ungettext(singular, plural, n)
        if trans in [singular, plural] and default is not None: return default
        if isinstance(trans, str): return unicode(trans, "utf-8")
        return trans
    # in python struct_time.tm_wday -> monday is 0
    de_weekdaynames = { None:"--", 0:"Mo", 1:"Di", 2:"Mi", 3:"Do", 4:"Fr", 5:"Sa", 6:"So" }
    def weekdaynames(self):
        if not self.locale or self.get("foo") == "foo":
            return self.de_weekdaynames
        return self.locale.days["format"]["abbreviated"]
    def prioritynames(self):
        return { None : self.get("-.prio", "--"),
              0 : self.get("0.prio", u"hmm"),  4: self.get("4.prio", u"muss"),
              1 : self.get("1.prio", u"egal"), 5: self.get("5.prio", u"muss!"),
              2 : self.get("2.prio", u"kann"), 6: self.get("6.prio", u"top!"),
              3 : self.get("3.prio", u"soll"), 7: self.get("7.prio", u"über!") }
    de_statusnames = { None:"--", "no":"no", "ok":"ok"}
    def statusnames(self):
        return { None :self.get("-.status",  u"--"),
                "no" : self.get("no.status", u"no"),
                "ok" : self.get("ok.status", u"OK") }