I know, I'm asking about something similar, requested a lot of times. But with a different point of view.
I'm working on a little django project. After developed the base functionalities, now I'm fighting with the multilanguage.
I was able to use a simple test template in my windows laptop. But when I copy this project to the test (CentOS) server it doesn't work.
Hereafter what I did.
The base environment (both systems) is virtualenv with python 2.7, django 1.5, mysql-python 1.2.3 and django-localeURL 1.5. gettext v.1.7.
These are the relevant options of settings.py (Note: I use django-localeURL application)
# Django settings for contact_site project.
import os.path
PROJECT_DIR = os.path.dirname(__file__) # this is not Django setting.
# ... skip
LANGUAGE_CODE = 'it'
#...skip
USE_I18N = True
USE_L10N = True
#...skip
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
# LocaleMiddleware after SessionMiddleware: it needs sessions
# commented out LocaleMiddleware to avoid conflict with LocaleURLMiddleware
# 'django.middleware.locale.LocaleMiddleware',
# LocaleURLMiddleware to manage multi language urls
'localeurl.middleware.LocaleURLMiddleware',
# CommonMiddleware after LocaleURLMiddleware or will not work APPEND_SLASH
'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',
)
# ... skip
_ = lambda s: s
LANGUAGES = (
('en', _(u'English')),
('it', _(u'Italiano')),
('fr', _(u'Francais')),
)
LOCALE_PATHS = (
# in windows laptop
'C:/...skip.../virtualenvs/djprj/contact_site/locale',
# in centos staging server
# '/var/www/html/virtualenvs/djprj/contact_site/locale',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.core.context_processors.i18n',
'django.contrib.auth.context_processors.auth',
)
TEMPLATE_DIRS = (
# ...skip
os.path.join(PROJECT_DIR, "templates"),
)
INSTALLED_APPS = (
# localeurl to manage multi language urls. it must be the 1st one!
'localeurl',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# ... skip
# my application
'contact',
)
# ... skip
my project urls.py (the home only):
# ... skip
urlpatterns += patterns('',
url(r'^$', 'contact_site.views.home', name='home'),
)
my views.py (the home only):
#... skip
def home(request):
return render(request, 'minimalistic.html', {})
and finally my minimalistic template:
{% load debug_tags %}
{% load i18n %}
<!DOCTYPE html>
<html>
<head>
<title>TITLE</title>
</head>
<body>
{# {% set_trace %} #}
<p>{% trans "Hello" %}</p>
<p>{% blocktrans %}Hello guys{% endblocktrans %}</p>
lingua richiesta: {{ request.LANGUAGE_CODE }}
</body>
</html>
Then I produced the dictionaries of messages using
django-admin.py makemessages --all
I edited the dictionaries (using poedit)
and finally, running development server, in windows I saw the coveted result:
peeking from browser http://127.0.0.1:8000/it gave me Salve...
while getting http://127.0.0.1:8000/en gave me Hello ...
Unfortunatly in the stage system I got Hello ... from both URLs.
Just to be safe, in centos I tried even
django-admin.py compilemessages
this operation was ok, but without different results about the minimalistic.html template rendering
And last, yes, I cleared my browser cache between tests.
Any suggestion about what am I missing? How can I debug this strange behaviour?
Well, I'm sorry and a little embarassed.
Today, debugging django/utils/translate/trans_real.py, I have realized the presence of a subtle typo mistake in the staging server settings.py file.
I cannot count how many times yesterday I checked that configuration file, missing the point!
Sorry again
Related
context: Django 3.1 app deployed to a AWS lambda with terraform. I have not set up my production settings yet, this is my dev server.
I'm using https://github.com/adamchainz/apig-wsgi
I noticed that messages from django.contrib.messages don't appear when attempting to get the api key with djangorestframework-api-key via the admin. It being on the lambda means I'm unable to access the lambda REPL to create the api key programmatically.
note: django-debug-toolbar also does not work and does so quietly, so I have a sneaking suspicion my context_processors settings are off so I don't have messages or DEBUG variables but I can't find the issue in my settings?? Below is my code without debug toolbar settings.
project/settings/base.py
DEBUG = True
INSTALLED_APPS = [
'app',
'rest_framework',
'rest_framework_api_key',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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',
]
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',
],
},
},
]
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'
AWS_STORAGE_BUCKET_NAME= [redacted]
AWS_DEFAULT_ACL = [redacted]
AWS_QUERYSTRING_AUTH= [redacted]
AWS_S3_REGION_NAME = [redacted]
AWS_LOCATION = [redacted]
USE_I18N = True
STATIC_URL = '/static/'
REST_FRAMEWORK = {
...
'DEFAULT_PERMISSION_CLASSES': [
"rest_framework_api_key.permissions.HasAPIKey",
]
}
project/wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
application = get_wsgi_application()
lambda_function.py
def install_secrets():
"""Add the secrets from the secret named by ENV_SECRET_ID to os.environ"""
import os
secret_id = os.environ.get('ENV_SECRET_ID')
if not secret_id:
return
import boto3
import json
session = boto3.session.Session()
client = session.client('secretsmanager')
response = client.get_secret_value(SecretId=secret_id)
overlay = json.loads(response['SecretString'])
os.environ.update(overlay)
def manage(event, context):
"""Entry point for running a management command. Supported formats:
- "migrate!"
- ["migrate"]
- {"command": ["migrate"]}
"""
if isinstance(event, dict):
command = event['command']
else:
command = event
if isinstance(command, str):
command = command.split()
install_secrets()
from django.core.wsgi import get_wsgi_application
get_wsgi_application() # Initialize Django
from django.core import management
return management.call_command(*command)
_real_handler = None
def lambda_handler(event, context):
"""Entry point for web requests"""
global _real_handler
if _real_handler is None:
print('No Cache, getting secrets.')
install_secrets()
from apig_wsgi import make_lambda_handler
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
application = get_wsgi_application()
_real_handler = make_lambda_handler(application)
return _real_handler(event, context)
default admin template admin/base.html where I'm expecting messages to show up (its just looping through the messages object, shouldn't need any JS or CSS to appear):
https://github.com/django/django/blob/2d8232fa716f5fe46eec9f35a0e90c2d0f126279/django/contrib/admin/templates/admin/base.html#L82
{% block messages %}
{% if messages %}
<ul class="messagelist">{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
{% endfor %}</ul>
{% endif %}
{% endblock messages %}
requirements.txt
Django>=3.1,<3.2
django-storages==1.11.1
apig-wsgi
psycopg2-binary
djangorestframework
djangorestframework-api-key==2.1.0
attempts:
tried hard reload F5 and all that
tried adding django debug toolbar and logging the apikey that shows up in messages in the admin, but didn't work because toolbar also doesn't show up (not shown in code above)
tried collectstatic on dev server
tried checking user sessions and seeing the messages on user sessions and there are messages there
A talented colleague fixed this. Adding
MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'
to your settings resolves the issue.
Credit goes to https://stackoverflow.com/users/1394697/flipperpa
In my Django 2.0 site, I want to set the lang atribute of the html tag to the current locale's language. In my base.html which other templates extend, I use get_current_language in the following way
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE }}">
...
</html>
The site has translations for multiple languages. If I switch the language in the browser, I see the correct translations, but the lang attribute will always contain en.
In my settings.py I have
USE_I18N = True
LANGUAGE_CODE = 'en-us'
and based on the suggestion of Goran the following middleware order
MIDDLEWARE = [
'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',
]
The LANGUAGES setting is unset.
As suggested by Kostadin Slavov I have tried printing the language from the view. It seems that get_current_language calls django.utils.translation.get_language, so I have inserted the following in my view
from django.utils import translation
print(translation.get_language())
It prints the correct value (eg de when accessing the view with a browser set to German).
What else am I missing?
I tried to simulate your environment with these steps:
$ cd ~
$ python3 -m venv ~/venvs/mysite
$ source ~/venvs/mysite/bin/activate
$ pip install django==2.0.8
$ django-admin startproject mysite
Then I updated the generate code as in your example:
mysite/settings.py
...
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',
]
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['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',
],
},
},
]
...
mysite/urls.py
from django.contrib import admin
from django.urls import path
from django.views.generic.base import TemplateView
urlpatterns = [
path('', TemplateView.as_view(template_name='base.html'), name='home'),
path('admin/', admin.site.urls),
]
templates/base.html
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE }}">
<body>
<pre>LANGUAGE_CODE = {{ LANGUAGE_CODE }}</pre>
<body>
</html>
With the Django generated code and my few above updates I can see different language code if I switch the language of my browser visiting http://localhost:8000/ after starting it with:
$ python manage.py runserver
Try my steps on your local environment and check if it works, and then compare your project to the code above.
Update
Try to use diffsettings to see "differences between the current settings file and Django’s default settings".
Had the same problem. In my case, the function was called async and always returned the default language.
SOLUTION: pass language from 'main' context.
Code like in this example:
def get_context_data( self, **kwargs ):
context = super().get_context_data(**kwargs)
lng_code = get_language() # -> 'de'
#sync_to_async
def get_data():
context['data1'] = Model1.objects.filter(language==get_language()) # get_language() -> 'en'
#sync_to_async
def get_data2():
...
#sync_to_async
def get_data3():
...
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
asyncio.gather(
get_data1(),
get_data2(),
get_data3()
))
loop.close()
I want to translate my website to other language , i've configured my settings.py as it should be but no changes are happens.
I've created messages and i can see them inside my local folder, also i've compiled them successfully without any erors.
Here is my settings :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# My apps
'main',
# Third part apps
'captcha',
'jsonify'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
LOCAL_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
LANGUAGE_CODE = 'en'
LANGUAGES = (
('ru', _(u'RU')),
('en', _(u'EN'))
)
My views:
from django.utils.translation import gettext as _
def main(request):
context = {
'title':_('Главная'),
}
return render(request, 'index.html', context)
Also i've loaded the i18n above inside my template where the translations happens index.html:
{% load static %}
{% load i18n %}
<title>{% block head_title %}{{title}}{% endblock %}</title>
I got it to work, instead of typing LOCALE_PATHS i typed LOCAL_PATHS, now everything is working just fine .
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
I'm running Django 1.2 beta and trying out the new feature: message framework.
http://docs.djangoproject.com/en/dev/ref/contrib/messages/
Everything seems to work, but when I try to output the messages, I get nothing. Seems that messages variable is empty. I double checked all the settings, they seem to be just like in the manual. What could be wrong?
settings.py
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', #send messages to users
'django.middleware.locale.LocaleMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
#debug tool
'debug_toolbar.middleware.DebugToolbarMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.messages.context_processors.messages', #send messages to users
'django.core.context_processors.auth',
)
#Store messages in sessions
MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage';
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
#'django.contrib.sites',
'django.contrib.admin',
'django.contrib.messages',
'debug_toolbar',
#my apps
#...
)
views.py
def myview(request):
from django.contrib import messages
messages.error(request, 'error test');
messages.success(request, 'success test');
return render_to_response('mytemplate.html', locals());
mytemplate.html
{% for message in messages %}
{{ message }}<br />
{% endfor %}
In template nothing is outputted.
You'll need to use a RequestContext in your call to render_to_response.
return render_to_response('mytemplate.html', locals(),
context_instance=RequestContext(request))
See the note a few screens down under this documentation section.