annotate MoinMoin/i18n/__init__.py @ 3067:7fa08b9b8a90

adjusting log levels (ported from 1.6)
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Fri, 22 Feb 2008 17:28:21 +0100
parents c242772754c5
children 18cba7defa5a
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
9fb4124ea499 use python's gettext module 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
9fb4124ea499 use python's gettext module 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 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
31
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
32 # 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
33 # 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
34
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
35 # 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
36 # 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
37 languages = None
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
38
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
39 translations = {}
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
40
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
41 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
42 """ 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
43
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
44 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
45 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
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 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
48
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
49 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
50 """ 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
51 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
52 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
53 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
54 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
55 """
9fb4124ea499 use python's gettext module 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 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
57 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
58 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
59 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
60 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
61 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
62 _languages = {}
1832
b180f047d918 comments only: cleaned up TODO, XXX, ...
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1793
diff changeset
63 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
64 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
65 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
66 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
67 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
68 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
69 #request.log("load translation %r" % language)
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
70 encoding = 'utf-8'
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
71 _languages[language] = {}
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
72 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
73 #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
74 _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
75 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
76 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
77 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
78 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
79
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
80 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
81 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
82 _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
83 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
84 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
85 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
86 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
87 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
88
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
89 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
90 """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
91
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
92 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
93
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
94 @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
95 """
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
96 translations = {}
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
97 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
98 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
99
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
100 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
101 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
102 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
103 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
104 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
105 f.close()
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
106 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
107 translations[language] = {}
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
108
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
109 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
110 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
111
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
112 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
113
9fb4124ea499 use python's gettext module 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 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
115 """ 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
116 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
117
9fb4124ea499 use python's gettext module 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 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
119 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
120 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
121 """
9fb4124ea499 use python's gettext module 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 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
123 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
124 self.domain = domain
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
125
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
126 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
127 """ 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
128 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
129 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
130 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
131 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
132 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
133 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
134 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
135 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
136
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 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
138 """ 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
139 # 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
140 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
141 self.info = info = self.translation.info()
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
142 self.name = info['x-language']
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
143 self.ename = info['x-language-in-english']
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
144 self.direction = info['x-direction']
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
145 assert self.direction in ('ltr', 'rtl', )
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
146 self.maintainer = info['last-translator']
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
147
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
148 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
149 """
9fb4124ea499 use python's gettext module 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 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
151 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
152 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
153 """
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
154 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
155 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
156 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
157 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
158 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
159 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
160
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
161 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
162 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
163 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
164 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
165
9fb4124ea499 use python's gettext module 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 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
167 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
168 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
169 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
170 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
171 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
172 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
173 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
174 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
175 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
176 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
177 text = out.getvalue()
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
178 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
179 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
180 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
181 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
182 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
183 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
184 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
185 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
186
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
187 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
188 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
189 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
190 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
191 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
192 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
193 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
194 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
195 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
196 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
197 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
198 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
199 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
200 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
201
9fb4124ea499 use python's gettext module 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 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
203 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
204 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
205 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
206 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
207 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
208 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
209 # 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
210 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
211 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
212 uc_unformatted = {}
489
9082a95430db fixed (wrong) cyclic usage exception
Thomas Waldmann <tw@waldmann-edv.de>
parents: 159
diff changeset
213 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
214 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
215 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
216 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
217 # 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
218 try:
1516
6900a7906244 added a comment about recursion in i18n code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1131
diff changeset
219 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
220 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
221 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
222 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
223 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
224 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
225 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
226 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
227 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
228 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
229 pass
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
230
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
231 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
232 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
233 request.clock.stop('loadLanguage')
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
234
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
235
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
236 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
237 """ 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
238 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
239
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
240 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
241 """ 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
242 if original == u"":
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
243 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
244
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
245 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
246 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
247 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
248 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
249 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
250
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
251 # 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
252 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
253 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
254 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
255 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
256 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
257 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
258 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
259 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
260 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
261 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
262 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
263 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
264 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
265 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
266 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
267 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
268 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
269 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
270 # 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
271 # 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
272 # 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
273 # 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
274 if lang != 'en':
3067
7fa08b9b8a90 adjusting log levels (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3032
diff changeset
275 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
276 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
277 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
278 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
279 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
280
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
281
2008
52c474b8c02f allow translating things early
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 1918
diff changeset
282 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
283 """
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
284 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
285
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
286 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
287 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
288 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
289
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
290 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
291 request object lang attribute.
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 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
294 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
295
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
296 @param request: the request object
2008
52c474b8c02f allow translating things early
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 1918
diff changeset
297 @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
298 @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
299 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
300 @rtype: string
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
301 @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
302 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
303 # 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
304 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
305 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
306
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
307 # 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
308 # 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
309 return get_browser_language(request)
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
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 # 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
312 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
313 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
314 # 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
315 else:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
316 lang = 'en'
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
317 return lang
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
318
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
319
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
320 def wikiLanguages():
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
321 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
322 Return the available user languages in this wiki.
159
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
323 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
324 """
159
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
325 return languages
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
326
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
327
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
328 def browserLanguages(request):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
329 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
330 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
331
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
332 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
333 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
334
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
335 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
336 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
337 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
338 fallback = []
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
339 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
340 if accepted:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
341 # 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
342 accepted = accepted.split(',')
1866
0194beaf511e reduce reduce, filter and map usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1832
diff changeset
343 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
344 # 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
345 # 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
346 # 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
347 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
348 lang = lang.lower()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
349 fallback.append(lang)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
350 if '-' in lang:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
351 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
352 fallback.append(baselang)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
353 return fallback
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
354
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
355 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
356 """
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
357 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
358 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
359
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
360 @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
361 @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
362 @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
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 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
365 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
366 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
367 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
368 return lang