Skip to content
Myr

Added i18n support to django2jinja (not complete support, but the best you can get before django fixes a bug in i18n handling)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent dcc217c · Browse files · 3 files+115 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+115 added−2 removed

  • other:+115 added−2 removed

3 files+115 added−2 removed

modified:ext/django2jinja/templates/index.html+21 added
modified:ext/django2jinja/django2jinja.py+92 added-1 removed
43 unmodified lines
44import sys45from jinja2.defaults import *46from django.conf import settings47from django.template import defaulttags as core_tags, loader, TextNode, \48 FilterExpression, libraries, Variable, loader_tags48 FilterExpression, libraries, Variable, loader_tags, TOKEN_TEXT, \49 TOKEN_VAR50from django.template.debug import DebugVariableNode as VariableNode51from django.templatetags import i18n as i18n_tags52from StringIO import StringIO535455_node_handlers = {}
259 unmodified lines
315316317@node(Variable)318def variable(writer, node):319 if node.translate:320 writer.warn('i18n system used, make sure to install translations', node)321 writer.write('_(')322 if node.literal is not None:323 writer.literal(node.literal)324 else:325 writer.variable(node.var)326 if node.translate:327 writer.write(')')328329330@node(VariableNode)331def variable_node(writer, node):
261 unmodified lines
593def warn_load(writer, node):594 writer.warn('load statement used which was ignored on conversion', node)595596597@node(i18n_tags.GetAvailableLanguagesNode)598def get_available_languages(writer, node):599 writer.warn('make sure to provide a get_available_languages function', node)600 writer.tag('set %s = get_available_languages()' %601 writer.translate_variable_name(node.variable))602603604@node(i18n_tags.GetCurrentLanguageNode)605def get_current_language(writer, node):606 writer.warn('make sure to provide a get_current_language function', node)607 writer.tag('set %s = get_current_language()' %608 writer.translate_variable_name(node.variable))609610611@node(i18n_tags.GetCurrentLanguageBidiNode)612def get_current_language_bidi(writer, node):613 writer.warn('make sure to provide a get_current_language_bidi function', node)614 writer.tag('set %s = get_current_language_bidi()' %615 writer.translate_variable_name(node.variable))616617618@node(i18n_tags.TranslateNode)619def simple_gettext(writer, node):620 writer.warn('i18n system used, make sure to install translations', node)621 writer.start_variable()622 writer.write('_(')623 writer.node(node.value)624 writer.write(')')625 writer.end_variable()626627628@node(i18n_tags.BlockTranslateNode)629def translate_block(writer, node):630 first_var = []631 variables = set()632633 def touch_var(name):634 variables.add(name)635 if not first_var:636 first_var.append(name)637638 def dump_token_list(tokens):639 for token in tokens:640 if token.token_type == TOKEN_TEXT:641 writer.write(token.contents)642 elif token.token_type == TOKEN_VAR:643 writer.print_expr(token.contents)644 touch_var(token.contents)645646 writer.warn('i18n system used, make sure to install translations', node)647 writer.start_block()648 writer.write('trans')649 idx = -1650 for idx, (key, var) in enumerate(node.extra_context.items()):651 if idx:652 writer.write(',')653 writer.write(' %s=' % key)654 touch_var(key)655 writer.node(var.filter_expression)656657 have_plural = False658 plural_var = None659 if node.plural and node.countervar and node.counter:660 have_plural = True661 plural_var = node.countervar662 if plural_var not in variables:663 if idx > -1:664 writer.write(',')665 touch_var(plural_var)666 writer.write(' %s=' % plural_var)667 writer.node(node.counter)668669 writer.end_block()670 dump_token_list(node.singular)671 if node.plural and node.countervar and node.counter:672 writer.start_block()673 writer.write('pluralize')674 if node.countervar != first_var[0]:675 writer.write(' ' + node.countervar)676 writer.end_block()677 dump_token_list(node.plural)678 writer.tag('endtrans')679680681# get rid of node now, it shouldn't be used normally682del node
modified:ext/django2jinja/example.py+2 added-1 removed
1from django.conf import settings2settings.configure(TEMPLATE_DIRS=['templates'], TEMPLATE_DEBUG=True)34from django2jinja import convert_templates, Writer54settings.configure(TEMPLATE_DIRS=['templates'], TEMPLATE_DEBUG=True)6writer = Writer(use_jinja_autoescape=True)7convert_templates('converted', writer=writer)