Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8454867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:08:14+00:00 2026-06-10T12:08:14+00:00

I started to play with new i18n_patterns in Django 1.4. Basically, i want to

  • 0

I started to play with new i18n_patterns in Django 1.4. Basically, i want to have language links for each of my supported languages on all of my templates headers. I have implemented my header as a separate template that is being included in other templates.

Is there a way to keep my header generic and solve this without passing the current view name or current url in template context? I guess it comes to a question how do i retrieve the current view or url from inside the template in a generic way.

BTW, i discovered that my previous approach with set_lang view to change the active language using the referrer will be broken with url_patterns as after changing the language it will change it back when redirected to the referred view.

Any help figuring out the common approach to set language links in templates to be used with url_patterns in a generic way would be appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:08:15+00:00Added an answer on June 10, 2026 at 12:08 pm

    My apologies for the long delay. Thank you all for your answers.

    First of all to comment on the two solution options by Chris:
    Neither custom set_language nor javascript are good for the purpose as the whole beauty of url patterns is being SEO friendly.

    Furthermore, simply replacing the prefix language in URL cannot be treated as full solution for urlpattern based urls as the whole URL might be translatable too. Ex: /en/profile/ for english and /fr/profil/ for french.
    To solve such a problem one needs to capture the viewfunc and the arguments in order to reverse it for different language.

    Fortunately for me, my project does not use translatable URLs for now and I took the following approach.

    1. Add django.core.context_processors.request to TEMPLATE_CONTEXT_PROCESSORS so that request to be available in template rendering context.

    2. Use RequestContext in views when rendering your views. This is always the case for me independent of the topic.

    3. Write a quite simple templatetag that requires the context and takes an argument a language code to return the current URL in the given language. It basically makes sure the current URL has valid language-code prefix and simply returns another string that is the same current URL with replaced language-code.

    ex:

    from django import template
    register = template.Library()
    
    @register.simple_tag(takes_context=True)
    def get_current_url_for_lang(context, lang_code):
        request=context.get('request',False)
        if not request:
            return None
    
        request=context['request']
        curr_url=request.path
        if len(curr_url) < 4 or curr_url[0] != '/' or curr_url[3] != '/':
            return curr_url
    
        if context.get('LANGUAGES',False):
            codes = []
            for code,name in context['LANGUAGES']:
                codes.append(code)
    
            curr_langcode = curr_url[1:3]
            if lang_code not in codes or curr_langcode not in codes:
                return curr_url
    
        changed_url = '/'+lang_code+curr_url[3:]
        return changed_url
    

    Furthermore, if one does not like to inject full request into context it will be quite straightforward to write your own context_processor that simply pushes the request.path as current_url_path for instance and use that instead in your templatetag.

    Your comments are welcomed as always!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started to play with xVal as my validation framework for an ASP.Net
I have just started to play with Play framework (2.0) and I'm having some
After the release of MVC 2, I have started to check and play with
Started to play around with Jenkins. Created new job and entered SVN repository URL.
I just started to play with android dev and java+eclipse is pretty new to
Just started to play around with node.js, have some past experience with JavaScript but
I have started to play around with knockoutjs and do some simple binding/dependant binding.
Newbie disclaimer: I am new to Python and just started using IDLE to play
I have recently started using Ruby, so quite new to this. My current objective
Recently I have started to play around with Namespaces in PHP, I am not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.