From 6c66888d3f0dd1b1e58cdcde33ef9354262b87ec Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 4 Mar 2007 20:26:45 +0100 Subject: [PATCH] [svn] small changes in the i18n system of jinja --HG-- branch : trunk --- jinja/datastructure.py | 16 +++++++++++++++- jinja/translators/python.py | 18 ++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/jinja/datastructure.py b/jinja/datastructure.py index e936165..0e7a76f 100644 --- a/jinja/datastructure.py +++ b/jinja/datastructure.py @@ -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: diff --git a/jinja/translators/python.py b/jinja/translators/python.py index aa3dd24..4998f15 100644 --- a/jinja/translators/python.py +++ b/jinja/translators/python.py @@ -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 )) -- 2.26.2