1 from django.conf import settings
2 from django.core.urlresolvers import resolve, reverse, Resolver404
3 from django import template
4 from django.utils import translation
5 from ..utils.views import get_current_object
7 register = template.Library()
10 @register.inclusion_tag('fnpdjango/lang_switcher.html', takes_context=True)
11 def lang_switcher(context):
12 """Context-aware language switcher.
14 Use ..utils.views.set_current_object to provide the context.
16 request = context['request']
17 obj = get_current_object(request)
18 languages = settings.LANGUAGES
19 if hasattr(obj, 'get_available_languages'):
20 available_languages = set(obj.get_available_languages())
21 languages = [lang for lang in languages if lang[0] in available_languages]
24 'languages': languages,
29 def get_here_url(request, lang):
30 obj = get_current_object(request, for_url=True)
31 if hasattr(obj, 'get_absolute_url'):
32 with translation.override(lang):
33 url = obj.get_absolute_url()
36 match = resolve(request.get_full_path())
43 lang = translation.get_language()
44 with translation.override(lang):
45 url = reverse(view, args=match.args, kwargs=match.kwargs)