add latvian language
[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(_("Finnish"), "fi", "FI")
25                 self.addLanguage(_("French"), "fr", "FR")
26                 self.addLanguage(_("Greek"), "el", "GR")
27                 self.addLanguage(_("Hungarian"), "hu", "HU")
28                 self.addLanguage(_("Lithuanian"), "lt", "LT")
29                 self.addLanguage(_("Latvian"), "lv", "LV")
30                 self.addLanguage(_("Icelandic"), "is", "IS")
31                 self.addLanguage(_("Italian"), "it", "IT")
32                 self.addLanguage(_("Norwegian"), "no", "NO")
33                 self.addLanguage(_("Polish"), "pl", "PL")
34                 self.addLanguage(_("Portuguese"), "pt", "PT")
35                 self.addLanguage(_("Russian"), "ru", "RU")
36                 self.addLanguage(_("Spanish"), "es", "ES")
37                 self.addLanguage(_("Swedish"), "sv", "SE")
38                 self.addLanguage(_("Turkish"), "tr", "TR")
39                 self.addLanguage(_("Ukrainian"), "uk", "UA")
40                 self.addLanguage(_("Frisian"), "fy", "x-FY") # there is no separate country for frisian
41
42                 self.callbacks = []
43
44         def addLanguage(self, name, lang, country):
45                 try:
46                         self.lang[str(lang + "_" + country)] = ((_(name), lang, country))
47                         self.langlist.append(str(lang + "_" + country))
48                 except:
49                         print "Language " + str(name) + " not found"
50
51         def activateLanguage(self, index):
52                 try:
53                         lang = self.lang[index]
54                         print "Activating language " + lang[0]
55                         gettext.translation('enigma2', resolveFilename(SCOPE_LANGUAGE, ""), languages=[lang[1]]).install()
56                         self.activeLanguage = index
57                         for x in self.callbacks:
58                                 x()
59                 except:
60                         print "Selected language does not exist!"
61
62         def activateLanguageIndex(self, index):
63                 if index < len(self.langlist):
64                         self.activateLanguage(self.langlist[index])
65
66         def getLanguageList(self):
67                 list = []
68                 for x in self.langlist:
69                         list.append((x, self.lang[x]))
70                 return list
71
72         def getActiveLanguage(self):
73                 return self.activeLanguage
74         
75         def getActiveLanguageIndex(self):
76                 for count in range(len(self.langlist)):
77                         if self.langlist[count] == self.activeLanguage:
78                                 return count
79                 return None                     
80
81         def getLanguage(self):
82                 try:
83                         return str(self.lang[self.activeLanguage][1]) + "_" + str(self.lang[self.activeLanguage][2])
84                 except:
85                         return ''
86
87         def addCallback(self, callback):
88                 self.callbacks.append(callback)
89
90         def precalcLanguageList(self):
91                 # excuse me for those T1, T2 hacks please. The goal was to keep the language_cache.py as small as possible, *and* 
92                 # don't duplicate these strings.
93                 T1 = _("Please use the UP and DOWN keys to select your language. Afterwards press the OK button.")
94                 T2 = _("Language selection")
95                 l = open("language_cache.py", "w")
96                 print >>l, "# -*- coding: UTF-8 -*-"
97                 print >>l, "LANG_TEXT = {"
98                 for language in self.langlist:
99                         self.activateLanguage(language)
100                         print >>l, '"%s": {' % language
101                         for name, lang, country in self.lang.values():
102                                 print >>l, '\t"%s_%s": "%s",' % (lang, country, _(name))
103
104                         print >>l, '\t"T1": "%s",' % (_(T1))
105                         print >>l, '\t"T2": "%s",' % (_(T2))
106                         print >>l, '},'
107                 print >>l, "}"
108
109 language = Language()