1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-05 00:02:29 -04:00
PySolFC/pysollib/mygettext.py
2019-11-13 08:39:59 +02:00

70 lines
1.6 KiB
Python

import gettext
import sys
import six
class myLocalGettext(object):
def __init__(self, lang):
self.language = lang
def translation(self):
domain = gettext._current_domain
localedir = gettext._localedirs.get(domain, None)
if self.language == "":
t = gettext.translation(domain, localedir)
else:
t = gettext.translation(
domain, localedir, languages=[self.language])
return t
def maketext(self, msg):
if not isinstance(msg, six.text_type):
return six.text_type(msg, 'utf-8')
return msg
def ungettext(self, msgid1, msgid2, n):
# unicoded ngettext
msgid1 = self.maketext(msgid1)
msgid2 = self.maketext(msgid2)
try:
t = self.translation()
except IOError:
if n == 1:
return msgid1
else:
return msgid2
if sys.version_info >= (3, 0):
return t.ngettext(msgid1, msgid2, n)
else:
return t.ungettext(msgid1, msgid2, n)
def ugettext(self, message):
# unicoded gettext
message = self.maketext(message)
try:
t = self.translation()
except IOError:
return message
if sys.version_info >= (3, 0):
return t.gettext(message)
else:
return t.ugettext(message)
myGettext = myLocalGettext('')
def n_(x):
return x
def fix_gettext():
gettext.ugettext = myGettext.ugettext
gettext.ungettext = myGettext.ungettext
fix_gettext()
_ = gettext.ugettext
ungettext = gettext.ungettext