summaryrefslogtreecommitdiffstats
path: root/build_tools/larch8/liblarch/translation.py
blob: 114a8a2541816a6254bb70cf9a09688c68f9f3db (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python2
#
# translation.py   --  Translation services
#
# (c) Copyright 2010 Michael Towers (larch42 at googlemail dot com)
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#-------------------------------------------------------------------
# 2010.08.14

import os, gettext
from liblarch_conf import liblarchdir

def i18n_module(basedir, appname):
    """This doesn't use gettext.install in order not to affect the whole
    application, it allows per module translation services.
    """
    return gettext.translation(appname, basedir + '/i18n', fallback=True).ugettext


def i18n_liblarch():
    """Provides translation for liblarch itself.
    """
    return i18n_module(liblarchdir, 'liblarch')
_ = i18n_liblarch()


lang = (os.environ.get('LANGUAGE') or os.environ.get('LC_ALL')
        or os.environ.get('LC_MESSAGES') or os.environ.get('LANG'))


def i18nurl(doc):
    docbase, docfile = doc.rsplit('/', 1)
    docbase += '/'
    i18npath = docbase + lang.split('.')[0] + '/' + docfile
    if not os.path.isfile(i18npath):
        i18npath = docbase + lang.split('_')[0] + '/' + docfile
    if not os.path.isfile(i18npath):
        i18npath = doc
        if not os.path.isfile(i18npath):
            return None
    return i18npath

def i18ndoc(doc):
    p = i18nurl(doc)
    if not p:
        return _("Document '%s' not found") % i18npath
    fh = open(p)
    data = fh.read().decode('utf-8')
    fh.close()
    return data