开发者

i18n in Django 1.3 doesn't work for me

开发者 https://www.devze.com 2023-03-08 22:55 出处:网络
I use Django 1.3. I have a clean project with: settings.py import os, sys # absolute path to the project

I use Django 1.3. I have a clean project with:

settings.py

import os, sys

# absolute path to the project
PROJECT_ROOT = os.path.dirname(os.path.realpath(__f开发者_运维知识库ile__))

    # add to PYTHONPATH
    sys.path.append(os.path.join(PROJECT_ROOT, "apps"))

LANGUAGE_CODE = 'ru-ru'  


    USE_I18N = True    
    USE_L10N = True

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

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

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'i18n_app',
)

urls.py

urlpatterns = patterns('',
    url(r'^$', 'i18n_app.views.test'),
)

i18n_app/views.py

from django.shortcuts import render_to_response
from django.template import RequestContext

def test(request):

    return render_to_response('i18n_app/test.html', context_instance=RequestContext(request))

templates/i18n_app/test.html

{% load i18n %}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
</head>
<body>
{% trans "String for trans" %}
<br>
<br>
<br>
{% blocktrans %}And one more{% endblocktrans %}
</body>
</html>

From project's root i run this command:

D:\Django\projects\testi18n>django-admin.py makemessages -l ru
processing language ru

Then I fill out file django.po which was created after this command and do:

D:\Django\projects\testi18n>django-admin.py compilemessages --traceback
processing file django.po in D:\Django\projects\testi18n\conf\locale\ru\LC_MESSAGES

But all I see it's english strings. I have found too much questions about this, but no one of them hasn't helped to me.

What I'm doing wrong?


Add to your settings:

ugettext = lambda s: s
LANGUAGES = (
    ('ru', ugettext('Russian')),
    ('en', ugettext('English')),
)

Create an empty folder named locale in your projects directory

Then from your projects directory run:

django-admin.py makemessages -l ru

This command will create a .po file inside your locale folder.Open that file and fill the empty msgstr "" fields with the desired translation of the msgid fields that sit above. This is where you define the translation of the strings you marked for translation in the .html file.

After writing the translation of your strings run:

django-admin.py compilemessages


django's test server needs a restart to take in account locale files changes/apparition. Otherwise translation won't work.


Looks like the LANGUAGES setting is missing in your settings.py. Try putting in something like that:

ugettext = lambda s: s
LANGUAGES = (
    ('ru', ugettext('Russian')),
    ('en', ugettext('English')),
)

In addition make sure that 'django.core.context_processors.i18n' is in your MIDDLEWARE_CLASSES setting, otherwise you won't be able to change the language. See the docs for details.

What i don't understand - why do you use django-admin.py in your project directory? Basically you should use manage.py in your project directory.

./manage.py makemessages -l ru


Don't forget to set LOCALE_PATHS to your locale folders

0

精彩评论

暂无评论...
验证码 换一张
取 消