Language: add Norwegian Bokmål
[openblackhole/openblackhole-enigma2.git] / lib / python / Components / Language.py
1 # -*- coding: UTF-8 -*-
2 import gettext
3 import locale
4 import os
5
6 from Tools.Directories import SCOPE_LANGUAGE, resolveFilename
7
8 class Language:
9         def __init__(self):
10                 gettext.install('enigma2', resolveFilename(SCOPE_LANGUAGE, ""), unicode=0, codeset="utf-8")
11                 self.activeLanguage = 0
12                 self.catalog = None
13                 self.lang = {}
14                 self.langlist = []
15                 # FIXME make list dynamically
16                 # name, iso-639 language, iso-3166 country. Please don't mix language&country!
17                 self.addLanguage("Arabic",      "ar", "AE")
18                 self.addLanguage("Български",   "bg", "BG")
19                 self.addLanguage("Català",      "ca", "AD")
20                 self.addLanguage("Česky",       "cs", "CZ")
21                 self.addLanguage("Dansk",       "da", "DK")
22                 self.addLanguage("Deutsch",     "de", "DE")
23                 self.addLanguage("Ελληνικά",    "el", "GR")
24                 self.addLanguage("English",     "en", "EN")
25                 self.addLanguage("Español",     "es", "ES")
26                 self.addLanguage("Eesti",       "et", "EE")
27                 self.addLanguage("Persian",     "fa", "IR")
28                 self.addLanguage("Suomi",       "fi", "FI")
29                 self.addLanguage("Français",    "fr", "FR")
30                 self.addLanguage("Frysk",       "fy", "NL")
31                 self.addLanguage("Hebrew",      "he", "IL")
32                 self.addLanguage("Hrvatski",    "hr", "HR")
33                 self.addLanguage("Magyar",      "hu", "HU")
34                 self.addLanguage("Íslenska",    "is", "IS")
35                 self.addLanguage("Italiano",    "it", "IT")
36                 self.addLanguage("Kurdish",     "ku", "KU")
37                 self.addLanguage("Lietuvių",    "lt", "LT")
38                 self.addLanguage("Latviešu",    "lv", "LV")
39                 self.addLanguage("Nederlands",  "nl", "NL")
40                 self.addLanguage("Norsk Bokmål","nb", "NO")
41                 self.addLanguage("Norsk",       "no", "NO")
42                 self.addLanguage("Polski",      "pl", "PL")
43                 self.addLanguage("Português",   "pt", "PT")
44                 self.addLanguage("Português do Brasil",  "pt", "BR")
45                 self.addLanguage("Romanian",    "ro", "RO")
46                 self.addLanguage("Русский",     "ru", "RU")
47                 self.addLanguage("Slovensky",   "sk", "SK")
48                 self.addLanguage("Slovenščina", "sl", "SI")
49                 self.addLanguage("Srpski",      "sr", "YU")
50                 self.addLanguage("Svenska",     "sv", "SE")
51                 self.addLanguage("ภาษาไทย",     "th", "TH")
52                 self.addLanguage("Türkçe",      "tr", "TR")
53                 self.addLanguage("Ukrainian",   "uk", "UA")
54
55                 self.callbacks = []
56
57         def addLanguage(self, name, lang, country):
58                 try:
59                         self.lang[str(lang + "_" + country)] = ((name, lang, country))
60                         self.langlist.append(str(lang + "_" + country))
61                 except:
62                         print "Language " + str(name) + " not found"
63
64         def activateLanguage(self, index):
65                 try:
66                         lang = self.lang[index]
67                         print "Activating language " + lang[0]
68                         self.catalog = gettext.translation('enigma2', resolveFilename(SCOPE_LANGUAGE, ""), languages=[index])
69                         self.catalog.install(names=("ngettext", "pgettext"))
70                         self.activeLanguage = index
71                         for x in self.callbacks:
72                                 x()
73                 except:
74                         print "Selected language does not exist!"
75                 # NOTE: we do not use LC_ALL, because LC_ALL will not set any of the categories, when one of the categories fails.
76                 # We'd rather try to set all available categories, and ignore the others
77                 for category in [locale.LC_CTYPE, locale.LC_COLLATE, locale.LC_TIME, locale.LC_MONETARY, locale.LC_MESSAGES, locale.LC_NUMERIC]:
78                         try:
79                                 locale.setlocale(category, (self.getLanguage(), 'UTF-8'))
80                         except:
81                                 pass
82                 # HACK: sometimes python 2.7 reverts to the LC_TIME environment value, so make sure it has the correct value
83                 os.environ["LC_TIME"] = self.getLanguage() + '.UTF-8'
84
85         def activateLanguageIndex(self, index):
86                 if index < len(self.langlist):
87                         self.activateLanguage(self.langlist[index])
88
89         def getLanguageList(self):
90                 return [ (x, self.lang[x]) for x in self.langlist ]
91
92         def getActiveLanguage(self):
93                 return self.activeLanguage
94
95         def getActiveCatalog(self):
96                 return self.catalog
97
98         def getActiveLanguageIndex(self):
99                 idx = 0
100                 for x in self.langlist:
101                         if x == self.activeLanguage:
102                                 return idx
103                         idx += 1
104                 return None
105
106         def getLanguage(self):
107                 try:
108                         return str(self.lang[self.activeLanguage][1]) + "_" + str(self.lang[self.activeLanguage][2])
109                 except:
110                         return ''
111
112         def addCallback(self, callback):
113                 self.callbacks.append(callback)
114
115 language = Language()