Django internationalization not working - django

My django is not executing translations that i define in my *.po files.
I have my setup like this:
MainFolder:
locale
en
LC_MESSAGES
django.po
django.mo
de
LC_MESSAGES
django.po
django.mo
app1
settings.py
app2
....
in my settings.py i have everything enabled i think:
USE_I18N = True
USE_L10N = True
LANGUAGE_CODE = 'en'
LANGUAGES = (
('en', _('English')),
('de', _('German')),
)
TEMPLATE_CONTEXT_PROCESSORS = (
....
'django.core.context_processors.static',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
)
let's say my django.po file contains an example like this:
msgid "Home"
msgstr "Home22222"
and i use this in my template file:
{% trans "Home" %}
The home is not being replace by Home22222
It stays the same, even when i remove the LocaleMiddleware.
i did ran
makemessages --locale=en
and afterwards:
compilemessages
I am using Python 2.7 and django 1.6.4

Related

Django uses untranslated strings instead of translations

I have a small Django application, which I tried to localize.
In the urls.py I have
urlpatterns += i18n_patterns(
path('add_request/',AddRequest.as_view(),name='add_request'),
path('add_offer/', AddOffer.as_view(), name='add_offer'),
)
In the settings.py I have
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
and
LANGUAGE_CODE = 'de'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
LOCAL_PATHS = (
os.path.join(BASE_DIR, 'locale/'),
)
USE_TZ = True
LANGUAGES= [
('en', 'English'),
('de', 'German')
]
I have marked strings for translation in my models.py (verbose_name),
the forms.py (label) and in a template.
I extracted po files via
django-admin makemessages -l en
django-admin makemessages -l de
Translated them and ran
django-admin compilemessages
Now I have the prefixes de/ and en/ for my urls. The Locale seems to be set correctly, as date input fields change to the right date format, but the translations are not used.
I'm an idiot. It is of course supposed to be LOCALE_PATHS, instead of LOCAL_PATHS.

Django unable to translate language

I want to translate my website to CH, but no response after I followed the online instruction. Did I miss something?
I have changed the language code in settings.py.
settings.py:
import os
import django_heroku
from django.utils.translation import gettext_lazy as _
LANGUAGES = (
('en', ('English')),
('zh-Hant', _('Traditional Chinese')),
)
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
...
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMP_DIR],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.i18n',
'django.template.context_processors.debug',
....]}
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-Hant'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale')
]
Added the tag for translation in html
base.html:
{% load i18n %}
<!DOCTYPE html>
<html>
....
home.html:
{% extends "base.html" %}
{% load i18n %}
{% block content %}
{% trans "Let's translate this" %}
....
Updated msgstr in ....\locale\zh-Hant\LC_MESSAGESdjango.po:
msgid "Let's translate this"
msgstr "來翻譯這個"
Updated msgstr in ....\locale\zh-Hant\LC_MESSAGESdjango.po:
For traditional Chinese:
zh-hant in your config, and your directory should be named zh_Hant.
See the locale directory: https://github.com/django/django/tree/master/django/contrib/auth/locale
locale name
A locale name, either a language specification of the form ll or a combined language and country specification of the form ll_CC. Examples: it, de_AT, es, pt_BR, sr_Latn. The language part is always in lowercase. The country part is in titlecase if it has more than 2 characters, otherwise it’s in uppercase. The separator is an underscore.
and languages keys with lowercase:
LANGUAGES = (
('en', _('English')),
('zh-hant', _('Traditional Chinese')),
)
Change your language code so that you have LANGUAGES like this;
LANGUAGES = (
('en', gettext('English')),
('ja', gettext('Japanese')),
('it', gettext('Italian')),
('zh-hant', gettext('Chinese')),
)
I've tested this on a multi-lingual site that I have.
python manage.py makemessages -l zh-hant
python manage.py compilemessages
Django admin then looks like this by default;

translation not working in Django 1.5 even after compiling message

I'm using Django 1.5
I have to enable internationalization in my application. For that, I have added a few things to the settings.py file
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
...
)
from django.conf import global_settings
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'...local context processrors...'
)
# global_settings.TEMPLATE_CONTEXT_PROCESSORS contains
# TEMPLATE_CONTEXT_PROCESSORS = (
# 'django.contrib.auth.context_processors.auth',
# 'django.core.context_processors.debug',
# 'django.core.context_processors.i18n',
# ...
# )
USE_I18N = True
USE_L10N = True
LANGUAGE_CODE = 'es'
# List of languages available for translation
ugettext = lambda s: s
LANGUAGES = (
('en', ugettext('English')),
('es', ugettext('Spanish'))
)
LOCALE_PATHS = (
os.path.join(PROJECT_ROOT, 'locale/'),
)
The LOCALE_PATHS has the location output as
('/media/path_to_project/workbench/workbench/settings/../locale/',)
But on running ./manage.py makemessages -l es it generates *.po file in
/media/path_to_project/workbench/workbench
instead of
/media/path_to_project/workbench/workbench/locale
Also, the compiled language is not showing in the template.
You need to check following things.
Ensure that you added USE_L10N = True and USE_I18N = True into your settings.py file
Check for PROJECT_ROOT and LOCALE_PATHS and make sure that it is correct
Add django.core.context_processors.i18n to TEMPLATE_CONTEXT_PROCESSORS in setting.py.
Translation and formatting are controlled by USE_I18N and USE_L10N settings respectively. However, both features involve internationalization and localization. The names of the settings are an unfortunate result of Django’s history.

django LANGUAGE_CODE not working

I have a Django 1.9.1 app. Some of it appears in French, but not all. When I visit one of my forms, I get some stock phrases in French ("Search" => "Rechercher", "Name" => "Nom"), but not any of my app's phrases (e.g., "Add New Patient", "Caregiver" are both in English).
I have language settings configured:
PROJECT_DIR = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.dirname(
os.path.abspath(__file__)))))
LANGUAGES = [
('fr', _('French')),
('en', _('English')),
]
USE_I18N = True
USE_L10N = True
LOCALE_PATHS = (
os.path.join(PROJECT_DIR, "locale"),
)
I have LocaleMiddleware configured:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
# LocaleMiddleware should be after SessionMiddleware and before CommonMiddleware
# See https://docs.djangoproject.com/en/1.9/topics/i18n/translation/#how-django-discovers-language-preference
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
I have a some translation files that came from "makemessages -l fr" with my phrases translated:
locale/fr/LC_MESSAGES/django.po
locale/fr/LC_MESSAGES/django.mo
I load i18n in my template.
{% load i18n %}
Some of it is in French, so that shouldn't be the problem anyway.
I am using the Quick Language Switcher to send the Accept-Language: HTTP header 'fr'.
The Django debug toolbar shows up in French (!), and says "Accept-Language: fr" in the HTTP request, and "Content-Language: fr" in the HTTP response. It also says the Django variable LANGUAGE_CODE is "en-us" (?), even though I have that setting commented out.
I have read "How Django discovers language preference" and I don't see what I'm missing.
My LOCALE_PATH was wrong. It was one level too high. Here is the right LOCALE_PATH for me:
BASE_DIR = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
LOCALE_PATHS = (
os.path.join(BASE_DIR, "locale"),
)

i18n in Django 1.3 doesn't work for me

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(__file__))
# 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