annotate MoinMoin/i18n/__init__.py @ 3107:c6e39279f83b

refactor logging usage
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 23 Feb 2008 22:59:30 +0100
parents 307abb1cf26a
children a48929a5036c
rev   line source
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1 # -*- 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
2 """
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
3 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
4
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
5 We use Python's gettext module for loading <language>.<domain>.mo files.
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 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
7 extension translations.
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
8
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
9 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
10 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
11
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
12 Public functions:
159
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
13 requestLanguage(request, usecache=1) -- return the request language
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
14 wikiLanguages() -- return the available wiki user languages
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
15 browserLanguages() -- return the browser accepted languages
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
16 getDirection(lang) -- return the lang direction either 'ltr' or 'rtl'
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
17 getText(str, request, lang, **kw) -- return str translation into lang
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2008
diff changeset
18
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
19 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
20 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
21
1918
bb2e053067fb fixing copyright headers: remove umlauts (encoding troubles), make epydoc compatible, reformat
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1866
diff changeset
22 @copyright: 2001-2004 Juergen Hermann <jh@web.de>,
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
23 2005-2008 MoinMoin:ThomasWaldmann
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
24 @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
25 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
26
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
27 import os, gettext, glob
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
28 from StringIO import StringIO
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
3107
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3082
diff changeset
30 from MoinMoin.server import getLogger
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3082
diff changeset
31 logging = getLogger(__name__)
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3082
diff changeset
32
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3082
diff changeset
33 # use this to temporarily and selectively enable debug logging for this module
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3082
diff changeset
34 #logging.setLevel(logging.DEBUG)
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3082
diff changeset
35
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
36 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
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 # 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
39 # 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
40
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
41 # 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
42 # 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
43 languages = None
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
44
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
45 translations = {}
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
46
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
47 def po_filename(request, language, domain, i18n_dir='i18n'):
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
48 """ we use MoinMoin/i18n/<language>[.<domain>].mo as filename for the PO file.
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2008
diff changeset
49
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
50 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
51 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
52 """
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
53 return os.path.join(request.cfg.moinmoin_dir, i18n_dir, "%s.%s.po" % (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
54
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
55 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
56 """ 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
57 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
58 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
59 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
60 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
61 """
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 global languages
1638
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
63 request.clock.start('i18n_init')
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
64 if languages is None:
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
65 logging.debug("i18n_init: trying to load translations from cache")
1638
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
66 meta_cache = caching.CacheEntry(request, 'i18n', 'meta', scope='farm', use_pickle=True)
1131
67044a2a0224 add some more timers, fix wrong path in i18n_init
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1130
diff changeset
67 i18n_dir = os.path.join(request.cfg.moinmoin_dir, 'i18n')
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
68 if meta_cache.needsUpdate(i18n_dir):
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
69 logging.debug("i18n_init: cache needs update")
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
70 _languages = {}
1832
b180f047d918 comments only: cleaned up TODO, XXX, ...
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1793
diff changeset
71 for lang_file in glob.glob(po_filename(request, language='*', domain='MoinMoin')): # XXX only MoinMoin domain for now
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
72 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
73 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
74 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
75 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
76 f.close()
3075
8f7097ad7755 i18n: improved debug logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3074
diff changeset
77 logging.debug("i18n_init: loading translation %r" % language)
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
78 encoding = 'utf-8'
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
79 _languages[language] = {}
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
80 for key, value in t.info.items():
3075
8f7097ad7755 i18n: improved debug logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3074
diff changeset
81 #logging.debug("i18n_init: 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
82 _languages[language][key] = value.decode(encoding)
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
83 logging.debug("i18n_init: dumping language metadata to disk cache")
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1629
diff changeset
84 try:
1638
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
85 meta_cache.update(_languages)
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1629
diff changeset
86 except caching.CacheError:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1629
diff changeset
87 pass
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
88
1638
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
89 if languages is None: # another thread maybe has done it before us
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
90 try:
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
91 logging.debug("i18n_init: loading language metadata from disk cache")
1638
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
92 _languages = meta_cache.content()
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
93 if languages is None:
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
94 languages = _languages
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
95 except caching.CacheError:
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
96 pass
1131
67044a2a0224 add some more timers, fix wrong path in i18n_init
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1130
diff changeset
97 request.clock.stop('i18n_init')
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
98
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
99 def bot_translations(request):
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
100 """Return translations to be used by notification bot
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
101
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
102 This is called by XML RPC code.
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
103
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
104 @return: a dict (indexed by language) of dicts of translated strings (indexed by original ones)
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
105 """
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
106 translations = {}
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
107 po_dir = os.path.join('i18n', 'jabberbot')
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
108 encoding = 'utf-8'
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
109
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
110 for lang_file in glob.glob(po_filename(request, i18n_dir=po_dir, language='*', domain='JabberBot')):
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
111 language, domain, ext = os.path.basename(lang_file).split('.')
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
112 t = Translation(language, domain)
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
113 f = file(lang_file)
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
114 t.load_po(f)
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
115 f.close()
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
116 t.loadLanguage(request, trans_dir=po_dir)
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
117 translations[language] = {}
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
118
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
119 for key, text in t.raw.items():
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
120 translations[language][key] = text
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
121
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
122 return translations
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
123
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 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
125 """ 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
126 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
127
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 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
129 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
130 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
131 """
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 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
133 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
134 self.domain = domain
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
135
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
136 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
137 """ 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
138 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
139 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
140 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
141 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
142 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
143 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
144 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
145
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
146 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
147 """ 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
148 # 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
149 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
150 self.info = info = self.translation.info()
3074
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
151 try:
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
152 self.name = info['x-language']
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
153 self.ename = info['x-language-in-english']
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
154 self.direction = info['x-direction']
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
155 self.maintainer = info['last-translator']
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
156 except KeyError, err:
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
157 logging.debug("load_mo: %r %s" % (self.language, str(err)))
3074
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
158 try:
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
159 assert self.direction in ('ltr', 'rtl', )
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
160 except (AttributeError, AssertionError), err:
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
161 logging.debug("load_mo: %r %s" % (self.language, str(err)))
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
162
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
163 def formatMarkup(self, request, text, percent):
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
164 """ Formats the text using the wiki parser/formatter.
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
165
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
166 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
167 this could possibly happen with macros.
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
168
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
169 @param request: the request object
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
170 @param text: the text to format
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
171 @param percent: True if result is used as left-side of a % operator and
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
172 thus any GENERATED % needs to be escaped as %%.
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
173 """
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
174 logging.debug("formatMarkup: %r" % text)
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
175
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
176 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
177 from MoinMoin.parser.text_moin_wiki import Parser as WikiParser
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
178 if percent:
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
179 from MoinMoin.formatter.text_html_percent import Formatter
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
180 else:
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
181 from MoinMoin.formatter.text_html import Formatter
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
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
183 out = StringIO()
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
184 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
185 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
186 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
187 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
188 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
189 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
190 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
191 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
192 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
193 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
194 text = out.getvalue()
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
195 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
196 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
197 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
198 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
199 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
200 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
201 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
202
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
203 def loadLanguage(self, request, trans_dir="i18n"):
1130
787e963c9bfd fix Clock code, add more timers (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 941
diff changeset
204 request.clock.start('loadLanguage')
1638
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
205 cache = caching.CacheEntry(request, arena='i18n', key=self.language, scope='farm', use_pickle=True)
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
206 langfilename = po_filename(request, self.language, self.domain, i18n_dir=trans_dir)
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
207 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
208 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
209 try:
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
210 unformatted = cache.content()
3075
8f7097ad7755 i18n: improved debug logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3074
diff changeset
211 logging.debug("loadLanguage: pickle %s load success" % self.language)
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1629
diff changeset
212 except caching.CacheError:
3075
8f7097ad7755 i18n: improved debug logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3074
diff changeset
213 logging.debug("loadLanguage: pickle %s load failed" % self.language)
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
214 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
215
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
216 if needsupdate:
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
217 logging.debug("loadLanguage: langfilename %s needs update" % langfilename)
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
218 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
219 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
220 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
221 trans = self.translation
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
222 unformatted = trans._catalog
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
223 self.has_wikimarkup = self.info.get('x-haswikimarkup', 'False') == 'True'
3075
8f7097ad7755 i18n: improved debug logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3074
diff changeset
224 logging.debug("loadLanguage: dumping lang %s" % self.language)
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1629
diff changeset
225 try:
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
226 cache.update(unformatted)
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1629
diff changeset
227 except caching.CacheError:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1629
diff changeset
228 pass
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
229
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
230 self.formatted = {}
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
231 self.raw = unformatted
1130
787e963c9bfd fix Clock code, add more timers (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 941
diff changeset
232 request.clock.stop('loadLanguage')
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
233
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
234
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
235 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
236 """ 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
237 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
238
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
239 def getText(original, request, lang, **kw):
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
240 """ Return a translation of some original text.
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
241
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
242 @param original: the original (english) text
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
243 @param request: the request object
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
244 @lang: the target language for the translation
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
245 @keyword formatted: True to use the wiki parser/formatter on the translation result,
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
246 False to return the translation result "as is"
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
247 @keyword percent: True if we need special escaping because we use the translation
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
248 result as the left side of a % operator: e.g. % chars need to
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
249 become %% for that usage. This will only escape generated % chars,
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
250 e.g. in wiki links to non-ascii pagenames (%XX%XX%XX).
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
251 False, if we don't use it as a left-side of % operator.
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
252 Only specify this option for formatted==True, it doesn't do
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
253 anything for formatted==False.
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
254 """
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
255 formatted = kw.get('formatted', True) # TODO: change to False, review all _() calls
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
256 percent = kw.get('percent', False) # TODO: review all _() calls
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
257 if original == u"":
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
258 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
259
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
260 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
261 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
262 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
263 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
264 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
265
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
266 # 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
267 translated = original
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
268 translation = translations[lang]
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
269 if original in translation.raw:
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
270 translated = translation.raw[original]
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
271 if formatted:
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
272 key = (original, percent)
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
273 if key in translation.formatted:
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
274 translated = translation.formatted[key]
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
275 if translated is None:
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
276 logging.error("i18n: formatting a %r text that is already being formatted: %r" % (lang, original))
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
277 translated = original + u'*' # get some error indication to the UI
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
278 else:
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
279 translation.formatted[key] = None # we use this as "formatting in progress" indicator
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
280 translated = translation.formatMarkup(request, translated, percent)
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
281 translation.formatted[key] = translated # remember it
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
282 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
283 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
284 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
285 dictpagename = "%sDict" % language
1629
2efb597c14d1 i18n via *Dict pages: we have them in request.dicts, no need to check for .exists()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1516
diff changeset
286 dicts = request.dicts
2efb597c14d1 i18n via *Dict pages: we have them in request.dicts, no need to check for .exists()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1516
diff changeset
287 if dicts.has_dict(dictpagename):
2efb597c14d1 i18n via *Dict pages: we have them in request.dicts, no need to check for .exists()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1516
diff changeset
288 userdict = dicts.dict(dictpagename)
2efb597c14d1 i18n via *Dict pages: we have them in request.dicts, no need to check for .exists()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1516
diff changeset
289 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
290 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
291 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
292 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
293 # 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
294 # 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
295 # 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
296 # on the fly (this is needed for cfg.editor_quickhelp).
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
297 logging.debug("i18n: requested string not in %r translation: %r" % (lang, 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
298 if lang != 'en':
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
299 logging.debug("i18n: falling back from %r to english" % lang)
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
300 translated = getText(original, request, 'en', formatted=formatted, percent=percent)
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
301 elif formatted: # and lang == 'en'
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
302 logging.debug("i18n: formatting for %r on the fly: %r" % (lang, original))
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
303 translated = translations[lang].formatMarkup(request, original, percent)
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
304 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
305
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
306
2008
52c474b8c02f allow translating things early
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 1918
diff changeset
307 def requestLanguage(request, try_user=True):
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2008
diff changeset
308 """
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
309 Return the user interface language for this request.
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2008
diff changeset
310
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
311 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
312 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
313 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
314
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
315 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
316 request object lang attribute.
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2008
diff changeset
317
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
318 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
319 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
320
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
321 @param request: the request object
2008
52c474b8c02f allow translating things early
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 1918
diff changeset
322 @param try_user: try getting language from request.user
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
323 @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
324 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
325 @rtype: string
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
326 @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
327 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
328 # Return the user language preferences for registered users
2008
52c474b8c02f allow translating things early
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 1918
diff changeset
329 if try_user and request.user.valid and request.user.language:
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
330 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
331
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
332 # 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
333 # is available on this wiki...
3082
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
334 lang = get_browser_language(request)
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
335 if not lang:
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
336 available = wikiLanguages()
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
337 # Or return the wiki default language...
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
338 if request.cfg.language_default in available:
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
339 lang = request.cfg.language_default
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
340 # If everything else fails, read the manual... or return 'en'
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
341 else:
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
342 lang = 'en'
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
343 return lang
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
344
3032
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
345
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
346 def wikiLanguages():
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
347 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
348 Return the available user languages in this wiki.
159
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
349 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
350 """
159
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
351 return languages
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
352
3032
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
353
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
354 def browserLanguages(request):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
355 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
356 Return the accepted languages as set in the user browser.
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2008
diff changeset
357
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
358 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
359 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
360
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
361 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
362 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
363 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
364 fallback = []
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
365 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
366 if accepted:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
367 # 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
368 accepted = accepted.split(',')
1866
0194beaf511e reduce reduce, filter and map usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1832
diff changeset
369 accepted = [lang.split(';')[0] for lang in accepted]
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
370 # 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
371 # 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
372 # 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
373 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
374 lang = lang.lower()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
375 fallback.append(lang)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
376 if '-' in lang:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
377 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
378 fallback.append(baselang)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
379 return fallback
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
380
3032
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
381 def get_browser_language(request):
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
382 """
3082
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
383 Return the language that is supported by wiki and what user browser
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
384 would prefer to get. Return empty string if there is no such language
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
385 or language_ignore_browser is true.
3032
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
386
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
387 @param request: the request object
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
388 @rtype: string
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
389 @return: ISO language code, e.g. 'en'
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
390 """
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
391 available = wikiLanguages()
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
392 if not request.cfg.language_ignore_browser:
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
393 for lang in browserLanguages(request):
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
394 if lang in available:
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
395 return lang
3082
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
396 return ''