changeset 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)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 22 Feb 2008 22:35:46 +0100
parents 3fec4c9c0bf8
children be729248d9f5
files MoinMoin/action/fullsearch.py MoinMoin/formatter/text_html_percent.py MoinMoin/i18n/__init__.py MoinMoin/request/__init__.py
diffstat 4 files changed, 64 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/fullsearch.py	Fri Feb 22 22:31:19 2008 +0100
+++ b/MoinMoin/action/fullsearch.py	Fri Feb 22 22:35:46 2008 +0100
@@ -213,7 +213,7 @@
 
         request.theme.add_msg(_('Your search query {{{"%s"}}} didn\'t return any results. '
                 'Please change some terms and refer to HelpOnSearching for '
-                'more information.%s') % (wikiutil.escape(needle),
+                'more information.%s', formatted=True, percent=True) % (wikiutil.escape(needle),
                     titlesearch and ''.join([
                         '<br>',
                         _('(!) Consider performing a'), ' ',
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/formatter/text_html_percent.py	Fri Feb 22 22:35:46 2008 +0100
@@ -0,0 +1,24 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - a special text/html formatter used by the i18n system
+    
+    If texts translated by the i18n system are used on the left side of a
+    % operator, any markup-generated % char (e.g. in links to non-ASCII page
+    names (%XX%XX%XX)) needs to get escaped (%%XX%%XX%%XX).
+
+    Everything else is as in the text/html formatter.
+
+    @copyright: 2007 MoinMoin:ThomasWaldmann
+    @license: GNU GPL, see COPYING for details.
+"""
+
+from MoinMoin.formatter.text_html import Formatter as TextHtmlFormatter
+
+class Formatter(TextHtmlFormatter):
+
+    def _open(self, tag, newline=False, attr=None, allowed_attrs=None, **kw):
+        """ Escape % signs in tags, see also text_html.Formatter._open. """
+        tagstr = TextHtmlFormatter._open(self, tag, newline, attr, allowed_attrs, **kw)
+        return tagstr.replace('%', '%%')
+
+    # override more methods, if needed
--- a/MoinMoin/i18n/__init__.py	Fri Feb 22 22:31:19 2008 +0100
+++ b/MoinMoin/i18n/__init__.py	Fri Feb 22 22:35:46 2008 +0100
@@ -155,11 +155,16 @@
         except (AttributeError, AssertionError), err:
             logging.debug("load_mo: %r %s" % (self.language, str(err)))
 
-    def formatMarkup(self, request, text, currentStack=[]):
-        """
-        Formats the text passed according to wiki markup.
+    def formatMarkup(self, request, text, percent, currentStack=[]):
+        """ Formats the text using the wiki parser/formatter.
+        
         This raises an exception if a text needs itself to be translated,
         this could possibly happen with macros.
+        
+        @param request: the request object
+        @param text: the text to format
+        @param percent: True if result is used as left-side of a % operator and
+                        thus any GENERATED % needs to be escaped as %%.
         """
         try:
             currentStack.index(text)
@@ -172,7 +177,10 @@
 
         from MoinMoin.Page import Page
         from MoinMoin.parser.text_moin_wiki import Parser as WikiParser
-        from MoinMoin.formatter.text_html import Formatter
+        if percent:
+            from MoinMoin.formatter.text_html_percent import Formatter
+        else:
+            from MoinMoin.formatter.text_html import Formatter
 
         out = StringIO()
         request.redirect(out)
@@ -231,8 +239,24 @@
     """ Return the text direction for a language, either 'ltr' or 'rtl'. """
     return languages[lang]['x-direction']
 
-def getText(original, request, lang, formatted=True):
-    """ Return a translation of text in the user's language. """
+def getText(original, request, lang, **kw):
+    """ Return a translation of some original text.
+    
+    @param original: the original (english) text
+    @param request: the request object
+    @lang: the target language for the translation
+    @keyword formatted: True to use the wiki parser/formatter on the translation result,
+                        False to return the translation result "as is"
+    @keyword percent: True if we need special escaping because we use the translation
+                      result as the left side of a % operator: e.g. % chars need to
+                      become %% for that usage. This will only escape generated % chars,
+                      e.g. in wiki links to non-ascii pagenames (%XX%XX%XX).
+                      False, if we don't use it as a left-side of % operator.
+                      Only specify this option for formatted==True, it doesn't do
+                      anything for formatted==False. 
+    """
+    formatted = kw.get('formatted', True) # TODO: change to False, review all _() calls
+    percent = kw.get('percent', False) # TODO: review all _() calls
     if original == u"":
         return u"" # we don't want to get *.po files metadata!
 
@@ -246,14 +270,13 @@
     translated = original
     translation = translations[lang]
     if original in translation.raw:
-        if not formatted:
-            translated = translation.raw[original]
-        else:
-            if original in translation.formatted:
-                translated = translation.formatted[original]
+        translated = translation.raw[original]
+        if formatted:
+            if (original, percent) in translation.formatted:
+                translated = translation.formatted[(original, percent)]
             else:
-                translated = translation.formatMarkup(request, original)
-                translation.formatted[original] = translated # remember it
+                translated = translation.formatMarkup(request, translated, percent)
+                translation.formatted[(original, percent)] = translated # remember it
     else:
         try:
             language = languages[lang]['x-language-in-english']
@@ -271,9 +294,9 @@
             # on the fly (this is needed for cfg.editor_quickhelp).
             if lang != 'en':
                 logging.debug("i18n: fallback to english, requested string not in %r translation: %r" % (lang, original))
-                translated = getText(original, request, 'en', formatted)
+                translated = getText(original, request, 'en', formatted=formatted, percent=percent)
             elif formatted:
-                translated = translations[lang].formatMarkup(request, original)
+                translated = translations[lang].formatMarkup(request, original, percent)
     return translated
 
 
--- a/MoinMoin/request/__init__.py	Fri Feb 22 22:31:19 2008 +0100
+++ b/MoinMoin/request/__init__.py	Fri Feb 22 22:35:46 2008 +0100
@@ -237,7 +237,7 @@
             # Language for content. Page content should use the wiki default lang,
             # but generated content like search results should use the user language.
             self.content_lang = self.cfg.language_default
-            self.getText = lambda text, i18n=self.i18n, request=self, lang=self.lang, **kv: i18n.getText(text, request, lang, kv.get('formatted', True))
+            self.getText = lambda text, i18n=self.i18n, request=self, lang=self.lang, **kv: i18n.getText(text, request, lang, **kv)
 
             self.reset()