summaryrefslogtreecommitdiffstats
path: root/deluge/ui/web/common.py
blob: a8c223d19e86080f5b54a810ea8d87f67fcff2ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: utf-8 -*-
#
# 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')