django翻译模板{%trans“ something” %} [英] django translation template {% trans "something" %}

查看:386
本文介绍了django翻译模板{%trans“ something” %}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我一直在疯狂寻找这个我认为很简单的问题。

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

我使用Django 1.4

I use Django 1.4

问题在于django不会在我的模板中翻译一个简单的{%trans work%}。

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

这就是我所做的:

Settings.py:

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'
)

这是我的地图结构:

/
myproject/
apps/
locale/
template/

所以我跑了


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

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

并成功在locale文件夹nl / LC_MESSAGES / django.po中创建了它,然后编辑了翻译在django.po中找到

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"

然后我运行编译命令


django-admin.py comp ilemessages

django-admin.py compilemessages

,它还会在正确的文件夹中成功创建django.mo文件

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

我也在myproject / urls.py

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'),
)

我在base.html文件中添加了此语言,以便能够更改语言

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>

因此,当我访问网站时,URL中显示127.0.0.1:8000/en/这是正确的,因为英语是默认语言,当我将其更改为NL时,它会重定向到127.0.0.1:8000/nl/,但我翻译的文本没有更改。我还确定该语言是NL,因为当我显示{{LANGUAGE_CODE}}时它说NL。

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?

感谢m00p

已解决的问题

在添加东西的过程中,我忘记了它的正常工作,我重新启动了部署服务器,但是我没有清除浏览器的缓存,它仍在使用旧页面,因此,当我在Chrome中清除浏览器数据并重新访问该页面并更改了语言时,它可以正确翻译该页面。还是谢谢您的建议!

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!

推荐答案

在Django 1.4中,不再支持项目根目录下的locale目录。您应该将其添加到 LOCALE_PATHS 设置中,该设置默认为空。 Django设置

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.

这篇关于django翻译模板{%trans“ something” %}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆