Merge commit 'origin/translations' into experimental
[enigma2.git] / lib / python / Screens / LanguageSelection.py
1 from Screen import Screen
2
3 from Components.ActionMap import ActionMap
4 from Components.Language import language
5 from Components.config import config
6 from Components.Sources.List import List
7 from Components.Label import Label
8 from Components.Pixmap import Pixmap
9 from Components.language_cache import LANG_TEXT
10
11 def _cached(x):
12         return LANG_TEXT.get(config.osd.language.value, {}).get(x, "")
13
14 from Screens.Rc import Rc
15
16 from Tools.Directories import resolveFilename, SCOPE_CURRENT_SKIN
17
18 from Tools.LoadPixmap import LoadPixmap
19
20 def LanguageEntryComponent(file, name, index):
21         png = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "countries/" + file + ".png"))
22         if png == None:
23                 png = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "countries/missing.png"))
24         res = (index, name, png)
25         return res
26
27 class LanguageSelection(Screen):
28         def __init__(self, session):
29                 Screen.__init__(self, session)
30
31                 self.oldActiveLanguage = language.getActiveLanguage()
32
33                 self.list = []
34                 self["languages"] = List(self.list)
35                 self["languages"].onSelectionChanged.append(self.changed)
36
37                 self.updateList()
38                 self.onLayoutFinish.append(self.selectActiveLanguage)
39
40                 self["actions"] = ActionMap(["OkCancelActions"], 
41                 {
42                         "ok": self.save,
43                         "cancel": self.cancel,
44                 }, -1)
45
46         def selectActiveLanguage(self):
47                 activeLanguage = language.getActiveLanguage()
48                 pos = 0
49                 for x in self.list:
50                         if x[0] == activeLanguage:
51                                 self["languages"].index = pos
52                                 break
53                         pos += 1
54
55         def save(self):
56                 self.run()
57                 self.close()
58
59         def cancel(self):
60                 language.activateLanguage(self.oldActiveLanguage)
61                 self.close()
62
63         def run(self, justlocal = False):
64                 print "updating language..."
65                 lang = self["languages"].getCurrent()[0]
66                 config.osd.language.value = lang
67                 config.osd.language.save()
68                 self.setTitle(_cached("T2"))
69                 
70                 if justlocal:
71                         return
72
73                 language.activateLanguage(lang)
74                 config.misc.languageselected.value = 0
75                 config.misc.languageselected.save()
76                 print "ok"
77
78         def updateList(self):
79                 first_time = not self.list
80
81                 languageList = language.getLanguageList()
82                 if not languageList: # no language available => display only english
83                         list = [ LanguageEntryComponent("en", _cached("en_EN"), "en_EN") ]
84                 else:
85                         list = [ LanguageEntryComponent(file = x[1][2].lower(), name = _cached("%s_%s" % x[1][1:3]), index = x[0]) for x in languageList]
86                 self.list = list
87
88                 #list.sort(key=lambda x: x[1][7])
89
90                 print "updateList"
91                 if first_time:
92                         self["languages"].list = list
93                 else:
94                         self["languages"].updateList(list)
95                 print "done"
96
97         def changed(self):
98                 self.run(justlocal = True)
99                 self.updateList()
100
101 class LanguageWizard(LanguageSelection, Rc):
102         def __init__(self, session):
103                 LanguageSelection.__init__(self, session)
104                 Rc.__init__(self)
105                 self.onLayoutFinish.append(self.selectKeys)
106                                 
107                 self["wizard"] = Pixmap()
108                 self["text"] = Label()
109                 self.setText()
110                 
111         def selectKeys(self):
112                 self.clearSelectedKeys()
113                 self.selectKey("UP")
114                 self.selectKey("DOWN")
115                 
116         def changed(self):
117                 self.run(justlocal = True)
118                 self.updateList()
119                 self.setText()
120                 
121         def setText(self):
122                 
123                 self["text"].setText(_cached("T1"))