简体   繁体   中英

django translation template {% trans “something” %}

Ok I have been searching like crazy for this I think simple problem.

I use Django 1.4

The problem is that django won't translate a simple {% trans "work" %} in my template.

This is what I have done:

Settings.py:

LANGUAGE_CODE = 'en-us'
USE_I18N = True
MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.request",
)

LOCALE_PATHS = (
    '/home/m00p/PycharmProjects/astrid/locale'
)

this is my map structure:

/
myproject/
apps/
locale/
template/

So I runned

django-admin.py makemessages -l nl -i settings.py

and it did succesfully created in the locale folder nl/LC_MESSAGES/django.po, I then edit the translation It found in the django.po

#: templates/base.html:22
msgid "work"
msgstr "ddddddddddddd"

I then run the compile command

django-admin.py compilemessages

and it also succesfully created a django.mo file in the correct folder

I added this also in the myproject/urls.py

urlpatterns = patterns('',
    url(r'^i18n/', include('django.conf.urls.i18n')),
)

urlpatterns += i18n_patterns('',
    url(r'^$', 'front.views.home', name='home'),
)

I added this in the base.html file to be able to change the language

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

So when I go to the website I get 127.0.0.1:8000/en/ in the url, this is correct because englisch is the default language, when I then change it with the form to NL, It redirects to 127.0.0.1:8000/nl/ but the text I translated didn't change. I'm also sure that the language is NL because when I display {{ LANGUAGE_CODE }} it says NL.

Anybody know why it doesn't change?

Thanks m00p

PROBLEM SOLVED

During the process when I was adding things I forgot for it to work, I restarted my deployment server, but I didn't cleared the cache of my browser it was still using the old pages, so when I cleared my browser data in Chrome and revisited the page and changed the language it translated it correctly. Thanks for the suggestions anyway!

I just spent few hours trying to fix this issue in Django 1.5 while working on my new project Sportolio and it turned out I was missing a comma at the end of LOCALE_PATH

LOCALE_PATHS = (
    '/path/to/my/project/locale/',
)

This is very crucial, as Django expects LOCALE_PATHS to be a TUPLE not a String.

I hope it save someone's life :)

In Django 1.4 the locale directory at project root is not longer supported. You should add it to LOCALE_PATHS setting, which is empty by default. Django Settings .

However the management commands involved in locale generation, seems to work, so I don't know if you already did it.

I had the same situation. Part of phrases were translation but most of them just showed the key. We have two main languages en and fa. Both had the same problem. I reviewed all the same problems and every single document online about the django translations. I can say I tried almost everything. The only difference was we were testing on the production server. And I didn't reload the server! So this one-line command took me out of my misery:

sudo /etc/init.d/uwsgi reload

what I was doing wrong was I restarted wsgi instead of uwsgi!

If you like to do what I did, here it is: I do the steps in this order on server:

django-admin makemessages -l en
django-admin makemessages -l fa

add Persian phrases and some english text to test.

django-admin compilemessages
python manage.py clear_cache
python manage.py runserver

Server runs with no errors. But non of the changes in en or fa take place. It looks that django is loading from old cache. So, I deleted the whole cache, and even deleted all the files from server and installed the whole project again. Yet the same issue.

I made messages from my django on my laptop and sent it to server. Also, I tried different text editors and translation tools such as poedit to generate po and mo files. Yet the problem keeps showing.

I used and replaced trans tags to see if it works in any of them:

{% trans ' ' %}
{% trans " " %}
{% blocktrans %} {% endblocktrans %}
{% trans "xyz" az XYZ %} {{ XYZ }}
{{ _() }}

none of them worked!

But reloading the server put everything in order. I hope it might be helpful for some one.

The position you key the command matter.

In my case, this is my project structure:

myproject/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py
    apps/
        migrations/
        __init__.py
        admin.py
        models.py
        tests.py
        views.py

If I call makemessages at mysite folder, like this:

D:\...\myproject\mysite> python ../manage.py makemessages -all

it will not get the string in HTML {% trans "str" %} .
But if I call makemessages at myproject folder, like this:

D:\...\myproject> python manage.py makemessages -all

it works! It finds all {% trans "str" %} in HTML.
So be careful about the location you are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM