summaryrefslogtreecommitdiffstats
path: root/deluge/ui/web/common.py
diff options
context:
space:
mode:
Diffstat (limited to 'deluge/ui/web/common.py')
-rw-r--r--deluge/ui/web/common.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/deluge/ui/web/common.py b/deluge/ui/web/common.py
new file mode 100644
index 0000000..32c29c8
--- /dev/null
+++ b/deluge/ui/web/common.py
@@ -0,0 +1,43 @@
+#
+# Copyright (C) 2009 Damien Churchill <damoxc@gmail.com>
+#
+# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
+# the additional special exception to link portions of this program with the OpenSSL library.
+# See LICENSE for more details.
+#
+
+import gettext
+
+from mako.template import Template as MakoTemplate
+
+from deluge.common import get_version
+
+
+def _(text):
+ text_local = gettext.gettext(text)
+ return text_local
+
+
+def escape(text):
+ """
+ Used by gettext.js template to escape any translated language strings that
+ might contain newlines or quotes as they would break the script.
+ """
+ text = text.replace("'", "\\'")
+ text = text.replace('\r\n', '\\n')
+ text = text.replace('\r', '\\n')
+ text = text.replace('\n', '\\n')
+ return text
+
+
+class Template(MakoTemplate):
+ """
+ A template that adds some built-ins to the rendering
+ """
+
+ builtins = {'_': _, 'escape': escape, 'version': get_version()}
+
+ def render(self, *args, **data):
+ data.update(self.builtins)
+ rendered = MakoTemplate.render_unicode(self, *args, **data)
+ return rendered.encode('utf-8')