summaryrefslogtreecommitdiffstats
path: root/translate.py
blob: 12299e6ee1c7faab3bfc70d1fb2a197c70c9f4d8 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#
# translate.py - persistent global gettext service for anaconda
#
# Matt Wilson <msw@redhat.com>
#
# Copyright 2001 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import gettext
import iconv
import os
import gzread

class i18n:
    def __init__(self, langs=None, unicode=0):
        self.unicode = unicode
        if langs:
            self.langs = langs
        else:
            self.langs = ['C']
        mofile = None
        searchpath = ('po/%s.mo',
                      '/usr/share/locale/%s/LC_MESSAGES/anaconda.mo')
        for lang in self.langs:
            for path in searchpath:
                try:
                    mofile = gzread.open(path % (lang))
                except IOError:
                    pass
                if mofile:
                    break
        if not mofile:
            self.cat = None
            self.iconv = iconv.open('utf-8', 'iso-8859-1')
            return

        self.cat = gettext.GNUTranslations(mofile)
        try:
            self.iconv = iconv.open('utf-8', self.cat.charset())
        except:
            print 'unable to translate from', self.cat.charset(), 'to utf-8'
            self.iconv = iconv.open('utf-8', 'iso-8859-1')

    def setunicode(self, value):
        self.unicode = value

    def getunicode(self):
        return self.unicode

    def getlangs(self):
	return self.langs
        
    def setlangs(self, langs):
        self.__init__(langs, self.unicode)

    def utf8(self, string):
        try:
            return self.iconv.iconv(string)
        except:
            return string

    def gettext(self, string):
        if self.unicode:
            if not self.cat:
                return string
            translation = self.cat._catalog.get(string)
            if translation:
                return self.utf8(translation)
            return string
        else: 
            if not self.cat:
                return string
            return self.cat.gettext(string)

def N_(str):
    return str

cat = i18n()
_ = cat.gettext
utf8 = cat.utf8