Skip to content

Commit 9221ec5

Browse files
author
Alexander Smishlajev
committed
export unicode functions and DOMAIN name;
added ngettext emulation for earlier python versions (2.1, 2.2); added get_translation() function to find different translations at runtime
1 parent 4067c1f commit 9221ec5

File tree

1 file changed

+107
-13
lines changed

1 file changed

+107
-13
lines changed

roundup/i18n.py

Lines changed: 107 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: i18n.py,v 1.6 2004-05-18 19:20:47 a1s Exp $
18+
# $Id: i18n.py,v 1.7 2004-05-22 14:40:17 a1s Exp $
1919

2020
"""
2121
RoundUp Internationalization (I18N)
@@ -37,21 +37,115 @@
3737
"""
3838
__docformat__ = 'restructuredtext'
3939

40+
import errno
41+
42+
# Roundup text domain
43+
DOMAIN = "roundup"
44+
4045
# first, we try to import gettext; this probably never fails, but we make
4146
# sure we survive this anyway
4247
try:
43-
import gettext
48+
import gettext as gettext_module
4449
except ImportError:
45-
# fall-back to dummy on errors (returning the english text)
46-
_ = gettext = lambda text: text
47-
def ngettext(singular, plural, count):
48-
if count == 1: return singular
49-
return plural
50+
gettext_module = None
51+
52+
# use or emulate features of gettext_module
53+
if not gettext_module:
54+
# no gettext engine available.
55+
# implement emulation for Translations class
56+
# and translation() function
57+
class RoundupNullTranslations:
58+
"""Dummy Translations class
59+
60+
Only methods used by Roundup are implemented.
61+
62+
"""
63+
def add_fallback(self, fallback):
64+
pass
65+
def gettext(self, text):
66+
return text
67+
def ugettext(self, text):
68+
return unicode(text)
69+
def ngettext(self, singular, plural, count):
70+
if count == 1: return singular
71+
return plural
72+
def ungettext(self, singular, plural, count):
73+
return unicode(self.ngettext(singular, plural, count))
74+
75+
RoundupTranslations = RoundupNullTranslations
76+
77+
def translation(domain, localedir=None, languages=None, class_=None):
78+
"""Always raise IOError (no message catalogs available)"""
79+
raise IOError(errno.ENOENT,
80+
"No translation file found for domain", domain)
81+
82+
elif not hasattr(gettext_module.GNUTranslations, "ngettext"):
83+
# prior to 2.3, there was no plural forms. mix simple emulation in
84+
class PluralFormsMixIn:
85+
def ngettext(self, singular, plural, count):
86+
if count == 1:
87+
_msg = singular
88+
else:
89+
_msg = plural
90+
return self.gettext(_msg)
91+
def ngettext(self, singular, plural, count):
92+
if count == 1:
93+
_msg = singular
94+
else:
95+
_msg = plural
96+
return self.gettext(_msg)
97+
class RoundupNullTranslations(
98+
gettext_module.NullTranslations, PluralFormsMixIn
99+
):
100+
pass
101+
class RoundupTranslations(
102+
gettext_module.GNUTranslations, PluralFormsMixIn
103+
):
104+
pass
105+
# lookup function is available
106+
translation = gettext_module.translation
50107
else:
51-
# use roundup messages
52-
gettext.textdomain("roundup")
53-
# export gettext functions
54-
ngettext = gettext.ngettext
55-
_ = gettext = gettext.gettext
108+
# gettext_module has everything needed
109+
RoundupNullTranslations = gettext_module.NullTranslations
110+
RoundupTranslations = gettext_module.GNUTranslations
111+
translation = gettext_module.translation
112+
113+
114+
def get_translation(language=None, domain=DOMAIN):
115+
"""Return Translation object for given language and domain"""
116+
if language:
117+
_languages = [language]
118+
else:
119+
# use OS environment
120+
_languages = None
121+
# except for english ("en") language, add english fallback if available
122+
try:
123+
_fallback = translation(domain=domain, languages=["en"],
124+
class_=RoundupTranslations)
125+
except IOError:
126+
# no .mo files found
127+
_fallback = None
128+
# get the translation
129+
try:
130+
_translation = translation(domain=domain, languages=_languages,
131+
class_=RoundupTranslations)
132+
except IOError:
133+
_translation = None
134+
# see what's found
135+
if _translation and _fallback:
136+
_translation.add_fallback(_fallback)
137+
elif _fallback:
138+
_translation = _fallback
139+
elif not _translation:
140+
_translation = RoundupNullTranslations()
141+
return _translation
142+
143+
# static translations object
144+
translation = get_translation()
145+
# static translation functions
146+
_ = gettext = translation.gettext
147+
ugettext = translation.ugettext
148+
ngettext = translation.ngettext
149+
ungettext = translation.ungettext
56150

57-
# vim: set filetype=python ts=4 sw=4 et si
151+
# vim: set filetype=python sts=4 sw=4 et si

0 commit comments

Comments
 (0)