mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +00:00
i18n: added get_available_languages tag and added a default view for switching languages (both from the django-dev posting of jacob)
git-svn-id: http://code.djangoproject.com/svn/django/branches/i18n@1048 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
5c8399469c
commit
13fcef45f7
5
django/conf/urls/i18n.py
Normal file
5
django/conf/urls/i18n.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
from django.conf.urls.defaults import *
|
||||||
|
|
||||||
|
urlpatterns = patterns('',
|
||||||
|
(r'^setlang/$', 'django.views.i18n.set_language'),
|
||||||
|
)
|
@ -290,6 +290,16 @@ class WidthRatioNode(Node):
|
|||||||
return ''
|
return ''
|
||||||
return str(int(round(ratio)))
|
return str(int(round(ratio)))
|
||||||
|
|
||||||
|
class GetAvailableLanguagesNode(Node):
|
||||||
|
|
||||||
|
def __init__(self, variable):
|
||||||
|
self.variable = variable
|
||||||
|
|
||||||
|
def render(self, context):
|
||||||
|
from django.conf.settings import LANGUAGES
|
||||||
|
context[self.variable] = LANGUAGES
|
||||||
|
return ''
|
||||||
|
|
||||||
class I18NNode(Node):
|
class I18NNode(Node):
|
||||||
|
|
||||||
def __init__(self, cmd):
|
def __init__(self, cmd):
|
||||||
@ -803,6 +813,28 @@ def do_widthratio(parser, token):
|
|||||||
raise TemplateSyntaxError("widthratio final argument must be an integer")
|
raise TemplateSyntaxError("widthratio final argument must be an integer")
|
||||||
return WidthRatioNode(this_value_var, max_value_var, max_width)
|
return WidthRatioNode(this_value_var, max_value_var, max_width)
|
||||||
|
|
||||||
|
def do_get_available_languages(parser, token):
|
||||||
|
"""
|
||||||
|
This will store a list of available languages
|
||||||
|
in the context.
|
||||||
|
|
||||||
|
Usage is as follows::
|
||||||
|
|
||||||
|
{% get_available_languages as languages %}
|
||||||
|
{% for language in languages %}
|
||||||
|
...
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
This will just pull the LANGUAGES setting from
|
||||||
|
your setting file (or the default settings) and
|
||||||
|
put it into the named variable.
|
||||||
|
"""
|
||||||
|
|
||||||
|
args = token.contents.split()
|
||||||
|
if len(args) != 3 or args[1] != 'as':
|
||||||
|
raise template.TemplateSyntaxError("'get_available_languages' requires 'as variable' (got %r)" % args)
|
||||||
|
return GetAvailableLanguagesNode(args[2])
|
||||||
|
|
||||||
def do_i18n(parser, token):
|
def do_i18n(parser, token):
|
||||||
"""
|
"""
|
||||||
translate a given string with the current
|
translate a given string with the current
|
||||||
@ -837,3 +869,4 @@ register_tag('now', do_now)
|
|||||||
register_tag('templatetag', do_templatetag)
|
register_tag('templatetag', do_templatetag)
|
||||||
register_tag('widthratio', do_widthratio)
|
register_tag('widthratio', do_widthratio)
|
||||||
register_tag('i18n', do_i18n)
|
register_tag('i18n', do_i18n)
|
||||||
|
register_tag('get_available_languages', do_get_available_languages)
|
||||||
|
@ -274,6 +274,19 @@ def ngettext(singular, plural, number):
|
|||||||
gettext_lazy = lazy(gettext, str)
|
gettext_lazy = lazy(gettext, str)
|
||||||
ngettext_lazy = lazy(ngettext, str)
|
ngettext_lazy = lazy(ngettext, str)
|
||||||
|
|
||||||
|
def check_for_language(lang_code):
|
||||||
|
"""
|
||||||
|
This function checks wether there is a global language
|
||||||
|
file for the given language code. This is used to decide
|
||||||
|
wether a user-provided language is available.
|
||||||
|
"""
|
||||||
|
from django.conf import settings
|
||||||
|
globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
|
||||||
|
if gettext_module.find('django', globalpath, [to_locale(lang_code)]) is not None:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
def get_language_from_request(request):
|
def get_language_from_request(request):
|
||||||
"""
|
"""
|
||||||
analyze the request to find what language the user
|
analyze the request to find what language the user
|
||||||
@ -284,29 +297,14 @@ def get_language_from_request(request):
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
|
globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
|
||||||
|
|
||||||
if request.GET or request.POST:
|
|
||||||
lang_code = request.GET.get('django_language', None) or request.POST.get('django_language', None)
|
|
||||||
if lang_code is not None:
|
|
||||||
lang = gettext_module.find('django', globalpath, [to_locale(lang_code)])
|
|
||||||
if lang is not None:
|
|
||||||
if hasattr(request, 'session'):
|
|
||||||
request.session['django_language'] = lang_code
|
|
||||||
else:
|
|
||||||
request.set_cookie('django_language', lang_code)
|
|
||||||
return lang_code
|
|
||||||
|
|
||||||
if hasattr(request, 'session'):
|
if hasattr(request, 'session'):
|
||||||
lang_code = request.session.get('django_language', None)
|
lang_code = request.session.get('django_language', None)
|
||||||
if lang_code is not None:
|
if lang_code is not None and check_for_language(lang_code):
|
||||||
lang = gettext_module.find('django', globalpath, [to_locale(lang_code)])
|
return lang_code
|
||||||
if lang is not None:
|
|
||||||
return lang_code
|
|
||||||
|
|
||||||
lang_code = request.COOKIES.get('django_language', None)
|
lang_code = request.COOKIES.get('django_language', None)
|
||||||
if lang_code is not None:
|
if lang_code is not None and check_for_language(lang_code):
|
||||||
lang = gettext_module.find('django', globalpath, [to_locale(lang_code)])
|
return lang_code
|
||||||
if lang is not None:
|
|
||||||
return lang_code
|
|
||||||
|
|
||||||
accept = request.META.get('HTTP_ACCEPT_LANGUAGE', None)
|
accept = request.META.get('HTTP_ACCEPT_LANGUAGE', None)
|
||||||
if accept is not None:
|
if accept is not None:
|
||||||
|
22
django/views/i18n.py
Normal file
22
django/views/i18n.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
from django.utils import httpwrappers
|
||||||
|
from django.utils.translation import check_for_language
|
||||||
|
|
||||||
|
def set_language(request):
|
||||||
|
"""
|
||||||
|
Redirect to a given url while setting the chosen language in the
|
||||||
|
session or cookie. The url and the language code need to be
|
||||||
|
specified in the GET paramters.
|
||||||
|
"""
|
||||||
|
lang_code = request.GET['language']
|
||||||
|
next = request.GET.get('next', None)
|
||||||
|
if not next:
|
||||||
|
next = request.META.get('HTTP_REFERER', None)
|
||||||
|
if not next:
|
||||||
|
next = '/'
|
||||||
|
response = httpwrappers.HttpResponseRedirect(next)
|
||||||
|
if check_for_language(lang_code):
|
||||||
|
if hasattr(request, 'session'):
|
||||||
|
request.session['django_language'] = lang_code
|
||||||
|
else:
|
||||||
|
response.set_cookie('django_language', lang_code)
|
||||||
|
return response
|
Loading…
x
Reference in New Issue
Block a user