Merge commit 'origin/bug_112_enable_extensions_manager' into experimental
[enigma2.git] / lib / python / Components / Language.py
1 import gettext
2
3 from Tools.Directories import SCOPE_LANGUAGE, resolveFilename
4
5 import language_cache
6
7 class Language:
8         def __init__(self):
9                 gettext.install('enigma2', resolveFilename(SCOPE_LANGUAGE, ""), unicode=0, codeset="utf-8")
10                 self.activeLanguage = 0
11                 self.lang = {}
12                 self.langlist = []
13                 # FIXME make list dynamically
14                 # name, iso-639 language, iso-3166 country. Please don't mix language&country!
15                 # also, see "precalcLanguageList" below on how to re-create the language cache after you added a language
16                 self.addLanguage(_("English"), "en", "EN")
17                 self.addLanguage(_("German"), "de", "DE")
18                 self.addLanguage(_("Arabic"), "ar", "AE")
19                 self.addLanguage(_("Catalan"), "ca", "AD")
20                 self.addLanguage(_("Croatian"), "hr", "HR")
21                 self.addLanguage(_("Czech"), "cs", "CZ")
22                 self.addLanguage(_("Danish"), "da", "DK")
23                 self.addLanguage(_("Dutch"), "nl", "NL")
24                 self.addLanguage(_("Estonian"), "et", "EE")
25                 self.addLanguage(_("Finnish"), "fi", "FI")
26                 self.addLanguage(_("French"), "fr", "FR")
27                 self.addLanguage(_("Greek"), "el", "GR")
28                 self.addLanguage(_("Hungarian"), "hu", "HU")
29                 self.addLanguage(_("Lithuanian"), "lt", "LT")
30                 self.addLanguage(_("Latvian"), "lv", "LV")
31                 self.addLanguage(_("Icelandic"), "is", "IS")
32                 self.addLanguage(_("Italian"), "it", "IT")
33                 self.addLanguage(_("Norwegian"), "no", "NO")
34                 self.addLanguage(_("Polish"), "pl", "PL")
35                 self.addLanguage(_("Portuguese"), "pt", "PT")
36                 self.addLanguage(_("Russian"), "ru", "RU")
37                 self.addLanguage(_("Serbian"), "sr", "YU")
38                 self.addLanguage(_("Slovakian"), "sk", "SK")
39                 self.addLanguage(_("Slovenian"), "sl", "SI")
40                 self.addLanguage(_("Spanish"), "es", "ES")
41                 self.addLanguage(_("Swedish"), "sv", "SE")
42                 self.addLanguage(_("Turkish"), "tr", "TR")
43                 self.addLanguage(_("Ukrainian"), "uk", "UA")
44                 self.addLanguage(_("Frisian"), "fy", "x-FY") # there is no separate country for frisian
45
46                 self.callbacks = []
47
48         def addLanguage(self, name, lang, country):
49                 try:
50                         self.lang[str(lang + "_" + country)] = ((_(name), lang, country))
51                         self.langlist.append(str(lang + "_" + country))
52                 except:
53                         print "Language " + str(name) + " not found"
54
55         def activateLanguage(self, index):
56                 try:
57                         lang = self.lang[index]
58                         print "Activating language " + lang[0]
59                         gettext.translation('enigma2', resolveFilename(SCOPE_LANGUAGE, ""), languages=[lang[1]]).install()
60                         self.activeLanguage = index
61                         for x in self.callbacks:
62                                 x()
63                 except:
64                         print "Selected language does not exist!"
65
66         def activateLanguageIndex(self, index):
67                 if index < len(self.langlist):
68                         self.activateLanguage(self.langlist[index])
69
70         def getLanguageList(self):
71                 return [ (x, self.lang[x]) for x in self.langlist ]
72
73         def getActiveLanguage(self):
74                 return self.activeLanguage
75         
76         def getActiveLanguageIndex(self):
77                 idx = 0
78                 for x in self.langlist:
79                         if x == self.activeLanguage:
80                                 return idx
81                         idx += 1
82                 return None                     
83
84         def getLanguage(self):
85                 try:
86                         return str(self.lang[self.activeLanguage][1]) + "_" + str(self.lang[self.activeLanguage][2])
87                 except:
88                         return ''
89
90         def addCallback(self, callback):
91                 self.callbacks.append(callback)
92
93         def precalcLanguageList(self):
94                 # excuse me for those T1, T2 hacks please. The goal was to keep the language_cache.py as small as possible, *and* 
95                 # don't duplicate these strings.
96                 T1 = _("Please use the UP and DOWN keys to select your language. Afterwards press the OK button.")
97                 T2 = _("Language selection")
98                 l = open("language_cache.py", "w")
99                 print >>l, "# -*- coding: UTF-8 -*-"
100                 print >>l, "LANG_TEXT = {"
101                 for language in self.langlist:
102                         self.activateLanguage(language)
103                         print >>l, '"%s": {' % language
104                         for name, lang, country in self.lang.values():
105                                 print >>l, '\t"%s_%s": "%s",' % (lang, country, _(name))
106
107                         print >>l, '\t"T1": "%s",' % (_(T1))
108                         print >>l, '\t"T2": "%s",' % (_(T2))
109                         print >>l, '},'
110                 print >>l, "}"
111
112 language = Language()