1 | #if 0 |
---|
2 | # ----------------------------------------------------------------------- |
---|
3 | # $Id: table.py,v 1.9 2004/05/28 12:26:24 dischi Exp $ |
---|
4 | # ----------------------------------------------------------------------- |
---|
5 | # $Log: table.py,v $ |
---|
6 | # Revision 1.9 2004/05/28 12:26:24 dischi |
---|
7 | # Replace __str__ with unicode to avoid bad transformations. Everything |
---|
8 | # inside mmpython should be handled as unicode object. |
---|
9 | # |
---|
10 | # Revision 1.8 2003/07/02 11:17:29 the_krow |
---|
11 | # language is now part of the table key |
---|
12 | # |
---|
13 | # ----------------------------------------------------------------------- |
---|
14 | # MMPython - Media Metadata for Python |
---|
15 | # Copyright (C) 2003 Thomas Schueppel, Dirk Meyer |
---|
16 | # |
---|
17 | # This program is free software; you can redistribute it and/or modify |
---|
18 | # it under the terms of the GNU General Public License as published by |
---|
19 | # the Free Software Foundation; either version 2 of the License, or |
---|
20 | # (at your option) any later version. |
---|
21 | # |
---|
22 | # This program is distributed in the hope that it will be useful, but |
---|
23 | # WITHOUT ANY WARRANTY; without even the implied warranty of MER- |
---|
24 | # CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
25 | # Public License for more details. |
---|
26 | # |
---|
27 | # You should have received a copy of the GNU General Public License along |
---|
28 | # with this program; if not, write to the Free Software Foundation, Inc., |
---|
29 | # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
30 | # |
---|
31 | # ----------------------------------------------------------------------- |
---|
32 | #endif |
---|
33 | |
---|
34 | from gettext import GNUTranslations |
---|
35 | import os |
---|
36 | |
---|
37 | LOCALEDIR = 'i18n' |
---|
38 | |
---|
39 | class Table: |
---|
40 | def __init__(self, hashmap, name, language='en'): |
---|
41 | self.dict = hashmap |
---|
42 | self.name = name |
---|
43 | self.language = language |
---|
44 | self.translations = {} |
---|
45 | self.languages = [] |
---|
46 | self.i18ndir = os.path.join(LOCALEDIR, name.lower()) |
---|
47 | try: |
---|
48 | self.read_translations() |
---|
49 | except: |
---|
50 | pass |
---|
51 | |
---|
52 | def read_translations(self): |
---|
53 | for filename in [x for x in os.listdir(self.i18ndir) if x.endswith('.mo')]: |
---|
54 | lang = filename[:-3] |
---|
55 | filename = os.path.join(self.i18ndir, filename) |
---|
56 | f = open(filename, 'rb') |
---|
57 | self.translations[lang] = GNUTranslations(f) |
---|
58 | f.close() |
---|
59 | self.languages = self.translations.keys() |
---|
60 | |
---|
61 | def gettext(self, message, language = None): |
---|
62 | try: |
---|
63 | return self.translations[language].gettext(unicode(message)) |
---|
64 | except KeyError: |
---|
65 | return unicode(message) |
---|
66 | |
---|
67 | def __setitem__(self,key,value): |
---|
68 | self.dict[key] = value |
---|
69 | |
---|
70 | def __getitem__(self,key): |
---|
71 | try: |
---|
72 | return self.dict[key] |
---|
73 | except KeyError: |
---|
74 | return None |
---|
75 | |
---|
76 | def getstr(self,key): |
---|
77 | s = self[key] |
---|
78 | try: |
---|
79 | if s and len(unicode(s)) < 100: |
---|
80 | return s |
---|
81 | else: |
---|
82 | return "Not Displayable" |
---|
83 | except UnicodeDecodeError: |
---|
84 | return "Not Displayable" |
---|
85 | |
---|
86 | def has_key(self, key): |
---|
87 | return self.dict.has_key(key) |
---|
88 | |
---|
89 | def getEntry(self, key, language = 'en'): |
---|
90 | pair = (self.gettext(key), unicode(self.dict[key])) |
---|
91 | |
---|
92 | def __unicode__(self): |
---|
93 | header = "\nTable %s (%s):" % (self.name, self.language) |
---|
94 | result = reduce( lambda a,b: self[b] and "%s\n %s: %s" % \ |
---|
95 | (a, self.gettext(b,'en'), self.getstr(b)) or \ |
---|
96 | a, self.dict.keys(), header ) |
---|
97 | return result |
---|
98 | |
---|
99 | def accept(self,mi): |
---|
100 | pass |
---|