Initial implementation of l10n block type with jingo-like render shortcut.

This commit is contained in:
Fred Wenzel 2011-04-29 13:51:50 -07:00
Родитель 730fe551c2
Коммит 306e0e5164
13 изменённых файлов: 129 добавлений и 2 удалений

Просмотреть файл

Просмотреть файл

@ -0,0 +1,15 @@
{% extends "base.html" %}
{% block content %}
<h1>Hello world!</h1>
{% l10n foo %}
<p>Localizable, unversioned.</p>
{% endl10n %}
{% l10n bar, 3 %}
<p>Localizable, versioned.</p>
{% endl10n %}
<p>unlocalizable.</p>
{% endblock %}

Просмотреть файл

@ -0,0 +1,8 @@
from django.conf.urls.defaults import patterns
import jingo
urlpatterns = patterns('',
(r'^$', 'l10n_example.views.example'),
)

Просмотреть файл

@ -0,0 +1,5 @@
import l10n_utils
def example(request):
return l10n_utils.render(request, 'l10n_example/example.html')

0
apps/models.py Normal file
Просмотреть файл

0
lib/__init__.py Normal file
Просмотреть файл

Просмотреть файл

@ -0,0 +1,29 @@
from django.conf import settings
import jingo
from jinja2.exceptions import TemplateNotFound
def render(request, template, context=None, **kwargs):
"""
Same as jingo's render() shortcut, but with l10n template support.
If used like this::
return l10n_utils.render(request, 'myapp/mytemplate.html')
... this helper will render the following template::
l10n/LANG/myapp/mytemplate.html
if present, otherwise, it'll render the specified (en-US) template.
"""
# Look for localized template if not default lang.
if request.locale != settings.LANGUAGE_CODE:
localized_tmpl = 'l10n/%s/%s' % (request.locale, template)
try:
return jingo.render(request, localized_tmpl, context, **kwargs)
except TemplateNotFound:
# If not found, just go on and try rendering the parent template.
pass
return jingo.render(request, template, context, **kwargs)

0
lib/l10n_utils/models.py Normal file
Просмотреть файл

Просмотреть файл

@ -0,0 +1,47 @@
"""
A Jinja extension adding support for localizable template blocks.
"""
from jinja2.ext import Environment, Extension, nodes
class L10nBlockExtension(Extension):
"""
Add support for an L10n block that works like a regular "block" for now.
"""
tags = set(['l10n'])
def parse(self, parser):
# Jump over first token ("l10n"), grab line number.
lineno = parser.stream.next().lineno
# Block name is mandatory.
name = parser.stream.expect('name').value
# Comma optional.
parser.stream.skip_if('comma')
# Add version if provided.
if parser.stream.current.type == 'integer':
version = int(parser.parse_expression().value)
else:
version = 0 # Default version for unversioned block.
# Parse content, drop closing tag.
body = parser.parse_statements(['name:endl10n'], drop_needle=True)
# Build regular block node with special node name and remember version.
node = nodes.Block()
node.set_lineno(lineno)
node.name = '__l10n__{0}'.format(name)
node.version = version # For debugging only, for now.
node.body = body
# I *think*, `true` would mean that variable assignments inside this
# block do not persist beyond this block (like a `with` block).
node.scoped = False
return node
# Makes for a prettier import in settings.py
l10n_blocks = L10nBlockExtension

Просмотреть файл

@ -50,7 +50,7 @@ TEXT_DOMAIN = 'messages'
LANGUAGE_CODE = 'en-US'
# Accepted locales
KNOWN_LANGUAGES = ('en-US',)
KNOWN_LANGUAGES = ('de', 'en-US', 'es', 'fr',)
# List of RTL locales known to this project. Subset of LANGUAGES.
RTL_LANGUAGES = () # ('ar', 'fa', 'fa-IR', 'he')
@ -120,7 +120,8 @@ def JINJA_CONFIG():
from django.conf import settings
# from caching.base import cache
config = {'extensions': ['tower.template.i18n', 'jinja2.ext.do',
'jinja2.ext.with_', 'jinja2.ext.loopcontrols'],
'jinja2.ext.with_', 'jinja2.ext.loopcontrols',
'l10n_utils.template.l10n_blocks'],
'finalize': lambda x: x if x is not None else ''}
# if 'memcached' in cache.scheme and not settings.DEBUG:
# We're passing the _cache object directly to jinja because
@ -172,6 +173,7 @@ INSTALLED_APPS = (
# Local apps
'commons', # Content common to most playdoh-based apps.
'careers',
'l10n_example', # DELETEME
# We need this so the jsi18n view will pick up our locale directory.
ROOT_PACKAGE,
@ -198,6 +200,7 @@ INSTALLED_APPS = (
# L10n
'product_details',
'l10n_utils',
)
# Tells the extract script what files to look for L10n in and what function

Просмотреть файл

@ -0,0 +1,11 @@
{% extends "l10n_example/example.html" %}
{#
{% l10n foo %}
<p>Foo, overwritten.</p>
{% endl10n %}
#}
{% l10n bar, 2 %}
<p>Dies ist deutsch: Bar, overwritten but v2.</p>
{% endl10n %}

Просмотреть файл

@ -0,0 +1,5 @@
{% extends "l10n_example/example.html" %}
{% l10n foo %}
<p>C'est français: Foo, overwritten.</p>
{% endl10n %}

Просмотреть файл

@ -1,6 +1,7 @@
from django.conf import settings
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
@ -8,6 +9,9 @@ from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'', include('careers.urls')),
# L10n example.
(r'^l10n_example/', include('l10n_example.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),