Reputation: 143
I'm creating django multilingual website.
Settings.py
USE_I18N = True
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.i18n',
],
},
},
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
]
from django.utils.translation import ugettext_lazy as _
LANGUAGES = (
('en', _('English')),
('de', _('German')),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
template:
<h5>{% trans "Hello, this is testing page" %}</h5>
urls:
from django.conf.urls.i18n import i18n_patterns
from django.conf.urls import include, url
urlpatterns = [
url(r'^rev/', rev, name="rev"),
url(r'^userprofile/', userprofile, name="profile"),
]
views:
@login_required
def rev(request):
return render_to_response('rev.html', {'client': request.user.client, 'user': request.user})
@login_required
@require_http_methods(["GET"])
def userprofile(request):
return render(request, 'userprofile.html', {
'user': request.user,
'form': UserProfileForm(instance=request.user)
})
So, using python makemessage -l de command I got next error:
CommandError: errors happened while running msguniq
msguniq: \u041d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0437 \xabASCII\xbb \u0432 \xabUTF-8\xbb. msguniq \u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 iconv(). \u042d\u0442\u0430 \u0432\u0435\u0440\u0441\u0438\u044f \u0431\u044b\u043b\u0430 \u0441\u043e\u0431\u0440\u0430\u043d\u0430 \u0431\u0435\u0437 iconv().
I think, that I set urls incorrect. All answers about it are at least 3-4 years old, on newest django version , setting urlpatterns = i18n_patterns[....] causes error:
TypeError: 'function' object is not subscriptable
GNU gettext installed.
Why Am I really getting this error, and I know, that it is forbidden here to advice anything:) Does anyone have working example with connecting JS (language buttons on website or something) with django translations.
Upvotes: 0
Views: 923
Reputation: 143
Solved. This problem appears because of 2 GNU Gettext installed (different version) 1 was already installed another I've installed by myself. Setting path in PyCharm(yes, my django project is in PyCharm) to newest gnu gettext in terminal solved my problem.
Upvotes: 0
Reputation: 7717
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
]
LocaleMiddleware
must add between SessionMiddleware
and CommonMiddleware
.
Upvotes: 1