Mercurial > p > roundup > code
comparison roundup/cgi/TranslationService.py @ 5416:56c9bcdea47f
Python 3 preparation: unicode.
This patch introduces roundup/anypy/strings.py, which has a comment
explaining the string representations generally used and common
functions to handle the required conversions. Places in the code that
explicitly reference the "unicode" type / built-in function are
generally changed to use the new functions (or, in a few places where
those new functions don't seem to fit well, other approaches such as
references to type(u'') or use of the codecs module). This patch does
not generally attempt to address text conversions in any places not
currently referencing the "unicode" type (although
scripts/import_sf.py is made to use binary I/O in places as fixing the
"unicode" reference didn't seem coherent otherwise).
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Wed, 25 Jul 2018 09:05:58 +0000 |
| parents | 6e3e4f24c753 |
| children | 214f34e18678 |
comparison
equal
deleted
inserted
replaced
| 5415:2d6a92c3e212 | 5416:56c9bcdea47f |
|---|---|
| 14 # | 14 # |
| 15 | 15 |
| 16 from roundup import i18n | 16 from roundup import i18n |
| 17 from roundup.cgi.PageTemplates import Expressions, PathIterator, TALES | 17 from roundup.cgi.PageTemplates import Expressions, PathIterator, TALES |
| 18 from roundup.cgi.TAL import TALInterpreter | 18 from roundup.cgi.TAL import TALInterpreter |
| 19 from roundup.anypy.strings import us2u, u2s | |
| 19 | 20 |
| 20 ### Translation classes | 21 ### Translation classes |
| 21 | 22 |
| 22 class TranslationServiceMixin: | 23 class TranslationServiceMixin: |
| 23 | |
| 24 OUTPUT_ENCODING = "utf-8" | |
| 25 | 24 |
| 26 def translate(self, domain, msgid, mapping=None, | 25 def translate(self, domain, msgid, mapping=None, |
| 27 context=None, target_language=None, default=None | 26 context=None, target_language=None, default=None |
| 28 ): | 27 ): |
| 29 _msg = self.gettext(msgid) | 28 _msg = self.gettext(msgid) |
| 30 #print ("TRANSLATE", msgid, _msg, mapping, context) | 29 #print ("TRANSLATE", msgid, _msg, mapping, context) |
| 31 _msg = TALInterpreter.interpolate(_msg, mapping) | 30 _msg = TALInterpreter.interpolate(_msg, mapping) |
| 32 return _msg | 31 return _msg |
| 33 | 32 |
| 34 def gettext(self, msgid): | 33 def gettext(self, msgid): |
| 35 if not isinstance(msgid, unicode): | 34 msgid = us2u(msgid) |
| 36 msgid = unicode(msgid, 'utf8') | |
| 37 msgtrans=self.ugettext(msgid) | 35 msgtrans=self.ugettext(msgid) |
| 38 return msgtrans.encode(self.OUTPUT_ENCODING) | 36 return u2s(msgtrans) |
| 39 | 37 |
| 40 def ngettext(self, singular, plural, number): | 38 def ngettext(self, singular, plural, number): |
| 41 if not isinstance(singular, unicode): | 39 singular = us2u(singular) |
| 42 singular = unicode(singular, 'utf8') | 40 plural = us2u(plural) |
| 43 if not isinstance(plural, unicode): | |
| 44 plural = unicode(plural, 'utf8') | |
| 45 msgtrans=self.ungettext(singular, plural, number) | 41 msgtrans=self.ungettext(singular, plural, number) |
| 46 return msgtrans.encode(self.OUTPUT_ENCODING) | 42 return u2s(msgtrans) |
| 47 | 43 |
| 48 class TranslationService(TranslationServiceMixin, i18n.RoundupTranslations): | 44 class TranslationService(TranslationServiceMixin, i18n.RoundupTranslations): |
| 49 pass | 45 pass |
| 50 | 46 |
| 51 class NullTranslationService(TranslationServiceMixin, | 47 class NullTranslationService(TranslationServiceMixin, |
| 53 def ugettext(self, message): | 49 def ugettext(self, message): |
| 54 if self._fallback: | 50 if self._fallback: |
| 55 return self._fallback.ugettext(message) | 51 return self._fallback.ugettext(message) |
| 56 # Sometimes the untranslatable message is a UTF-8 encoded string | 52 # Sometimes the untranslatable message is a UTF-8 encoded string |
| 57 # (thanks to PageTemplate's internals). | 53 # (thanks to PageTemplate's internals). |
| 58 if not isinstance(message, unicode): | 54 message = us2u(message) |
| 59 return unicode(message, 'utf8') | |
| 60 return message | 55 return message |
| 61 | 56 |
| 62 ### TAL patching | 57 ### TAL patching |
| 63 # | 58 # |
| 64 # Template Attribute Language (TAL) uses only global translation service, | 59 # Template Attribute Language (TAL) uses only global translation service, |
