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
Provenance
- Commit
1b54f74- Tree
74494c3- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
dcc217c
3 files changed+115 added−2 removed
modified:ext/django2jinja/templates/index.html+21 added
12345626 unmodified lines3334353637383940414243444546474849505152535455565758{% extends "layout.html" %}{% load i18n %}{% block title %}Foo{% endblock %}{% block page-body %} {{ block.super }} Hello {{ name|cut:"d" }}!26 unmodified lines {% endwith %}
<pre>{% debug %}</pre>
{% blocktrans with book|title as book_t and author|title as author_t %} This is {{ book_t }} by {{ author_t }} {% endblocktrans %}
{% blocktrans count list|length as counter %} There is only one {{ name }} object. {% plural %} There are {{ counter }} {{ name }} objects. {% endblocktrans %}
{% blocktrans with name|escape as name count list|length as counter %} There is only one {{ name }} object. {% plural %} There are {{ counter }} {{ name }} objects. {% endblocktrans %}
{% blocktrans %}This string will have {{ value }} inside.{% endblocktrans %}
<p>{% trans "This is the title." %}</p>
{% regroup people by gender as grouped %}{% endblock %}modified:ext/django2jinja/django2jinja.py+92 added-1 removed
43 unmodified lines44 import sys45 from jinja2.defaults import *46 from django.conf import settings47 from django.template import defaulttags as core_tags, loader, TextNode, \48- FilterExpression, libraries, Variable, loader_tags48+ FilterExpression, libraries, Variable, loader_tags, TOKEN_TEXT, \49+ TOKEN_VAR50 from django.template.debug import DebugVariableNode as VariableNode51+from django.templatetags import i18n as i18n_tags52 from StringIO import StringIO53 54 55 _node_handlers = {}259 unmodified lines315 316 317 @node(Variable)318 def 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(')')328 329 330 @node(VariableNode)331 def variable_node(writer, node):261 unmodified lines593 def warn_load(writer, node):594 writer.warn('load statement used which was ignored on conversion', node)595 596 597+@node(i18n_tags.GetAvailableLanguagesNode)598+def 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))602+603+604+@node(i18n_tags.GetCurrentLanguageNode)605+def 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))609+610+611+@node(i18n_tags.GetCurrentLanguageBidiNode)612+def 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))616+617+618+@node(i18n_tags.TranslateNode)619+def 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()626+627+628+@node(i18n_tags.BlockTranslateNode)629+def translate_block(writer, node):630+ first_var = []631+ variables = set()632+633+ def touch_var(name):634+ variables.add(name)635+ if not first_var:636+ first_var.append(name)637+638+ 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)645+646+ 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)656+657+ 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)668+669+ 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')679+680+681 # get rid of node now, it shouldn't be used normally682 del nodemodified:ext/django2jinja/example.py+2 added-1 removed
1 from django.conf import settings2+settings.configure(TEMPLATE_DIRS=['templates'], TEMPLATE_DEBUG=True)3+4 from django2jinja import convert_templates, Writer5 4-settings.configure(TEMPLATE_DIRS=['templates'], TEMPLATE_DEBUG=True)6 writer = Writer(use_jinja_autoescape=True)7 convert_templates('converted', writer=writer)