annotate MoinMoin/i18n/__init__.py @ 941:16f7b6728b4e

move vary: header code to request module and fix it (multiple headers of same type don't work!)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 16 Jul 2006 14:58:21 +0200
parents d2f07ff64b78
children 787e963c9bfd
rev   line source
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
1 #!/usr/bin/env python2.4
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
2 # -*- coding: iso-8859-1 -*-
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
3 """
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
4 MoinMoin - internationalization (aka i18n)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
5
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
6 We use Python's gettext module now for loading <language>.<domain>.mo files.
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
7 Domain is "MoinMoin" for MoinMoin distribution code and something else for
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
8 extension translations.
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
9
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
10 Public attributes:
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
11 languages -- dict of languages that MoinMoin knows metadata about
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
12
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
13 Public functions:
159
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
14 requestLanguage(request, usecache=1) -- return the request language
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
15 wikiLanguages() -- return the available wiki user languages
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
16 browserLanguages() -- return the browser accepted languages
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
17 getDirection(lang) -- return the lang direction either 'ltr' or 'rtl'
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
18 getText(str, request) -- return str translation
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
19
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
20 TODO: as soon as we have some "farm / server plugin dir", extend this to
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
21 load translations from there, too.
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
22
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
23 @copyright: 2001-2004 by Jürgen Hermann <jh@web.de>,
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
24 2005-2006 by MoinMoin:ThomasWaldmann
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
25 @license: GNU GPL, see COPYING for details.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
26 """
489
9082a95430db fixed (wrong) cyclic usage exception
Thomas Waldmann <tw@waldmann-edv.de>
parents: 159
diff changeset
27 debug = 0
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
28
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
29 import os, gettext, glob
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
30
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
31 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
32 import cPickle as pickle
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
33 except ImportError:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
34 import pickle
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
35 # Set pickle protocol, see http://docs.python.org/lib/node64.html
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
36 PICKLE_PROTOCOL = pickle.HIGHEST_PROTOCOL
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
37
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
38 from MoinMoin import caching
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
39
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
40 # This is a global for a reason: in persistent environments all languages in
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
41 # use will be cached; Note: you have to restart if you update language data.
159
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
42
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
43 # key: language, value: language metadata
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
44 # this gets loaded early and completely:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
45 languages = None
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
46
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
47 translations = {}
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
48
896
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
49 def po_filename(request, language, domain):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
50 """ we use MoinMoin/i18n/<language>[.<domain>].mo as filename for the PO file.
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
51
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
52 TODO: later, when we have a farm scope plugin dir, we can also load
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
53 language data from there.
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
54 """
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
55 return os.path.join(request.cfg.moinmoin_dir, 'i18n', "%s.%s.po" % (language, domain))
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
56
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
57 def i18n_init(request):
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
58 """ this is called early from request initialization and makes sure we
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
59 have metadata (like what languages are available, direction of language)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
60 loaded into the global "languages".
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
61 The very first time, this will be slow as it will load all languages,
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
62 but next time it will be fast due to caching.
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
63 """
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
64 global languages
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
65 if languages is None:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
66 meta_cache = caching.CacheEntry(request, 'i18n', 'meta', scope='farm')
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
67 i18n_dir = os.path.join(request.cfg.moinmoin_dir, 'i18n', 'mo')
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
68 if meta_cache.needsUpdate(i18n_dir):
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
69 _languages = {}
896
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
70 for lang_file in glob.glob(po_filename(request, language='*', domain='MoinMoin')): # only MoinMoin domain for now XXX
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
71 language, domain, ext = os.path.basename(lang_file).split('.')
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
72 t = Translation(language, domain)
896
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
73 f = file(lang_file)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
74 t.load_po(f)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
75 f.close()
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
76 #request.log("load translation %r" % language)
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
77 encoding = 'utf-8'
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
78 _languages[language] = {}
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
79 for key, value in t.info.items():
896
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
80 #request.log("meta key %s value %r" % (key, value))
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
81 _languages[language][key] = value.decode(encoding)
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
82 meta_cache.update(pickle.dumps(_languages))
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
83
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
84 if languages is None: # another tread maybe has done it before us
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
85 _languages = pickle.loads(meta_cache.content())
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
86 if languages is None:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
87 languages = _languages
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
88
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
89
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
90 class Translation(object):
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
91 """ This class represents a translation. Usually this is a translation
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
92 from English original texts to a single language, like e.g. "de" (german).
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
93
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
94 The domain value defaults to 'MoinMoin' and this is reserved for
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
95 translation of the MoinMoin distribution. If you do a translation for
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
96 a third-party plugin, you have to use a different and unique value.
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
97 """
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
98 def __init__(self, language, domain='MoinMoin'):
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
99 self.language = language
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
100 self.domain = domain
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
101
896
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
102 def load_po(self, f):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
103 """ load the po file """
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
104 from StringIO import StringIO
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
105 from MoinMoin.i18n.msgfmt import MsgFmt
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
106 mf = MsgFmt()
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
107 mf.read_po(f.readlines())
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
108 mo_data = mf.generate_mo()
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
109 f = StringIO(mo_data)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
110 self.load_mo(f)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
111 f.close()
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
112
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
113 def load_mo(self, f):
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
114 """ load the mo file, setup some attributes from metadata """
717
ab91620af608 Fixed .mo loader for non-posix platforms.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 688
diff changeset
115 # binary files have to be opened in the binary file mode!
896
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
116 self.translation = gettext.GNUTranslations(f)
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
117 self.info = info = self.translation.info()
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
118 self.name = info['x-language']
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
119 self.ename = info['x-language-in-english']
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
120 self.direction = info['x-direction']
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
121 assert self.direction in ('ltr', 'rtl', )
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
122 self.maintainer = info['last-translator']
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
123
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
124 def formatMarkup(self, request, text, currentStack=[]):
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
125 """
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
126 Formats the text passed according to wiki markup.
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
127 This raises an exception if a text needs itself to be translated,
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
128 this could possibly happen with macros.
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
129 """
51
54d5932d5a03 merge moin--main--1.3--patch-930: fix error handling in plugins, fix broken chart action
Nir Soffer <nirs@freeshell.org>
parents: 42
diff changeset
130 try:
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
131 currentStack.index(text)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
132 raise Exception("Formatting a text that is being formatted?!")
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
133 except ValueError:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
134 pass
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
135 currentStack.append(text)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
136
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
137 from MoinMoin.Page import Page
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
138 from MoinMoin.parser.text_moin_wiki import Parser as WikiParser
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
139 from MoinMoin.formatter.text_html import Formatter
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
140 import StringIO
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
141
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
142 origtext = text
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
143 out = StringIO.StringIO()
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
144 request.redirect(out)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
145 parser = WikiParser(text, request, line_anchors=False)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
146 formatter = Formatter(request, terse=True)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
147 reqformatter = None
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
148 if hasattr(request, 'formatter'):
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
149 reqformatter = request.formatter
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
150 request.formatter = formatter
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
151 p = Page(request, "$$$$i18n$$$$")
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
152 formatter.setPage(p)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
153 parser.format(formatter)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
154 text = out.getvalue()
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
155 if reqformatter is None:
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
156 del request.formatter
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
157 else:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
158 request.formatter = reqformatter
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
159 request.redirect()
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
160 del currentStack[-1]
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
161 text = text.strip()
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
162 return text
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
163
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
164 def loadLanguage(self, request):
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
165 cache = caching.CacheEntry(request, arena='i18n', key=self.language, scope='farm')
896
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
166 langfilename = po_filename(request, self.language, self.domain)
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
167 needsupdate = cache.needsUpdate(langfilename)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
168 if debug:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
169 request.log("i18n: langfilename %s needsupdate %d" % (langfilename, needsupdate))
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
170 if not needsupdate:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
171 try:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
172 uc_texts, uc_unformatted = pickle.loads(cache.content())
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
173 except (IOError, ValueError, pickle.UnpicklingError): # bad pickle data, no pickle
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
174 if debug:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
175 request.log("i18n: pickle %s load failed" % lang)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
176 needsupdate = 1
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
177
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
178 if needsupdate:
896
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
179 f = file(langfilename)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
180 self.load_po(f)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
181 f.close()
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
182 trans = self.translation
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
183 texts = trans._catalog
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
184 has_wikimarkup = self.info.get('x-haswikimarkup', 'False') == 'True'
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
185 # convert to unicode
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
186 if debug:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
187 request.log("i18n: processing unformatted texts of lang %s" % lang)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
188 uc_unformatted = {}
489
9082a95430db fixed (wrong) cyclic usage exception
Thomas Waldmann <tw@waldmann-edv.de>
parents: 159
diff changeset
189 uc_texts = {}
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
190 for ukey, utext in texts.items():
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
191 uc_unformatted[ukey] = utext
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
192 if has_wikimarkup:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
193 # use the wiki parser now to replace some wiki markup with html
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
194 try:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
195 uc_texts[ukey] = self.formatMarkup(request, utext)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
196 except: # infinite recursion or crash
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
197 if debug:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
198 request.log("i18n: crashes in language %s on string: %s" % (lang, text))
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
199 uc_texts[ukey] = u"%s*" % utext
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
200 if debug:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
201 request.log("i18n: dumping lang %s" % lang)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
202 cache.update(pickle.dumps((uc_texts, uc_unformatted), PICKLE_PROTOCOL))
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
203
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
204 self.formatted = uc_texts
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
205 self.raw = uc_unformatted
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
206
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
207
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
208 def getDirection(lang):
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
209 """ Return the text direction for a language, either 'ltr' or 'rtl'. """
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
210 return languages[lang]['x-direction']
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
211
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
212 def getText(original, request, lang, formatted=True):
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
213 """ Return a translation of text in the user's language. """
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
214 if original == u"":
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
215 return u"" # we don't want to get *.po files metadata!
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
216
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
217 global translations
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
218 if not lang in translations: # load translation if needed
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
219 t = Translation(lang)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
220 t.loadLanguage(request)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
221 translations[lang] = t
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
222
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
223 # get the matching entry in the mapping table
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
224 translated = original
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
225 if formatted:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
226 trans_table = translations[lang].formatted
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
227 else:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
228 trans_table = translations[lang].raw
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
229 try:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
230 translated = trans_table[original]
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
231 except KeyError:
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
232 try:
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
233 from MoinMoin.Page import Page
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
234 language = languages[lang]['x-language-in-english']
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
235 dictpagename = "%sDict" % language
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
236 if Page(request, dictpagename).exists():
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
237 dicts = request.dicts
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
238 if dicts.has_dict(dictpagename):
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
239 userdict = dicts.dict(dictpagename)
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
240 translated = userdict[original]
839
715171e93d79 send original strings through the wiki formatter if we find no translation (backport to 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 717
diff changeset
241 else:
715171e93d79 send original strings through the wiki formatter if we find no translation (backport to 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 717
diff changeset
242 raise KeyError
715171e93d79 send original strings through the wiki formatter if we find no translation (backport to 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 717
diff changeset
243 else:
715171e93d79 send original strings through the wiki formatter if we find no translation (backport to 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 717
diff changeset
244 raise KeyError
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
245 except KeyError:
839
715171e93d79 send original strings through the wiki formatter if we find no translation (backport to 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 717
diff changeset
246 # do not simply return trans with str, but recursively call
715171e93d79 send original strings through the wiki formatter if we find no translation (backport to 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 717
diff changeset
247 # to get english translation, maybe formatted.
715171e93d79 send original strings through the wiki formatter if we find no translation (backport to 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 717
diff changeset
248 # if we don't find an english "translation", we just format it
715171e93d79 send original strings through the wiki formatter if we find no translation (backport to 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 717
diff changeset
249 # on the fly (this is needed for cfg.editor_quickhelp).
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
250 if lang != 'en':
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
251 translated = getText(original, request, 'en', formatted)
839
715171e93d79 send original strings through the wiki formatter if we find no translation (backport to 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 717
diff changeset
252 elif formatted:
715171e93d79 send original strings through the wiki formatter if we find no translation (backport to 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 717
diff changeset
253 translated = translations[lang].formatMarkup(request, original)
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
254 return translated
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
255
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
256
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
257 def requestLanguage(request):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
258 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
259 Return the user interface language for this request.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
260
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
261 The user interface language is taken from the user preferences for
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
262 registered users, or request environment, or the default language of
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
263 the wiki, or English.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
264
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
265 This should be called once per request, then you should get the value from
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
266 request object lang attribute.
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
267
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
268 Unclear what this means: "Until the code for get
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
269 text is fixed, we are caching the request language locally."
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
270
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
271 @param request: the request object
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
272 @keyword usecache: whether to get the value form the local cache or
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
273 actually look for it. This will update the cache data.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
274 @rtype: string
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
275 @return: ISO language code, e.g. 'en'
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
276 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
277 # Return the user language preferences for registered users
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
278 if request.user.valid and request.user.language:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
279 return request.user.language
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
280
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
281 # Or try to return one of the user browser accepted languages, if it
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
282 # is available on this wiki...
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
283 available = wikiLanguages()
131
1738cbba372c new cfg settings: language_ignore_browser and language_default (was: default_lang)
Thomas Waldmann <tw@waldmann-edv.de>
parents: 67
diff changeset
284 if not request.cfg.language_ignore_browser:
1738cbba372c new cfg settings: language_ignore_browser and language_default (was: default_lang)
Thomas Waldmann <tw@waldmann-edv.de>
parents: 67
diff changeset
285 for lang in browserLanguages(request):
551
17ab8fe4a94b refactored some d.has_key(k) to k in d (faster)
Thomas Waldmann <tw@waldmann-edv.de>
parents: 514
diff changeset
286 if lang in available:
131
1738cbba372c new cfg settings: language_ignore_browser and language_default (was: default_lang)
Thomas Waldmann <tw@waldmann-edv.de>
parents: 67
diff changeset
287 return lang
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
288
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
289 # Or return the wiki default language...
551
17ab8fe4a94b refactored some d.has_key(k) to k in d (faster)
Thomas Waldmann <tw@waldmann-edv.de>
parents: 514
diff changeset
290 if request.cfg.language_default in available:
131
1738cbba372c new cfg settings: language_ignore_browser and language_default (was: default_lang)
Thomas Waldmann <tw@waldmann-edv.de>
parents: 67
diff changeset
291 lang = request.cfg.language_default
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
292 # If everything else fails, read the manual... or return 'en'
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
293 else:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
294 lang = 'en'
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
295 return lang
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
296
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
297 def wikiLanguages():
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
298 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
299 Return the available user languages in this wiki.
159
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
300 As we do everything in unicode (or utf-8) now, everything is available.
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
301 """
159
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
302 return languages
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
303
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
304 def browserLanguages(request):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
305 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
306 Return the accepted languages as set in the user browser.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
307
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
308 Parse the HTTP headers and extract the accepted languages, according to:
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
309 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
310
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
311 Return a list of languages and base languages - as they are specified in
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
312 the request, normalizing to lower case.
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
313 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
314 fallback = []
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
315 accepted = request.http_accept_language
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
316 if accepted:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
317 # Extract the languages names from the string
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
318 accepted = accepted.split(',')
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
319 accepted = map(lambda x: x.split(';')[0], accepted)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
320 # Add base language for each sub language. If the user specified
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
321 # a sub language like "en-us", we will try to to provide it or
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
322 # a least the base language "en" in this case.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
323 for lang in accepted:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
324 lang = lang.lower()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
325 fallback.append(lang)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
326 if '-' in lang:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
327 baselang = lang.split('-')[0]
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
328 fallback.append(baselang)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
329 return fallback
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
330