annotate MoinMoin/i18n/__init__.py @ 4792:7a826f946da3

Groups2009: wikidicts were refactored. request.dict provides access only to WikiDicts. DictBase class was merged with Dict. Group class was removed. DictDict was merged with GroupDict removing methods related to the group functionality. The cache key for dicts changed from 'dicts_groups' to 'dicts'. wikidicts test was refined to capture new functionality. Changes in the other code to use DictDict.__contains__ instead of has_dict and DictDict.__getitem__ in place of dict.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Sun, 07 Jun 2009 16:04:05 +0200
parents f0c2ab8ef256
children c16381dd17fe
rev   line source
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
2 """
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
3 MoinMoin - internationalization (aka i18n)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
4
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
5 We use Python's gettext module for loading <language>.<domain>.mo files.
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
6 Domain is "MoinMoin" for MoinMoin distribution code and something else for
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
7 extension translations.
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
8
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
9 Public attributes:
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
10 languages -- dict of languages that MoinMoin knows metadata about
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
11
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
12 Public functions:
159
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
13 requestLanguage(request, usecache=1) -- return the request language
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
14 wikiLanguages() -- return the available wiki user languages
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
15 browserLanguages() -- return the browser accepted languages
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
16 getDirection(lang) -- return the lang direction either 'ltr' or 'rtl'
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
17 getText(str, request, lang, **kw) -- return str translation into lang
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2008
diff changeset
18
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
19 TODO: as soon as we have some "farm / server plugin dir", extend this to
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
20 load translations from there, too.
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
21
1918
bb2e053067fb fixing copyright headers: remove umlauts (encoding troubles), make epydoc compatible, reformat
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1866
diff changeset
22 @copyright: 2001-2004 Juergen Hermann <jh@web.de>,
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
23 2005-2008 MoinMoin:ThomasWaldmann
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
24 @license: GNU GPL, see COPYING for details.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
25 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
26
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
27 import os, gettext, glob
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
28 from StringIO import StringIO
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
29
3110
a48929a5036c logging: make it work correctly by doing logging configuration very early
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3107
diff changeset
30 from MoinMoin import log
a48929a5036c logging: make it work correctly by doing logging configuration very early
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3107
diff changeset
31 logging = log.getLogger(__name__)
3107
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3082
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 from MoinMoin import caching
4707
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
34 from MoinMoin.i18n import strings
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
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 # 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
37 # 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
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 # 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
40 # 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
41 languages = None
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
42
4707
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
43 # system_pages has a dictionary containing all english
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
44 # system page names and also all translated system pages names as keys,
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
45 # see also wikiutil.isSystemPage:
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
46 system_pages = {}
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
47
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
48 translations = {}
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
49
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
50 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
51 """ 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
52
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
53 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
54 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
55 """
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
56 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
57
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 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
59 """ 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
60 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
61 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
62 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
63 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
64 """
9fb4124ea499 use python's gettext module 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 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
66 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
67 if languages is None:
3124
5d7582e47c50 i18n logging: remove 'i18n:' from the log msg, channel and line no are done by logging framework
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3121
diff changeset
68 logging.debug("trying to load translations from cache")
4515
04f3cfbf446a use per-wiki i18n cache (fixes wrong links to other farm wikis)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4342
diff changeset
69 # the scope of the i18n cache needs to be per-wiki, because some translations
04f3cfbf446a use per-wiki i18n cache (fixes wrong links to other farm wikis)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4342
diff changeset
70 # have http links (to some help pages) and they must not point to another
04f3cfbf446a use per-wiki i18n cache (fixes wrong links to other farm wikis)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4342
diff changeset
71 # wiki in the farm (confusing and maybe not even readable due to ACLs):
04f3cfbf446a use per-wiki i18n cache (fixes wrong links to other farm wikis)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4342
diff changeset
72 meta_cache = caching.CacheEntry(request, 'i18n', 'meta', scope='wiki', 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
73 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
74 if meta_cache.needsUpdate(i18n_dir):
3124
5d7582e47c50 i18n logging: remove 'i18n:' from the log msg, channel and line no are done by logging framework
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3121
diff changeset
75 logging.debug("cache needs update")
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
76 _languages = {}
4707
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
77 _system_pages = {}
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
78 for pagename in strings.all_pages:
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
79 _system_pages[pagename] = ('en', pagename)
1832
b180f047d918 comments only: cleaned up TODO, XXX, ...
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1793
diff changeset
80 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
81 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
82 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
83 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
84 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
85 f.close()
3124
5d7582e47c50 i18n logging: remove 'i18n:' from the log msg, channel and line no are done by logging framework
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3121
diff changeset
86 logging.debug("loading translation %r" % language)
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
87 encoding = 'utf-8'
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
88 _languages[language] = {}
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
89 for key, value in t.info.items():
3124
5d7582e47c50 i18n logging: remove 'i18n:' from the log msg, channel and line no are done by logging framework
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3121
diff changeset
90 #logging.debug("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
91 _languages[language][key] = value.decode(encoding)
4707
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
92 for pagename in strings.all_pages:
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
93 try:
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
94 pagename_translated = t.translation._catalog[pagename]
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
95 except KeyError:
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
96 pass
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
97 else:
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
98 _system_pages[pagename_translated] = (language, pagename)
3124
5d7582e47c50 i18n logging: remove 'i18n:' from the log msg, channel and line no are done by logging framework
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3121
diff changeset
99 logging.debug("dumping language metadata to disk cache")
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1629
diff changeset
100 try:
4707
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
101 meta_cache.update({
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
102 'languages': _languages,
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
103 'system_pages': _system_pages,
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
104 })
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1629
diff changeset
105 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
106 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
107
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
108 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
109 try:
3124
5d7582e47c50 i18n logging: remove 'i18n:' from the log msg, channel and line no are done by logging framework
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3121
diff changeset
110 logging.debug("loading language metadata from disk cache")
4707
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
111 d = meta_cache.content()
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
112 if languages is None:
4707
6acb48a16e32 use i18n.strings for wikiutil.isSystemPage()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4516
diff changeset
113 globals().update(d)
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
114 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
115 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
116 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
117
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
118 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
119 """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
120
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
121 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
122
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
123 @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
124 """
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
125 translations = {}
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
126 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
127 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
128
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
129 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
130 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
131 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
132 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
133 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
134 f.close()
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
135 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
136 translations[language] = {}
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
137
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
138 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
139 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
140
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
141 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
142
9fb4124ea499 use python's gettext module 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 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
144 """ 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
145 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
146
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
147 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
148 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
149 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
150 """
9fb4124ea499 use python's gettext module 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 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
152 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
153 self.domain = domain
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
154
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
155 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
156 """ 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
157 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
158 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
159 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
160 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
161 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
162 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
163 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
164
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
165 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
166 """ 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
167 # 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
168 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
169 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
170 try:
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
171 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
172 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
173 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
174 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
175 except KeyError, err:
3161
5a304e55a945 logging: fix i18n logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3143
diff changeset
176 logging.warning("metadata problem in %r: %s" % (self.language, str(err)))
3074
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
177 try:
18cba7defa5a i18n: avoid crashing, log errors in po files, update i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3067
diff changeset
178 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
179 except (AttributeError, AssertionError), err:
3161
5a304e55a945 logging: fix i18n logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3143
diff changeset
180 logging.warning("direction problem in %r: %s" % (self.language, str(err)))
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
181
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
182 def formatMarkup(self, request, text, percent):
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
183 """ Formats the text using the wiki parser/formatter.
3120
a7c1680aeaa0 PEP8 fixes, mostly whitespace
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
184
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
185 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
186 this could possibly happen with macros.
3120
a7c1680aeaa0 PEP8 fixes, mostly whitespace
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
187
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
188 @param request: the request object
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
189 @param text: the text to format
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
190 @param percent: True if result is used as left-side of a % operator and
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
191 thus any GENERATED % needs to be escaped as %%.
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
192 """
3124
5d7582e47c50 i18n logging: remove 'i18n:' from the log msg, channel and line no are done by logging framework
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3121
diff changeset
193 logging.debug("formatting: %r" % text)
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
194
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
195 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
196 from MoinMoin.parser.text_moin_wiki import Parser as WikiParser
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
197 if percent:
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
198 from MoinMoin.formatter.text_html_percent import Formatter
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
199 else:
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
200 from MoinMoin.formatter.text_html import Formatter
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
201
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
202 out = StringIO()
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
203 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
204 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
205 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
206 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
207 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
208 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
209 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
210 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
211 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
212 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
213 text = out.getvalue()
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
214 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
215 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
216 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
217 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
218 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
219 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
220 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
221
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2286
diff changeset
222 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
223 request.clock.start('loadLanguage')
4515
04f3cfbf446a use per-wiki i18n cache (fixes wrong links to other farm wikis)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4342
diff changeset
224 # see comment about per-wiki scope above
04f3cfbf446a use per-wiki i18n cache (fixes wrong links to other farm wikis)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4342
diff changeset
225 cache = caching.CacheEntry(request, arena='i18n', key=self.language, scope='wiki', 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
226 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
227 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
228 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
229 try:
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
230 unformatted = cache.content()
3124
5d7582e47c50 i18n logging: remove 'i18n:' from the log msg, channel and line no are done by logging framework
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3121
diff changeset
231 logging.debug("pickle %s load success" % self.language)
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1629
diff changeset
232 except caching.CacheError:
3124
5d7582e47c50 i18n logging: remove 'i18n:' from the log msg, channel and line no are done by logging framework
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3121
diff changeset
233 logging.debug("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
234 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
235
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
236 if needsupdate:
3124
5d7582e47c50 i18n logging: remove 'i18n:' from the log msg, channel and line no are done by logging framework
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3121
diff changeset
237 logging.debug("langfilename %s needs update" % langfilename)
896
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 889
diff changeset
238 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
239 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
240 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
241 trans = self.translation
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
242 unformatted = trans._catalog
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
243 self.has_wikimarkup = self.info.get('x-haswikimarkup', 'False') == 'True'
3124
5d7582e47c50 i18n logging: remove 'i18n:' from the log msg, channel and line no are done by logging framework
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3121
diff changeset
244 logging.debug("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
245 try:
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
246 cache.update(unformatted)
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1629
diff changeset
247 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
248 pass
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
249
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
250 self.formatted = {}
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
251 self.raw = unformatted
1130
787e963c9bfd fix Clock code, add more timers (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 941
diff changeset
252 request.clock.stop('loadLanguage')
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 839
diff changeset
253
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
254
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
255 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
256 """ 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
257 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
258
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
259 def getText(original, request, lang, **kw):
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
260 """ Return a translation of some original text.
3120
a7c1680aeaa0 PEP8 fixes, mostly whitespace
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
261
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
262 @param original: the original (english) text
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
263 @param request: the request object
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
264 @lang: the target language for the translation
3121
ca1c60a5285c i18n.getText: renaming the 'formatted' (default True) keyword arg to 'wiki' (default False)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3120
diff changeset
265 @keyword wiki: True to use the wiki parser/formatter on the translation result,
ca1c60a5285c i18n.getText: renaming the 'formatted' (default True) keyword arg to 'wiki' (default False)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3120
diff changeset
266 False to return the translation result "as is"
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
267 @keyword percent: True if we need special escaping because we use the translation
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
268 result as the left side of a % operator: e.g. % chars need to
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
269 become %% for that usage. This will only escape generated % chars,
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
270 e.g. in wiki links to non-ascii pagenames (%XX%XX%XX).
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
271 False, if we don't use it as a left-side of % operator.
3143
16ae95df840a i18n.getText: fixed some callers that had still 'formatted=' kw args
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3124
diff changeset
272 Only specify this option for wiki==True, it doesn't do
16ae95df840a i18n.getText: fixed some callers that had still 'formatted=' kw args
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3124
diff changeset
273 anything for wiki==False.
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
274 """
3121
ca1c60a5285c i18n.getText: renaming the 'formatted' (default True) keyword arg to 'wiki' (default False)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3120
diff changeset
275 formatted = kw.get('wiki', False) # 1.6 and early 1.7 (until 2/2008) used 'formatted' with True as default!
ca1c60a5285c i18n.getText: renaming the 'formatted' (default True) keyword arg to 'wiki' (default False)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3120
diff changeset
276 percent = kw.get('percent', False)
688
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
277 if original == u"":
15c55ecd7ccb fix some i18n bugs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 686
diff changeset
278 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
279
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
280 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
281 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
282 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
283 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
284 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
285
9fb4124ea499 use python's gettext module 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 # 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
287 translated = original
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
288 translation = translations[lang]
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
289 if original in translation.raw:
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
290 translated = translation.raw[original]
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
291 if formatted:
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
292 key = (original, percent)
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
293 if key in translation.formatted:
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
294 translated = translation.formatted[key]
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
295 if translated is None:
3124
5d7582e47c50 i18n logging: remove 'i18n:' from the log msg, channel and line no are done by logging framework
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3121
diff changeset
296 logging.error("formatting a %r text that is already being formatted: %r" % (lang, original))
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
297 translated = original + u'*' # get some error indication to the UI
3076
3fec4c9c0bf8 i18n: format text only when requested, better logging (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3075
diff changeset
298 else:
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
299 translation.formatted[key] = None # we use this as "formatting in progress" indicator
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
300 translated = translation.formatMarkup(request, translated, percent)
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
301 translation.formatted[key] = translated # remember it
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
302 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
303 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
304 language = languages[lang]['x-language-in-english']
4786
f0c2ab8ef256 Groups2009: Merge with the second head to get rid of it. No changes from df833ab68df2 are merged.
Dmitrijs Milajevs <dimazest@gmail.com>
parents: 4776
diff changeset
305 dictpagename = "%sDict" % language.replace(' ', '')
f0c2ab8ef256 Groups2009: Merge with the second head to get rid of it. No changes from df833ab68df2 are merged.
Dmitrijs Milajevs <dimazest@gmail.com>
parents: 4776
diff changeset
306 dicts = request.dicts
4792
7a826f946da3 Groups2009: wikidicts were refactored. request.dict provides access only to WikiDicts.
Dmitrijs Milajevs <dimazest@gmail.com>
parents: 4786
diff changeset
307 if dictpagename in dicts:
7a826f946da3 Groups2009: wikidicts were refactored. request.dict provides access only to WikiDicts.
Dmitrijs Milajevs <dimazest@gmail.com>
parents: 4786
diff changeset
308 userdict = dicts[dictpagename]
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
309 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
310 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
311 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
312 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
313 # 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
314 # 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
315 # if we don't find an english "translation", we just format it
3784
951ed1df8ec3 move editor quickhelp out of config into parser modules
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3491
diff changeset
316 # on the fly (this is needed for 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
317 if lang != 'en':
3161
5a304e55a945 logging: fix i18n logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3143
diff changeset
318 logging.debug("falling back to english, requested string not in %r translation: %r" % (lang, original))
3143
16ae95df840a i18n.getText: fixed some callers that had still 'formatted=' kw args
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3124
diff changeset
319 translated = getText(original, request, 'en', wiki=formatted, percent=percent)
3081
a869096c582e i18n: trying to fix recursion in formatMarkup in a cleaner way (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3077
diff changeset
320 elif formatted: # and lang == 'en'
3124
5d7582e47c50 i18n logging: remove 'i18n:' from the log msg, channel and line no are done by logging framework
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3121
diff changeset
321 logging.debug("formatting for %r on the fly: %r" % (lang, original))
3077
66d35649ca99 add 'percent' kw to getText (must be True when using result as left side of % operator), refactored some code, better comments (ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3076
diff changeset
322 translated = translations[lang].formatMarkup(request, original, percent)
684
9fb4124ea499 use python's gettext module and binary *.mo files, cache metadata directly from the modular translation files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
323 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
324
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
325
2008
52c474b8c02f allow translating things early
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 1918
diff changeset
326 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
327 """
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
328 Return the user interface language for this request.
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2008
diff changeset
329
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
330 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
331 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
332 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
333
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
334 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
335 request object lang attribute.
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2008
diff changeset
336
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
337 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
338 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
339
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
340 @param request: the request object
2008
52c474b8c02f allow translating things early
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 1918
diff changeset
341 @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
342 @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
343 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
344 @rtype: string
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
345 @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
346 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
347 # 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
348 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
349 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
350
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
351 # 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
352 # is available on this wiki...
3082
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
353 lang = get_browser_language(request)
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
354 if not lang:
4340
615b785f9a3f i18n.__init__: bug fix for wiki language not installed (MoinMoinBugs/WikiLanguageNotDefined)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4062
diff changeset
355 available = wikiLanguages() or ["en"]
3082
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
356 # Or return the wiki default language...
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
357 if request.cfg.language_default in available:
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
358 lang = request.cfg.language_default
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
359 # If everything else fails, read the manual... or return 'en'
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
360 else:
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
361 lang = 'en'
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
362 return lang
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
363
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
364
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
365 def wikiLanguages():
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
366 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
367 Return the available user languages in this wiki.
159
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
368 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
369 """
159
74bcce9d3dae i18n code cleanup
Thomas Waldmann <tw@waldmann-edv.de>
parents: 131
diff changeset
370 return languages
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
371
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
372
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
373 def browserLanguages(request):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
374 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
375 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
376
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
377 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
378 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
379
514
fc74944187ad i18n: cosmetic fixes, removed unused fn
Thomas Waldmann <tw@waldmann-edv.de>
parents: 489
diff changeset
380 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
381 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
382 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
383 fallback = []
4221
f37c12f53bf2 Fixed: use werkzeugs accept_languages header property
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 3784
diff changeset
384 accepted = request.accept_languages
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
385 if accepted:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
386 # 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
387 # 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
388 # a least the base language "en" in this case.
4221
f37c12f53bf2 Fixed: use werkzeugs accept_languages header property
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 3784
diff changeset
389 for lang, quality in accepted:
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
390 lang = lang.lower()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
391 fallback.append(lang)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
392 if '-' in lang:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
393 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
394 fallback.append(baselang)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
395 return fallback
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
396
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
397 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
398 """
3120
a7c1680aeaa0 PEP8 fixes, mostly whitespace
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
399 Return the language that is supported by wiki and what user browser
3082
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
400 would prefer to get. Return empty string if there is no such language
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
401 or language_ignore_browser is true.
3032
c242772754c5 stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3028
diff changeset
402
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
403 @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
404 @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
405 @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
406 """
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
407 available = wikiLanguages()
3491
9fabe3f8a0d8 i18n.__init__.get_browser_language: bug fix if user selected lang is not installed (default languages is None)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3161
diff changeset
408 if available and not request.cfg.language_ignore_browser:
9fabe3f8a0d8 i18n.__init__.get_browser_language: bug fix if user selected lang is not installed (default languages is None)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3161
diff changeset
409 for lang in browserLanguages(request):
9fabe3f8a0d8 i18n.__init__.get_browser_language: bug fix if user selected lang is not installed (default languages is None)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3161
diff changeset
410 if lang in available:
9fabe3f8a0d8 i18n.__init__.get_browser_language: bug fix if user selected lang is not installed (default languages is None)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3161
diff changeset
411 return lang
3082
307abb1cf26a fix for MoinMoinBugs/1.6.0LanguageIgnoreBrowserConfigurationError (thanks for Ninnnu for the patch, ported from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3081
diff changeset
412 return ''
3120
a7c1680aeaa0 PEP8 fixes, mostly whitespace
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
413