annotate roundup/cgi/TranslationService.py @ 5439:b00cd44fea16

Python 3 preparation: update string translate method call in cgi/accept_language.py. The str translate method in Python 3 is sufficiently different from that in Python 2 that different arguments are needed here. Note that the existing code "ascii = ''.join([chr(x) for x in range(256)])" is redundant with both versions (in Python 2, None can be used instead as the first translate argument from Python 2.6 onwards).
author Joseph Myers <jsm@polyomino.org.uk>
date Wed, 25 Jul 2018 11:44:01 +0000
parents 56c9bcdea47f
children 214f34e18678
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2556
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
1 # TranslationService for Roundup templates
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
2 #
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
3 # This module is free software, you may redistribute it
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
4 # and/or modify under the same terms as Python.
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
5 #
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
6 # This module provides National Language Support
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
7 # for Roundup templating - much like roundup.i18n
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
8 # module for Roundup command line interface.
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
9 # The only difference is that translator objects
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
10 # returned by get_translation() have one additional
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
11 # method which is used by TAL engines:
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
12 #
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
13 # translate(domain, msgid, mapping, context, target_language, default)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
14 #
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
15
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
16 from roundup import i18n
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
17 from roundup.cgi.PageTemplates import Expressions, PathIterator, TALES
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
18 from roundup.cgi.TAL import TALInterpreter
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 4570
diff changeset
19 from roundup.anypy.strings import us2u, u2s
2556
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
20
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
21 ### Translation classes
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
22
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
23 class TranslationServiceMixin:
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
24
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
25 def translate(self, domain, msgid, mapping=None,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
26 context=None, target_language=None, default=None
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
27 ):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
28 _msg = self.gettext(msgid)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
29 #print ("TRANSLATE", msgid, _msg, mapping, context)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
30 _msg = TALInterpreter.interpolate(_msg, mapping)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
31 return _msg
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
32
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
33 def gettext(self, msgid):
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 4570
diff changeset
34 msgid = us2u(msgid)
3978
f11fe2a13234 Fix for translations (patch [SF#2032526])
Richard Jones <richard@users.sourceforge.net>
parents: 3806
diff changeset
35 msgtrans=self.ugettext(msgid)
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 4570
diff changeset
36 return u2s(msgtrans)
2556
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
37
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
38 def ngettext(self, singular, plural, number):
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 4570
diff changeset
39 singular = us2u(singular)
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 4570
diff changeset
40 plural = us2u(plural)
3978
f11fe2a13234 Fix for translations (patch [SF#2032526])
Richard Jones <richard@users.sourceforge.net>
parents: 3806
diff changeset
41 msgtrans=self.ungettext(singular, plural, number)
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 4570
diff changeset
42 return u2s(msgtrans)
2556
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
43
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
44 class TranslationService(TranslationServiceMixin, i18n.RoundupTranslations):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
45 pass
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
46
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
47 class NullTranslationService(TranslationServiceMixin,
3775
beaf7ea86e5e handle 8-bit untranslateable messages in tracker templates
Richard Jones <richard@users.sourceforge.net>
parents: 2807
diff changeset
48 i18n.RoundupNullTranslations):
beaf7ea86e5e handle 8-bit untranslateable messages in tracker templates
Richard Jones <richard@users.sourceforge.net>
parents: 2807
diff changeset
49 def ugettext(self, message):
beaf7ea86e5e handle 8-bit untranslateable messages in tracker templates
Richard Jones <richard@users.sourceforge.net>
parents: 2807
diff changeset
50 if self._fallback:
beaf7ea86e5e handle 8-bit untranslateable messages in tracker templates
Richard Jones <richard@users.sourceforge.net>
parents: 2807
diff changeset
51 return self._fallback.ugettext(message)
beaf7ea86e5e handle 8-bit untranslateable messages in tracker templates
Richard Jones <richard@users.sourceforge.net>
parents: 2807
diff changeset
52 # Sometimes the untranslatable message is a UTF-8 encoded string
beaf7ea86e5e handle 8-bit untranslateable messages in tracker templates
Richard Jones <richard@users.sourceforge.net>
parents: 2807
diff changeset
53 # (thanks to PageTemplate's internals).
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 4570
diff changeset
54 message = us2u(message)
3775
beaf7ea86e5e handle 8-bit untranslateable messages in tracker templates
Richard Jones <richard@users.sourceforge.net>
parents: 2807
diff changeset
55 return message
2556
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
56
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
57 ### TAL patching
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
58 #
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
59 # Template Attribute Language (TAL) uses only global translation service,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
60 # which is not thread-safe. We will use context variable 'i18n'
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
61 # to access request-dependent transalation service (with domain
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
62 # and target language set during initializations of the roundup
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
63 # client interface.
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
64 #
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
65
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
66 class Context(TALES.Context):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
67
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
68 def __init__(self, compiler, contexts):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
69 TALES.Context.__init__(self, compiler, contexts)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
70 if not self.contexts.get('i18n', None):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
71 # if the context contains no TranslationService,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
72 # create default one
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
73 self.contexts['i18n'] = get_translation()
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
74 self.i18n = self.contexts['i18n']
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
75
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
76 def translate(self, domain, msgid, mapping=None,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
77 context=None, target_language=None, default=None):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
78 if context is None:
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
79 context = self.contexts.get('here')
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
80 return self.i18n.translate(domain, msgid,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
81 mapping=mapping, context=context, default=default,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
82 target_language=target_language)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
83
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
84 class Engine(TALES.Engine):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
85
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
86 def getContext(self, contexts=None, **kwcontexts):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
87 if contexts is not None:
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
88 if kwcontexts:
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
89 kwcontexts.update(contexts)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
90 else:
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
91 kwcontexts = contexts
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
92 return Context(self, kwcontexts)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
93
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
94 # patching TAL like this is a dirty hack,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
95 # but i see no other way to specify different Context class
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
96 Expressions._engine = Engine(PathIterator.Iterator)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
97 Expressions.installHandlers(Expressions._engine)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
98
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
99 ### main API function
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
100
2807
aa0316a1b2aa get_translation: removed 'domain' argument, added 'tracker_home' argument
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 2556
diff changeset
101 def get_translation(language=None, tracker_home=None,
2556
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
102 translation_class=TranslationService,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
103 null_translation_class=NullTranslationService
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
104 ):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
105 """Return Translation object for given language and domain
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
106
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
107 Arguments 'translation_class' and 'null_translation_class'
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
108 specify the classes that are instantiated for existing
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
109 and non-existing translations, respectively.
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
110 """
2807
aa0316a1b2aa get_translation: removed 'domain' argument, added 'tracker_home' argument
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 2556
diff changeset
111 return i18n.get_translation(language=language,
aa0316a1b2aa get_translation: removed 'domain' argument, added 'tracker_home' argument
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 2556
diff changeset
112 tracker_home=tracker_home,
2556
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
113 translation_class=translation_class,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
114 null_translation_class=null_translation_class)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
115
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
116 # vim: set et sts=4 sw=4 :

Roundup Issue Tracker: http://roundup-tracker.org/