0
votes

I create multilingual flatpages. i want to translate the content according to the language which i select. i refer this code Django i18n setlang view gives Error 404 When i select the language the post method is call i18n/setlang & again comes into same page, but the content is not change. Can anybody help me to solve this problem?

Thanks

settings

gettext = lambda s: s

LANGUAGES = (
    ('en', gettext('English'),),
    ('no', gettext('Norwegian'),),
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.i18n',
    'multilingual.context_processors.multilingual',
    'django.core.context_processors.media',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

template

{% load i18n %}
 <form action="/i18n/setlang/" method="post">
  {% csrf_token %}
   <input name="next" type="hidden" value="{{ redirect_to }}" />
   <select name="language">
      {% get_language_info_list for LANGUAGES as languages %}
      {% for language in languages %}
       <option value="{{ language.code }}">{{ language.name }} ({{ language.code }})</option>
      {% endfor %}
 </select>
 <input type="submit" value="Go" />
</form>


 **view.py**
 def index(request):
    return render_to_response('flatpages/index.html', locals(),   context_instance=RequestContext(request))

**index.html**
 {% block content %}
 {% load i18n %}
     <h1>{{ flatpage.title }}</h1>
     Language Code:{{ LANGUAGE_CODE}}<br>
     {% trans "Hello" %}<br>
     {% trans "Home" %}<br>
     {{ flatpage.content }}
 {% endblock content %}

In above code i use the
url (r'^i18n/', include('django.conf.urls.i18n')),
This means when form is submitted it call the method set_language from django's i18n.py file.

I check that method, in that method "check_for_language(lang_code) returns false"
that why the session variable not set.

Can anybody please tell me why this not set?
There will any changes in setting to set path of locale.
I created locale folder in my project folder.

1
Have you followed the django documentation and setup the appropriate Locale middleware? In other words, post your relevant settings, form and view pleaseHedde van der Heide
Thanks for the reply. I add the code which i use for language translation. Can you please refer & tell me what should i done wrong?Meenakshi
In view i just render the default flatpage template file.Meenakshi
I didn't understand how should i accept the post i18n/setlang response in view?Meenakshi
If you know any data regarding this can you please tell me?Meenakshi

1 Answers

0
votes

404 means you're calling for a page that doesn't exist. By your comments I understand you don't have an endpoint at /i18n/lang/.

Example:

from django.views.i18n import set_language

url('^i18n/setlang/$', set_language)

However if you want some custom behaviour so have a look at set_language and create your own implementation