summaryrefslogtreecommitdiffstats
path: root/pyanaconda/language.py
blob: f1663d749817c9b24caff108b5035a28071286e9 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#
# language.py: install data component that stores information about both
#              installer runtime language choice and installed system
#              language support.
#
# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2009  Red Hat, Inc.
# All rights reserved.
#
# 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, see <http://www.gnu.org/licenses/>.
#

import os
import string
import locale

import gettext
from pyanaconda.constants import ROOT_PATH, DEFAULT_LANG
import localeinfo
from simpleconfig import SimpleConfigFile
import system_config_keyboard.keyboard as keyboard

import logging
log = logging.getLogger("anaconda")

class Language(object):
    def _setInstLang(self, value):
        # Always store in its full form so we know what we're comparing with.
        try:
            self._instLang = self._canonLang(value)
        except ValueError:
            # If the language isn't listed in lang-table, we won't know what
            # keyboard/etc. to use.  However, we can still set the $LANG
            # to that and make sure it works in the installed system.
            self._instLang = value

        # If we're running in text mode, value may not be a supported language
        # to display.  Fall back to the default for now.
        if self.displayMode == 't':
            for (lang, info) in self.localeInfo.iteritems():
                if lang == self._instLang and info[2] == "False":
                    self._instLang = self._default
                    break

        # Now set some things to make sure the language setting takes effect
        # right now.
        os.environ["LANG"] = self._instLang
        os.environ["LC_NUMERIC"] = "C"

        try:
            locale.setlocale(locale.LC_ALL, "")
        except locale.Error:
            pass

        # XXX: oh ick.  this is the sort of thing which you should never do...
        # but we switch languages at runtime and thus need to invalidate
        # the set of languages/mofiles which gettext knows about
        gettext._translations = {}

    def _getInstLang(self):
        # If we were given a language that's not in lang-table, lie and say
        # we're using the default.  This prevents us from having to check all
        # over the place.
        if self._instLang in self.localeInfo.keys():
            return self._instLang
        else:
            return self._default

    # The language being displayed while anaconda is running.
    instLang = property(lambda s: s._getInstLang(), lambda s, v: s._setInstLang(v))

    def _setSystemLang(self, value):
        # Always store in its full form so we know what we're comparing with.
        try:
            self._systemLang = self._canonLang(value)
        except ValueError:
            # If the language isn't listed in lang-table, we won't know what
            # keyboard/etc. to use.  However, we can still set the $LANG
            # to that and make sure it works in the installed system.
            self._systemLang = value

        # Now set a bunch of other things that'll get written to
        # /etc/sysconfig/i18n on the installed system.
        self.info["LANG"] = self._systemLang

        if not self.localeInfo.has_key(self._systemLang):
            return

        if self.localeInfo[self._systemLang][2] == "False":
            self.info["SYSFONT"] = None
        else:
            self.info["SYSFONT"] = "latarcyrheb-sun16"

        # XXX hack - because of exceptional cases on the var - zh_CN.GB2312
        if self._systemLang == "zh_CN.GB18030":
            self.info["LANGUAGE"] = "zh_CN.GB18030:zh_CN.GB2312:zh_CN"

    # The language to use on the installed system.  This can differ from the
    # language being used during anaconda.  For instance, text installs cannot
    # display all languages (CJK, Indic, etc.).
    systemLang = property(lambda s: s._systemLang, lambda s, v: s._setSystemLang(v))

    def __init__ (self, display_mode = 'g'):
        self._default = DEFAULT_LANG
        self.displayMode = display_mode
        self.info = {}
        self.nativeLangNames = {}

        # English name -> native name mapping
        search = ('lang-names', '/usr/share/anaconda/lang-names')
        for path in search:
            if os.access(path, os.R_OK):
                f = open(path, 'r')
                for line in f.readlines():
                    lang, native = string.split(line, '\t')
                    native = native.strip()
                    self.nativeLangNames[lang] = native

                f.close()
                break

        self.localeInfo = localeinfo.get(self._default)

        # instLang must be set after localeInfo is populated, in case the
        # current setting is unsupported by anaconda..
        self.instLang = os.environ.get("LANG", self._default)
        self.systemLang = os.environ.get("LANG", self._default)

    def _canonLang(self, lang):
        """Convert the shortened form of a language name into the full
           version.  If it's not found, raise ValueError.

           Example:  fr    -> fr_FR.UTF-8
                     fr_FR -> fr_FR.UTF-8
                     fr_CA -> ValueError
        """
        for key in self.localeInfo.keys():
            if lang in localeinfo.expandLangs(key):
                return key

        raise ValueError

    def available(self):
        return self.nativeLangNames.keys()

    def dracutSetupArgs(self):
        args=set()

        for (key, val) in self.info.iteritems():
            if val != None:
                args.add("%s=%s" % (key, val))

        return args

    def getCurrentLangSearchList(self):
        return localeinfo.expandLangs(self.systemLang) + ['C']

    def getDefaultKeyboard(self):
        try:
            return self.localeInfo[self.systemLang][3]
        except KeyError:
            try:
                kbd = keyboard.Keyboard()
                kbd.read(ROOT_PATH)
                return kbd.get()
            except:
                return self.localeInfo[self._default][3]

    def getDefaultTimeZone(self):
        try:
            return self.localeInfo[self.systemLang][4]
        except KeyError:
            # If doing an upgrade and the system language is something not
            # recognized by anaconda, we should try to see if we can figure
            # it out from the running system.
            if os.path.exists(ROOT_PATH + "/etc/sysconfig/clock"):
                cfg = SimpleConfigFile()
                cfg.read(ROOT_PATH + "/etc/sysconfig/clock")

                try:
                    return cfg.get("ZONE")
                except:
                    return self.localeInfo[self._default][4]
            else:
                return self.localeInfo[self._default][4]

    def textSupported(self, lang):
        try:
            l = self._canonLang(lang)
        except ValueError:
            l = self._default

        return self.localeInfo[l][2]

    def getLangName(self, lang):
        try:
            l = self._canonLang(lang)
        except ValueError:
            l = self._default

        return self.localeInfo[l][0]

    def getLangByName(self, name):
        for (key, val) in self.localeInfo.iteritems():
            if val[0] == name:
                return key

    def getNativeLangName(self, lang):
        return self.nativeLangNames[lang]

    def write(self):
        f = open(ROOT_PATH + "/etc/sysconfig/i18n", "w")

        for (key, val) in self.info.iteritems():
            if val != None:
                f.write("%s=\"%s\"\n" % (key, val))

        f.close()