Mercurial > p > roundup > code
comparison roundup/cgi/TranslationService.py @ 6200:718f205dbe50
pep8 fixes.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Fri, 19 Jun 2020 21:34:21 -0400 |
| parents | b0c2307be3d1 |
| children | 07ce4e4110f5 |
comparison
equal
deleted
inserted
replaced
| 6199:e860c6a30508 | 6200:718f205dbe50 |
|---|---|
| 18 from roundup.cgi.TAL import TALInterpreter | 18 from roundup.cgi.TAL import TALInterpreter |
| 19 from roundup.anypy.strings import us2u, u2s | 19 from roundup.anypy.strings import us2u, u2s |
| 20 | 20 |
| 21 ### Translation classes | 21 ### Translation classes |
| 22 | 22 |
| 23 | |
| 23 class TranslationServiceMixin: | 24 class TranslationServiceMixin: |
| 24 | 25 |
| 25 def translate(self, domain, msgid, mapping=None, | 26 def translate(self, domain, msgid, mapping=None, |
| 26 context=None, target_language=None, default=None | 27 context=None, target_language=None, default=None): |
| 27 ): | |
| 28 _msg = self.gettext(msgid) | 28 _msg = self.gettext(msgid) |
| 29 #print ("TRANSLATE", msgid, _msg, mapping, context) | 29 #print ("TRANSLATE", msgid, _msg, mapping, context) |
| 30 _msg = TALInterpreter.interpolate(_msg, mapping) | 30 _msg = TALInterpreter.interpolate(_msg, mapping) |
| 31 return _msg | 31 return _msg |
| 32 | 32 |
| 33 if hasattr(i18n.RoundupTranslations, 'ugettext'): | 33 if hasattr(i18n.RoundupTranslations, 'ugettext'): |
| 34 def gettext(self, msgid): | 34 def gettext(self, msgid): |
| 35 msgid = us2u(msgid) | 35 msgid = us2u(msgid) |
| 36 msgtrans=self.ugettext(msgid) | 36 msgtrans = self.ugettext(msgid) |
| 37 return u2s(msgtrans) | 37 return u2s(msgtrans) |
| 38 | 38 |
| 39 def ngettext(self, singular, plural, number): | 39 def ngettext(self, singular, plural, number): |
| 40 singular = us2u(singular) | 40 singular = us2u(singular) |
| 41 plural = us2u(plural) | 41 plural = us2u(plural) |
| 42 msgtrans=self.ungettext(singular, plural, number) | 42 msgtrans = self.ungettext(singular, plural, number) |
| 43 return u2s(msgtrans) | 43 return u2s(msgtrans) |
| 44 | |
| 44 | 45 |
| 45 class TranslationService(TranslationServiceMixin, i18n.RoundupTranslations): | 46 class TranslationService(TranslationServiceMixin, i18n.RoundupTranslations): |
| 46 pass | 47 pass |
| 47 | 48 |
| 49 | |
| 48 class NullTranslationService(TranslationServiceMixin, | 50 class NullTranslationService(TranslationServiceMixin, |
| 49 i18n.RoundupNullTranslations): | 51 i18n.RoundupNullTranslations): |
| 50 if hasattr(i18n.RoundupNullTranslations, 'ugettext'): | 52 if hasattr(i18n.RoundupNullTranslations, 'ugettext'): |
| 51 def ugettext(self, message): | 53 def ugettext(self, message): |
| 52 if self._fallback: | 54 if self._fallback: |
| 53 return self._fallback.ugettext(message) | 55 return self._fallback.ugettext(message) |
| 54 # Sometimes the untranslatable message is a UTF-8 encoded string | 56 # Sometimes the untranslatable message is a UTF-8 encoded string |
| 63 # to access request-dependent transalation service (with domain | 65 # to access request-dependent transalation service (with domain |
| 64 # and target language set during initializations of the roundup | 66 # and target language set during initializations of the roundup |
| 65 # client interface. | 67 # client interface. |
| 66 # | 68 # |
| 67 | 69 |
| 70 | |
| 68 class Context(TALES.Context): | 71 class Context(TALES.Context): |
| 69 | 72 |
| 70 def __init__(self, compiler, contexts): | 73 def __init__(self, compiler, contexts): |
| 71 TALES.Context.__init__(self, compiler, contexts) | 74 TALES.Context.__init__(self, compiler, contexts) |
| 72 if not self.contexts.get('i18n', None): | 75 if not self.contexts.get('i18n', None): |
| 78 def translate(self, domain, msgid, mapping=None, | 81 def translate(self, domain, msgid, mapping=None, |
| 79 context=None, target_language=None, default=None): | 82 context=None, target_language=None, default=None): |
| 80 if context is None: | 83 if context is None: |
| 81 context = self.contexts.get('here') | 84 context = self.contexts.get('here') |
| 82 return self.i18n.translate(domain, msgid, | 85 return self.i18n.translate(domain, msgid, |
| 83 mapping=mapping, context=context, default=default, | 86 mapping=mapping, context=context, |
| 84 target_language=target_language) | 87 default=default, |
| 88 target_language=target_language) | |
| 89 | |
| 85 | 90 |
| 86 class Engine(TALES.Engine): | 91 class Engine(TALES.Engine): |
| 87 | 92 |
| 88 def getContext(self, contexts=None, **kwcontexts): | 93 def getContext(self, contexts=None, **kwcontexts): |
| 89 if contexts is not None: | 94 if contexts is not None: |
| 91 kwcontexts.update(contexts) | 96 kwcontexts.update(contexts) |
| 92 else: | 97 else: |
| 93 kwcontexts = contexts | 98 kwcontexts = contexts |
| 94 return Context(self, kwcontexts) | 99 return Context(self, kwcontexts) |
| 95 | 100 |
| 101 | |
| 96 # patching TAL like this is a dirty hack, | 102 # patching TAL like this is a dirty hack, |
| 97 # but i see no other way to specify different Context class | 103 # but i see no other way to specify different Context class |
| 98 Expressions._engine = Engine(PathIterator.Iterator) | 104 Expressions._engine = Engine(PathIterator.Iterator) |
| 99 Expressions.installHandlers(Expressions._engine) | 105 Expressions.installHandlers(Expressions._engine) |
| 100 | 106 |
| 101 ### main API function | 107 ### main API function |
| 102 | 108 |
| 109 | |
| 103 def get_translation(language=None, tracker_home=None, | 110 def get_translation(language=None, tracker_home=None, |
| 104 translation_class=TranslationService, | 111 translation_class=TranslationService, |
| 105 null_translation_class=NullTranslationService | 112 null_translation_class=NullTranslationService): |
| 106 ): | |
| 107 """Return Translation object for given language and domain | 113 """Return Translation object for given language and domain |
| 108 | 114 |
| 109 Arguments 'translation_class' and 'null_translation_class' | 115 Arguments 'translation_class' and 'null_translation_class' |
| 110 specify the classes that are instantiated for existing | 116 specify the classes that are instantiated for existing |
| 111 and non-existing translations, respectively. | 117 and non-existing translations, respectively. |
| 112 """ | 118 """ |
| 113 return i18n.get_translation(language=language, | 119 return i18n.get_translation(language=language, |
| 114 tracker_home=tracker_home, | 120 tracker_home=tracker_home, |
| 115 translation_class=translation_class, | 121 translation_class=translation_class, |
| 116 null_translation_class=null_translation_class) | 122 null_translation_class=null_translation_class) |
| 117 | 123 |
| 118 # vim: set et sts=4 sw=4 : | 124 # vim: set et sts=4 sw=4 : |
