summaryrefslogtreecommitdiff
path: root/Mailman/i18n.py
diff options
context:
space:
mode:
authorbwarsaw2000-12-07 19:47:49 +0000
committerbwarsaw2000-12-07 19:47:49 +0000
commit41a9850ae7cf543f4ea02752fd85c145773180b6 (patch)
tree6b22d3f23334db9502c648926a15eaa4a2a107d9 /Mailman/i18n.py
parent3547451a08df81a29ca35d991ab1613a11fe0658 (diff)
downloadmailman-41a9850ae7cf543f4ea02752fd85c145773180b6.tar.gz
mailman-41a9850ae7cf543f4ea02752fd85c145773180b6.tar.zst
mailman-41a9850ae7cf543f4ea02752fd85c145773180b6.zip
Diffstat (limited to 'Mailman/i18n.py')
-rw-r--r--Mailman/i18n.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/Mailman/i18n.py b/Mailman/i18n.py
new file mode 100644
index 000000000..d00c75947
--- /dev/null
+++ b/Mailman/i18n.py
@@ -0,0 +1,75 @@
+# Copyright (C) 2000 by the Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+import sys
+import gettext
+
+from Mailman import mm_cfg
+from Mailman.Utils import SafeDict
+
+
+
+_translation = gettext.NullTranslations()
+
+def set_language(language):
+ global _translation
+ _translation = gettext.translation('mailman', mm_cfg.LOCALEDIR, [language])
+
+
+
+def _x(s, frame):
+ # Do translation of the given string into the current language, and do
+ # Ping-string interpolation into the resulting string.
+ #
+ # This lets you write something like:
+ #
+ # now = time.ctime(time.time())
+ # print _('The current time is: %(now)s')
+ #
+ # and have it Just Work. Note that the lookup order for keys in the
+ # original string is 1) locals dictionary, 2) globals dictionary.
+ #
+ # A `safe' dictionary is used so we won't get an exception if there's a
+ # missing key in the dictionary.
+ dict = SafeDict(frame.f_globals.copy())
+ dict.update(frame.f_locals)
+ # Translate the string, then interpolate into it.
+ return _translation.gettext(s) % dict
+
+
+
+# Public version, to be used by most modules. There are three ways to get the
+# stack frame to serve as the namespace source. First, we try to use the
+# Python 2.1 extension to the sys module. If that's not there, we fall back
+# to the optional Mailman enhancement module, and finally use the
+# tried-and-true (but slow) pure Python approach. The latter should work in
+# every supported version of Python.
+if hasattr(sys, '_getframe'):
+ def _(s):
+ return _x(s, sys._getframe(1))
+else:
+ try:
+ import _mailman
+ def _(s):
+ return _x(s, _mailman._getframe(1))
+ except ImportError:
+ def _(s):
+ exc = 'exc'
+ try: raise exc
+ except exc:
+ # Get one frame up the stack.
+ frame = sys.exc_info()[2].tb_frame.f_back
+ return _x(s, frame)