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?
To use internationalization:
settings.py.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:
/en/foo/bar/django_language key in the session.django_language cookie.Accept-Language header.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:
activate('fa')You can force a language in your template, with the language template tag:
{% load i18n %}
{% language 'fa' %}
{% trans alert %}
set_language redirect view, to properly set the language and internationalize your URLs.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With