28π
I solved my issue. In my case, the problem was with the LOCALE_PATHS
definition in settings.py.
I tested it in the view by :
from TranslationTest import settings
return HttpResponse(settings.LOCALE_PATHS)
It was showing home/myProjects/TranslationTest/TranslationTest/locale
, however makemessages
was producing the files in home/myProjects/TranslationTest/locale
so I changed my settings as follows :
SITE_ROOT = os.path.dirname(os.path.realpath(__name__))
LOCALE_PATHS = ( os.path.join(SITE_ROOT, 'locale'), )
and now it works.
But I still wonder, why didnβt makemessages
understand that it should create the files in the LOCALE_PATHS
specificed by settings.py?
In my understanding, it always produces the locale files in SITE_ROOT/locale, so we should always set LOCALE_PATHS to this? If this is a default, why set it at all? I would appreciate further information on this issue.
Thanks for all the help !
9π
This happens if you have the locale middleware activated. By so doing, I think your session local depends on your browser locale.
if you comment the locale middleware, it should be working as expected.
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',
]
- Django rest framework group based permissions for individual views
- Django Bi-directional ManyToMany β How to prevent table creation on second model?
- About index and primary key in SQL?
7π
and keep in mind if your language has a country in it e.g. fa-IR, in your settings file you must add:
LANGUAGE_CODE = 'fa-IR'
but when you want to call makemessages command you should change -
to _
:
makemessages -l fa_IR
- Filter on datetime closest to the given datetime
- Django update one field using ModelForm
- How to display "x days ago" type time using Humanize in Django template?
6π
I tried almost all of the offered solutions above without any success. Then I realised that I forgot to run compilemessages
after makemessages
command. So make sure you run:
python manage.py compilemessages
After running makemessages. What the compilemessages
command does is it converts .po
files to .mo
files in your locale directories. This is needed because .mo
files are the only files that gettext can understand. Without .mo
files translation wonβt work, which was the case for me.
2π
As of today, with Django 3.2 this issue is still present.
In my case, I have the following setup:
C:.
ββββdocuments
ββββproject
β ββββapps
β β ββββaccounts
β β β ββββ...
β β ββββcommon
β β β ββββ...
β β ββββ...
β ββββlocale
β β ββββit
β β ββββLC_MESSAGES
β ββββ...
ββββtests
The settings file is set in this way:
LANGUAGE_CODE = 'it'
LOCALE_PATHS = [
os.path.join(Path(__file__).resolve().parent, "locale"),
]
LANGUAGES = [
('en', _('English')),
('it', _('Italian')),
]
The trick is in the definition of LOCALE_PATHS. Django-admin is in some way not synced with the Django environment.
1π
I just want to remind something that cause me a tiny problem and it was that I should change language of browser(from options).
However I did everything completely but I couldnβt see the translated texts.
And by the way I didnβt want to use any specific url for seeing the translation.
- Use Django dumpdata to dump a subset of overall data?
- How can I pass kwargs in URL in django
- How to test django model method __str__()
0π
For me commenting en line works OK
USE_I18N = True
USE_L10N = True
LANGUAGE_CODE = 'uk'
LANGUAGES = (
('uk', _('Ukrainian')),
# ('en', _('English')),
)
no locale folder in a project root, no custom LOCALE_PATHS
0π
adding the local middle ware to the django middle ware worked for me 'django.middleware.locale.LocaleMiddleware',
- DateTimeField received a naive datetime
- What is a good Django workflow?
- Django message template tag checking
- Static files won't load when out of debug in Django
0π
You might need to read this How Django discovers language preference.
TLDR;
The locale middleware decides the user language in the following order:
i18n_patterns
function in your rootURLconf
- Failing that, it looks for a cookie set by the
LANGUAGE_COOKIE_NAME
setting - Failing that, it looks at the Accept-Language HTTP header sent by your Browser
- Failing that, it uses the global
LANGUAGE_CODE
setting
In my case I followed a tutorial and I didnβt had a clue what why my language was not changing.
I was already done with the locale folder where i had all the strings for both of languages translated and i had already setup the LOCALE_PATH
and other stuffs correctly.
So for testing i changed the LANGUAGE_CODE
to display the text in hindi but i was not aware of the language precedence stuff.
i had set the following in my settings
LANGUAGES = (
('hi', _('Hindi')),
('en', _('English')),
)
LANGUAGE_CODE = 'hi'
So what was happening was that i had no cookie that specified the language so the precedence fell for the browsers default language passed in http header.
So it never came to the LANGUAGE_CODE
in my settings and the language in the request.LANGUAGE_CODE
was set to en.
So for solving this issue i set the cookie to take precedence before my browser
from django.conf import settings
from django.http import HttpResponse
from django.utils import translation
user_language = 'hi'
translation.activate(user_language)
response = HttpResponse(...)
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, user_language)
I took this from here Explicitly setting the active language
- How to append pages of data using jQuery and Django pagination?
- Django: ContentTypes during migration while running tests
0π
you can try Candy Translate instead of Django native translations module. It is much easier, do not require to set up the middleware and basically does most of the work for you.
It also allows you to manage the translations in the excel file which comes handy when you are sending the texts to the translators.
It comes with some limitations but for all my websites it was just enough and achieved much faster.
- Django admin inline many to many custom fields
- Django Gunicorn not load static files
- Is there a command for creating an app using cookiecutter-django?
- How to access RequestContext in class-based generic views?
0π
You need to put the JavaScript code in a file inside the static folder(you need to run the collectstatic command) in order to the djangojs.po
to be generated. Because Django by default look at this destination when dealing with JS files.
- Django datetime field β convert to timezone in view
- How can I use `email` in "django-rest-framework-simplejwt" instead of `username` to generate token?
- Django; AWS Elastic Beanstalk ERROR: Your WSGIPath refers to a file that does not exist
- Django β Custom Admin Actions Logging