mortymacs
mortymacs

Reputation: 3726

django i18n doesn't work

I have problem in django internationalization (i18n). the translation doesn't work. my django version is : 1.5.4 This is my code:

settings.py

ugettext = lambda s:s
LANGUAGES = [
    ('en','English'),
    ('fa','Farsi'),
]

LANGUAGE_CODE = 'en_us'
USE_I18N = True
USE_L10N = True

TEMPLATE_DIRS = (
    os.path.join(PROJECT_PATH,'templates'),
)

urls.py

from django.conf.urls import patterns, include, url
from main import *
urlpatterns = patterns('',
    url(r'$^',index),
)

main.py

from django.shortcuts import render
from django.utils.translation import ugettext_lazy as _
def index(request):
    alert = _('this is my page')
    return render(request,'index.html',{'alert':alert})

index.html

 {% load i18n %}
 {{alert}}

and then:

$mkdir conf/locale -p
$django-admin.py makemessages -l fa
processing language fa
$django-admin.py compilemessages
processing file django.po in /home/mori/Desktop/salam/salam/locale/fa/LC_MESSAGES

django.po

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-10-15 13:12+0330\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=1; plural=0;\n"

#: main.py:4
msgid "this is my page"
msgstr "in safeye mane"

I have 'django.middleware.locale.LocaleMiddleware' in middleware class and 'django.core.context_processors.i18n' in TEMPLATE_COTEXT_PROCESSORS.

output

this is my page

How can I solve it?

Upvotes: 3

Views: 4396

Answers (2)

mortymacs
mortymacs

Reputation: 3726

Just one thing I've forgot to consider in my settings.py: LOCALE_PATHS

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174614

To use internationalization:

  1. Define the languages your site supports.
  2. Mark strings for translation.
  3. Create the language files.
  4. Set up the middleware in settings.py.
  5. Set the default language of the request.

You need to set the language for the request, otherwise django will default to the language request coming from the browser which in your case is English.

You also need to fix your template, it should be:

{% load i18n %}
{% trans alert %}

In your view the _ function only marks your string for translation; it doesn't actually translate it. Marking the string for translation makes django export it in the language files, and makes django look it up when a different language is requested by the user.

There are a few ways that django detects the preferred language (it will stop at the first place it finds a language preference). These ways in the order they are checked are:

  1. Language prefix in the URL, for example /en/foo/bar/
  2. A django_language key in the session.
  3. A django_language cookie.
  4. Accept-Language header.
  5. LANGUAGE_CODE setting.

In your case, it catching #4, because your browser is sending a list of languages it will accept, and the first one is English.

There are a few ways you can resolve this:

  1. You can activate the language in your view: activate('fa')
  2. You can force a language in your template, with the language template tag:

    {% load i18n %}
    {% language 'fa' %}
    {% trans alert %}
    
  3. You can use the url internationalization feature, and pair that with the set_language redirect view, to properly set the language and internationalize your URLs.

Upvotes: 2

Related Questions