Mein i18n locale Test nicht wie erwartet, ist das Verzeichnis wie folgt aus:i18n nicht, wenn Sprache ändern Präfix
from django.contrib import admin
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
urlpatterns = patterns('',
url(r'^i18n/', include('django.conf.urls.i18n')),
)
urlpatterns += i18n_patterns(
url(r'^admin/', include(admin.site.urls)),
url(r'^localedemo/$', include('localedemo.urls')),
# url(r'^$', 'localedemo.views.home', name='home'),
# url(r'^i18n/', include('django.conf.urls.i18n')),
)
localedemo/Urls. py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name="index"),
# url(r'^set-language/', views.set_language, name='set-language'),
]
localede mo/views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.utils.translation import ugettext_lazy as _
from django.utils import translation
def index(request):
translation.activate('zh_CN')
language = translation.get_language_from_request(request)
print("language:" + language)
#Translators: This message appears on the home page only
output = _("Welcome to my site.")
str = {'mystr': output}
return render(request, 'localedemo/index.html',str)
wenn Server laufen, ist das Ergebnis:
wenn ich die Sprache Präfix ändern, ist es nicht funktioniert:
die django. po in jeder Sprache Unterauftrag ist
msgid "Welcome to my site."
msgstr "this is english website"
msgid "Welcome to my site."
msgstr "中文网站"
msgid "Welcome to my site."
msgstr "中文繁体网站"
und die localedemo/templates/index.html
{% load i18n %}
<html !DOCTYPE=html>
<head><title>hi</title></head>
<body>
<div>
\t <h1>Hello, world!</h1>.
\t <h1>{% trans mystr %}</h1>
\t <h1>{{ mystr }}</h1>
</div>
<div>
<form action="/i18n/setlang/" method="post">
{% csrf_token %}
<input name="next" type="hidden" value="/" />
<select name="language">
{% for lang in LANGUAGES %}
<option value="{{ lang.0 }}" {% if lang.0 == SELECTEDLANG %}selected{% endif %}>{% if lang.1 == 'Spanish' %}Español{% else %}{{ lang.1 }}{% endif %}</option>
{% endfor %}
</select>
<input type="submit" value="Go" />
</form>
</div>
<div>
{% get_current_language as LANGUAGE_CODE %}
<!-- Current language: {{ LANGUAGE_CODE }} -->
<p>{% trans "Welcome to my site." %}</p>
{% language 'en' %}
{% get_current_language as LANGUAGE_CODE %}
<!-- Current language: {{ LANGUAGE_CODE }} -->
<p>{% trans "Welcome to my site." %}</p>
{% endlanguage %}
</div>
</body>
</html>
und wenn ich die Sprache im Listenfeld ausgewählt:
Habe ich etwas verpasst oder irgendwo habe ich falsch? kann mir jemand helfen? Vielen Dank im Voraus