[svn] small changes in the i18n system of jinja
authorArmin Ronacher <armin.ronacher@active-4.com>
Sun, 4 Mar 2007 19:26:45 +0000 (20:26 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sun, 4 Mar 2007 19:26:45 +0000 (20:26 +0100)
--HG--
branch : trunk

jinja/datastructure.py
jinja/translators/python.py

index e936165b5a77077b2803d48e4f0cf62ca96a5e58..0e7a76f13a4faa404c378fda7ca9d0c79fb351df 100644 (file)
@@ -73,6 +73,20 @@ class UndefinedType(object):
 Undefined = UndefinedType()
 
 
+class FakeTranslator(object):
+    """
+    Default null translator.
+    """
+
+    def gettext(self, s):
+        return s
+
+    def ngettext(self, s, p, n):
+        if n == 1:
+            return s
+        return p
+
+
 class Deferred(object):
     """
     Object marking an deferred value. Deferred objects are
@@ -114,7 +128,7 @@ class Context(object):
 
     def get_translator(self):
         """Return the translator for i18n."""
-        return lambda a, b, c: a
+        return FakeTranslator()
 
     def pop(self):
         if len(self._stack) <= 2:
index aa3dd2400ef529780be921565a025ef0b98d9d5e..4998f15e2282f7a7363ea6374c23606262a40983 100644 (file)
@@ -227,13 +227,19 @@ class PythonTranslator(Translator):
             '    apply_filters = environment.apply_filters\n'
             '    call_function = environment.call_function\n'
             '    call_function_simple = environment.call_function_simple\n'
-            '    finish_var = environment.finish_var\n'
+            '    finish_var = environment.finish_var'
         ]
         self.indention = 1
         rv = self.handle_node_list(node)
 
         if self.require_translations:
-            lines.append('    translate = context.get_translator()')
+            lines.append(
+                '    translator = context.get_translator()\n'
+                '    def translate(s, p=None, n=None, r=None):\n'
+                '        if p is None:\n'
+                '            return translator.gettext(s) % (r or {})\n'
+                '        return translator.ngettext(s, p, r[n]) % (r or {})'
+            )
         lines.append(rv)
 
         return '\n'.join(lines)
@@ -460,14 +466,10 @@ class PythonTranslator(Translator):
             replacements = '{%s}' % ', '.join(replacements)
         else:
             replacements = 'None'
-        if node.indicator is not None:
-            indicator = 'context[\'%s\']' % node.indicator
-        else:
-            indicator = 'None'
-        return self.indent('write(translate(%r, %r, %s) %% %s)' % (
+        return self.indent('write(translate(%r, %r, %r, %s))' % (
             node.singular,
             node.plural,
-            indicator,
+            node.indicator,
             replacements
         ))