cut away 1/4th of enigma loading time by loading languages when they are used
[enigma2.git] / lib / python / Components / Language.py
1 import gettext
2
3 from Tools.Directories import SCOPE_LANGUAGE, resolveFilename
4
5 class Language:
6         def __init__(self):
7                 gettext.install('enigma2', resolveFilename(SCOPE_LANGUAGE, ""), unicode=0, codeset="utf-8")
8                 self.activeLanguage = 0
9                 self.lang = {}
10                 self.langlist = []
11                 # FIXME make list dynamically
12                 # name, iso-639 language, iso-3166 country. Please don't mix language&country!
13                 self.addLanguage(_("English"), "en", "EN")
14                 self.addLanguage(_("German"), "de", "DE")
15                 self.addLanguage(_("Arabic"), "ar", "AE")
16                 self.addLanguage(_("Catalan"), "ca", "AD")
17                 self.addLanguage(_("Croatian"), "hr", "HR")
18                 self.addLanguage(_("Czech"), "cs", "CZ")
19                 self.addLanguage(_("Danish"), "da", "DK")
20                 self.addLanguage(_("Dutch"), "nl", "NL")
21                 self.addLanguage(_("Finnish"), "fi", "FI")
22                 self.addLanguage(_("French"), "fr", "FR")
23                 self.addLanguage(_("Greek"), "el", "GR")
24                 self.addLanguage(_("Hungarian"), "hu", "HU")
25                 self.addLanguage(_("Lithuanian"), "lt", "LT")
26                 self.addLanguage(_("Icelandic"), "is", "IS")
27                 self.addLanguage(_("Italian"), "it", "IT")
28                 self.addLanguage(_("Norwegian"), "no", "NO")
29                 self.addLanguage(_("Polish"), "pl", "PL")
30                 self.addLanguage(_("Portuguese"), "pt", "PT")
31                 self.addLanguage(_("Russian"), "ru", "RU")
32                 self.addLanguage(_("Spanish"), "es", "ES")
33                 self.addLanguage(_("Swedish"), "sv", "SE")
34                 self.addLanguage(_("Turkish"), "tr", "TR")
35
36                 self.callbacks = []
37
38         def addLanguage(self, name, lang, country):
39                 try:
40                         self.lang[str(lang + "_" + country)] = ((_(name), lang, country))
41                         self.langlist.append(str(lang + "_" + country))
42                 except:
43                         print "Language " + str(name) + " not found"
44
45         def activateLanguage(self, index):
46                 try:
47                         lang = self.lang[index]
48                         print "Activating language " + lang[0]
49                         gettext.translation('enigma2', resolveFilename(SCOPE_LANGUAGE, ""), languages=[lang[1]]).install()
50                         self.activeLanguage = index
51                         for x in self.callbacks:
52                                 x()
53                 except:
54                         print "Selected language does not exist!"
55
56         def getLanguageList(self):
57                 list = []
58                 for x in self.langlist:
59                         list.append((x, self.lang[x]))
60                 return list
61
62         def getActiveLanguage(self):
63                 return self.activeLanguage
64
65         def getLanguage(self):
66                 try:
67                         return str(self.lang[self.activeLanguage][1]) + "_" + str(self.lang[self.activeLanguage][2])
68                 except:
69                         return ''
70
71         def addCallback(self, callback):
72                 self.callbacks.append(callback)
73
74 language = Language()