annotate MoinMoin/i18n/__init__.py @ 3074:18cba7defa5a

i18n: avoid crashing, log errors in po files, update i18n
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 18 Sep 2007 19:48:35 +0200
parents 7fa08b9b8a90
children 8f7097ad7755
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
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
5 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
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'
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
17 getText(str, request) -- return str translation
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>,
bb2e053067fb fixing copyright headers: remove umlauts (encoding troubles), make epydoc compatible, reformat
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1866
diff changeset
23 2005-2006 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 """
489
9082a95430db fixed (wrong) cyclic usage exception
Thomas Waldmann <tw@waldmann-edv.de>
parents: 159
diff changeset
26 debug = 0
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
27
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
28 import os, gettext, glob
3074
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
29 import logging
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
30
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
31 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
32
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
33 # 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
34 # 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
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 # 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
37 # 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
38 languages = None
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 translations = {}
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
41
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
42 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
43 """ 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
44
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
45 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
46 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
47 """
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
48 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
49
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
50 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
51 """ 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
52 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
53 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
54 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
55 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
56 """
9fb4124ea499 use python's gettext module 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 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
58 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
59 if languages is None:
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
60 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
61 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
62 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
63 _languages = {}
1832
b180f047d918 comments only: cleaned up TODO, XXX, ...
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1793
diff changeset
64 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
65 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
66 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
67 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
68 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
69 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
70 #request.log("load translation %r" % language)
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
71 encoding = 'utf-8'
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
72 _languages[language] = {}
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
73 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
74 #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
75 _languages[language][key] = value.decode(encoding)
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1629
diff changeset
76 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
77 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
78 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
79 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
80
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
81 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
82 try:
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
83 _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
84 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
85 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
86 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
87 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
88 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
89
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
90 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
91 """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
92
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
93 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
94
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
95 @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
96 """
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
97 translations = {}
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
98 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
99 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
100
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
101 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
102 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
103 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
104 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
105 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
106 f.close()
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
107 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
108 translations[language] = {}
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 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
111 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
112
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
113 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
114
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
115 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
116 """ 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
117 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
118
9fb4124ea499 use python's gettext module 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 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
120 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
121 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
122 """
9fb4124ea499 use python's gettext module 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 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
124 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
125 self.domain = domain
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
126
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
127 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
128 """ 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
129 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
130 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
131 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
132 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
133 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
134 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
135 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
136 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
137
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 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
139 """ 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
140 # 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
141 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
142 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
143 try:
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
144 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
145 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
146 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
147 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
148 except KeyError, err:
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
149 logging.debug("%r %s" % (self.language, str(err)))
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
150 try:
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
151 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
152 except (AttributeError, AssertionError), err:
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
153 logging.debug("%r %s" % (self.language, str(err)))
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
154
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
155 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
156 """
9fb4124ea499 use python's gettext module 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 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
158 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
159 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
160 """
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
161 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
162 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
163 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
164 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
165 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
166 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
167
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
168 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
169 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
170 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
171 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
172
9fb4124ea499 use python's gettext module 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 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
174 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
175 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
176 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
177 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
178 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
179 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
180 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
181 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
182 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
183 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
184 text = out.getvalue()
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
185 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
186 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
187 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
188 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
189 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
190 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
191 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
192 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
193
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
194 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
195 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
196 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
197 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
198 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
199 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
200 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
201 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
202 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
203 uc_texts, uc_unformatted = cache.content()
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1629
diff changeset
204 except caching.CacheError:
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
205 if debug:
1791
6dd2e29acffe Eclipse PyDev Check: fixed lots of its errors and warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1638
diff changeset
206 request.log("i18n: 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
207 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
208
9fb4124ea499 use python's gettext module 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 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
210 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
211 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
212 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
213 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
214 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
215 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
216 # 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
217 if debug:
1791
6dd2e29acffe Eclipse PyDev Check: fixed lots of its errors and warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1638
diff changeset
218 request.log("i18n: processing unformatted texts of lang %s" % 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
219 uc_unformatted = {}
489
9082a95430db fixed (wrong) cyclic usage exception
Thomas Waldmann <tw@waldmann-edv.de>
parents: 159
diff changeset
220 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
221 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
222 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
223 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
224 # 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
225 try:
1516
6900a7906244 added a comment about recursion in i18n code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1131
diff changeset
226 uc_texts[ukey] = self.formatMarkup(request, utext) # XXX RECURSION!!! Calls gettext via markup
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
227 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
228 if debug:
1791
6dd2e29acffe Eclipse PyDev Check: fixed lots of its errors and warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1638
diff changeset
229 request.log("i18n: crashes in language %s on string: %s" % (self.language, utext))
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
230 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
231 if debug:
1791
6dd2e29acffe Eclipse PyDev Check: fixed lots of its errors and warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1638
diff changeset
232 request.log("i18n: 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
233 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
234 cache.update((uc_texts, uc_unformatted))
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1629
diff changeset
235 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
236 pass
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
237
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
238 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
239 self.raw = uc_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
240 request.clock.stop('loadLanguage')
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
241
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
242
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
243 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
244 """ 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
245 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
246
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
247 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
248 """ 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
249 if original == u"":
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
250 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
251
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
252 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
253 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
254 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
255 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
256 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
257
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
258 # 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
259 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
260 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
261 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
262 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
263 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
264 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
265 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
266 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
267 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
268 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
269 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
270 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
271 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
272 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
273 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
274 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
275 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
276 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
277 # 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
278 # 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
279 # 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
280 # 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
281 if lang != 'en':
3067
7fa08b9b8a90 adjusting log levels (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3032
diff changeset
282 logging.debug("i18n: fallback to english, 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
283 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
284 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
285 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
286 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
287
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
288
2008
52c474b8c02f allow translating things early
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 1918
diff changeset
289 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
290 """
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
291 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
292
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
293 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
294 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
295 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
296
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
297 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
298 request object lang attribute.
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2008
diff changeset
299
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
300 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
301 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
302
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
303 @param request: the request object
2008
52c474b8c02f allow translating things early
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 1918
diff changeset
304 @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
305 @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
306 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
307 @rtype: string
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
308 @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
309 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
310 # 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
311 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
312 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
313
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
314 # 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
315 # is available on this wiki...
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
316 return get_browser_language(request)
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
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 # 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
319 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
320 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
321 # 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
322 else:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
323 lang = 'en'
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
324 return lang
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
325
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
326
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
327 def wikiLanguages():
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
328 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
329 Return the available user languages in this wiki.
159
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
330 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
331 """
159
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
332 return languages
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
333
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
334
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
335 def browserLanguages(request):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
336 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
337 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
338
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
339 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
340 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
341
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
342 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
343 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
344 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
345 fallback = []
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
346 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
347 if accepted:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
348 # 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
349 accepted = accepted.split(',')
1866
0194beaf511e reduce reduce, filter and map usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1832
diff changeset
350 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
351 # 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
352 # 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
353 # 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
354 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
355 lang = lang.lower()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
356 fallback.append(lang)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
357 if '-' in lang:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
358 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
359 fallback.append(baselang)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
360 return fallback
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
361
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
362 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
363 """
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
364 Return the language that is supported by wiki and what user 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
365 would prefer to get. Return nothing if there is no such language.
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
366
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
367 @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
368 @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
369 @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
370 """
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
371 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
372 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
373 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
374 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
375 return lang