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