summaryrefslogtreecommitdiffstats
path: root/docs/analysis
diff options
context:
space:
mode:
Diffstat (limited to 'docs/analysis')
0 files changed, 0 insertions, 0 deletions
'>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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
#
# language.py: install data component that stores information about both
#              installer runtime language choice and installed system
#              language support.
#
# Copyright 2001-2003 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 os
import string
import locale
import rpm

from rhpl.translate import cat
from rhpl.simpleconfig import SimpleConfigFile

# Converts a single language into a "language search path". For example,
# fr_FR.utf8@euro would become "fr_FR.utf8@eueo fr_FR.utf8 fr_FR fr"
def expandLangs(str):
    langs = [str]
    # remove charset ...
    if '.' in str:
	langs.append(string.split(str, '.')[0])

    if '@' in str:
	langs.append(string.split(str, '@')[0])

    # also add 2 character language code ...
    if len(str) > 2:
	langs.append(str[:2])

    return langs

# This is the langauge that's being used at install time (a list of the
# choices is in lang-table). 
class InstallTimeLanguage:

    def __init__ (self):
        if os.environ.has_key("LANG"):
            self.current = os.environ["LANG"]
        else:
            self.current = "en_US.UTF-8"
        self.nativeLangNames = {}

        search = ('lang-names', '/usr/lib/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 = line.split(' ', 1)
                    native = native.strip()
                    self.nativeLangNames[lang] = native
                break

        search = ('lang-table', '/etc/lang-table',
                  '/usr/lib/anaconda/lang-table')
        for path in search:
            if os.access(path, os.R_OK):
                f = open(path, "r")
                break

	lines = f.readlines ()
	f.close()
	self.langNicks = {}
	self.font = {}
	self.kbd = {}
	self.tz = {}
	self.langList = []
        self.runtimeLangs = {}

        self.tempDefault = ""

	for line in lines:
	    string.strip(line)
	    l = string.split(line)

            # throw out invalid lines
            if len(l) < 7:
                continue
	    
	    longName = l[0]
	    font = l[2]
	    shortName = l[3]
	    keyboard = l[4]
	    timezone = l[5]
            runtime = l[6]

	    self.langList.append(longName)
	    self.langNicks[longName] = shortName
	    self.font[longName] = font
	    self.kbd[longName] = keyboard
	    self.tz[longName] = timezone
            self.runtimeLangs[runtime] = shortName

        if self.runtimeLangs.has_key(self.current):
            self.current = self.runtimeLangs[self.current]

	self.langList.sort()
        self.setRuntimeLanguage(self.getLangNameByNick(self.current))

    def getFontFile (self, lang):
	# Note: in /etc/fonts.cgz fonts are named by the map
	# name as that's unique, font names are not
	return self.font[lang]

    def getLangNick (self, lang):
        # returns the short locale ID
	return self.langNicks[lang]

    def getNativeLangName(self, lang):
        return self.nativeLangNames.get(lang)

    def getLangNameByNick(self, lang):
	# The nick we get here may be long (fr_FR@euro), when we need
	# shorter (fr_FR), so be a bit fuzzy
	for (langName, nick) in self.langNicks.items():
            if (nick == lang) or (nick == lang[0:len(nick)]) or (lang == nick[0:len(lang)]):
		return langName

        # FIXME: this could end up infinitely recursing if we screw up the
        # lang-table.  need to revise the whole thing with post-install
        # language not being the same as the installer language
        for (nick, main) in self.runtimeLangs.items():
            if (nick == lang) or (nick == lang[0:len(nick)]) or (lang == nick[0:len(lang)]):
                return self.getLangNameByNick(main)

#        raise KeyError, "language %s not found" % lang
        return self.getLangNameByNick("en_US.UTF-8")

    def getDefaultKeyboard(self):
	return self.kbd[self.getCurrent()]

    def getDefaultTimeZone(self):
	return self.tz[self.getCurrent()]

    def available (self):
        return self.langList

    def getCurrentLangSearchList(self):
	return expandLangs(self.langNicks[self.getCurrent()]) + ['C']

    def getCurrent(self):
	return self.getLangNameByNick(self.current)

    def setRuntimeDefaults(self, name):
	lang = self.langNicks[name]
        self.current = lang
        # XXX HACK HACK, I'm using an environment variable to communicate
        # between two classes (runtimelang and lang support)
        os.environ["RUNTIMELANG"] = lang

    def setRuntimeLanguage(self, name):
        self.setRuntimeDefaults(name)
        lang = self.langNicks[name]

        for (runtime, main) in self.runtimeLangs.items():
            if main == lang:
                lang = runtime

        os.environ["LANG"] = lang
        os.environ["LC_NUMERIC"] = 'C'
        try:
            locale.setlocale(locale.LC_ALL, "")
        except locale.Error:
            pass

        newlangs = [lang]
        if lang.find(".") != -1:
            newlangs.append(lang[:lang.find(".")])
	if len(lang) > 2:
            newlangs.append(lang[:2])
        cat.setlangs(newlangs)

    def writeKS(self, f):
	lang = self.getLangNick(self.getCurrent())
	f.write("lang %s\n" % lang);

# The languages which should be supported on the installed system, including
# which language to set as the default.
class Language (SimpleConfigFile):

    def __init__ (self):
        self.info = {}
        self.info["SUPPORTED"] = None
	self.supported = []
	self.default = None

        self.allSupportedLangs = []
        self.langInfoByName = {}

        allSupportedLangs = []
        langInfoByName = {}
        langFilter = {}
        allInstalledFlag = 0

        langsInstalled = []
        if os.access("/usr/share/anaconda/locale-list", os.R_OK):
            f = open("/usr/share/anaconda/locale-list")
            lines = f.readlines()
            f.close()
            for line in lines:
                line = string.strip(line)
                (lang, map, font, name) = string.split(line, ' ', 3)
                langInfoByName[name] = (lang, map, font)
                allSupportedLangs.append(name)

                if allInstalledFlag or (langFilter and langFilter.has_key(lang)):
                    langsInstalled.append(name)
        else:
            langInfoByName['English (USA)'] = ('en_US.UTF-8', 'iso01', 'default8x16')
            allSupportedLangs.append('English (USA)')
            langsInstalled.append('English (USA)')

        self.langInfoByName = langInfoByName
        self.allSupportedLangs = allSupportedLangs

    def getAllSupported(self):
	return self.allSupportedLangs

    def getLangNameByNick(self, nick):
	for langName in self.langInfoByName.keys():
	    (lang, map, font) = self.langInfoByName[langName]
            if (nick == lang) or (nick == lang[0:len(nick)]) or (lang == nick[0:len(lang)]):            
		return langName

#	raise KeyError, "language %s not found" % nick
        return self.getLangNameByNick("en_US.UTF-8")

    def getLangNickByName(self, name):
	(lang, map, font) = self.langInfoByName[name]
        return lang

    def getSupported (self):
	return self.supported