deliver service events only to last created class which is based on
[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 activateLanguageIndex(self, index):
57                 if index < len(self.langlist):
58                         self.activateLanguage(self.langlist[index])
59
60         def getLanguageList(self):
61                 list = []
62                 for x in self.langlist:
63                         list.append((x, self.lang[x]))
64                 return list
65
66         def getActiveLanguage(self):
67                 return self.activeLanguage
68         
69         def getActiveLanguageIndex(self):
70                 for count in range(len(self.langlist)):
71                         if self.langlist[count] == self.activeLanguage:
72                                 return count
73                 return None                     
74
75         def getLanguage(self):
76                 try:
77                         return str(self.lang[self.activeLanguage][1]) + "_" + str(self.lang[self.activeLanguage][2])
78                 except:
79                         return ''
80
81         def addCallback(self, callback):
82                 self.callbacks.append(callback)
83
84 language = Language()