django internationalization and translations issue - django

I have a problem with django translations.
Problem 1 - I updated string in django.po file, but the change does not appear on the webpage.
Problem 2 - I have created my own locale file with django-admin.py makemessages -l et, added the translation string into file, but they too do not appear on the page.
I do not think this is setting problem, because the translations from django.po file do appear on the website, its just the changes and the translations from my own generated file that do not appear.
Edit:
My settings.py contains this:
gettext = lambda s: s
LANGUAGE_CODE = 'et'
LANGUAGES = (
('et', gettext('Estonian')),
)
my own locale files are in
/path/to/project/locale/et/LC_MESSAGES/
and the files are
django.mo and django.po
the file I refer to in problem 1 is django own et transaltion, which I changed.

Well, I got this same error a few moments ago. I solved it deleting the "#, fuzzy" tag over the translation strings in my django.po files. It seems that translated text is not served if it got this tag, so make sure to translate the text and then delete this line.
Here is an example of a translated text not server on a po file:
#: course/models.py:13
#, fuzzy
msgid "code"
msgstr "código"
So, just delete the flag and leave it like this:
#: course/models.py:13
msgid "code"
msgstr "código"
I hope this work for you. Good luck!
Reference: http://share-experiences.com/blog/what-fuzzy-means-python-django-gettext/
PD: I know you got this issue a few month ago, but I leave this response due that you we never heard if you got this problem solved.

Had a same/similar issue with translations not showing up. Setting the LOCALE_PATHS fixed the issue:
# settings.py
USE_I18N = True
USE_L10N = True
LOCALE_PATHS = (
'/path/to/djangoapp/locale',
)

Translation files (PO) are loaded in memory only one time, changes to the PO files are not picked up by Django. In order to load the new translation files you need to restart Django (eg. stop/start runserver, Apache or NGINX).

One additional reason for Django translations not working is to compile the .po file with a Python version different than the one being used to run your application. Make sure you use the same version.

Make sure to use ugettext_lazy and not ugettext

If you are using gettext.translation to get the translations, i.e:
text_de = gettext.translation('django', locale_dir, ['de'], fallback=True).ugettext('Welcome to my site')
... and your translation works on the development server but not on production, note that locale_dir must point to your locale directory. It might be located elsewhere on one of the systems. Spent like 2 hrs finding it.

Check for the USE_I18N setting. More info. Anyway, I think by default it's True...

Related

Django how to not comment some lines with makemessages

Problem
I translate my group names in my Django application with the default translation tools.
Since my group names are not hard-coded in my code, when I run makemessages, the lines corresponding to my group names are commented out.
Example
I have a group named management_product which is automatically created during migrations. I put these lines in django.po:
msgid "management_product"
msgstr "Gestion des produits"
But if I run django-admin makemessages -l fr, they are commented out:
#~ msgid "management_product"
#~ msgstr "Gestion des produits"
Question
How can I disable this behaviour?
Django translations are not meant for dynamic data translations that come from third party data sources like your db. It is meant to translate the static stuff you have in your codebase.
You may want to use something like: https://django-modeltranslation.readthedocs.io/en/latest/ instead.

How to setup email translation with Django Allauth

I am trying to setup translations for emails with django-allauth.
I have rewritten my templates, translated my .po files and complied them.
The html translations work fine but for some reason just the emails don't get translated.
I have properly configured translations following the django tutorial
https://docs.djangoproject.com/en/1.10/topics/i18n/translation/#how-django-discovers-language-preference
The related question doesn't help:
How does email translation work with django allauth?
Package versions:
Django==1.10
django-allauth==0.27.0
I had the same problem with django-allauth==0.32.0 and Django==1.11.1. Translations that are shipped with allauth are simply outdated or incomplete. In my project, for example, an email with a link to reset the password was always in English (in default language LANGUAGE_CODE = 'en') although the user set the language to Czech.
In .po file for Czech translations: https://github.com/pennersr/django-allauth/blob/master/allauth/locale/cs/LC_MESSAGES/django.po#L408 is this e-mail translation marked as fuzzy – so translations are not used. The same applies to other languages I've checked (de, es...).
The reason is explained in this comment from the author of allauth:
The allauth templates are merely meant as a starting point, something
to get you going quickly. Hence, I do not treat issues in the
translations as blocking for release, which can result in translations
getting left behind. Feel free to submit pull requests to fill in the
gaps.
Source: https://github.com/pennersr/django-allauth/issues/1113#issuecomment-141189606
I think there are two ways to solve this issue:
Fix/update translations on https://www.transifex.com/django-allauth/django-allauth/, submit pull request on Github and wait for a new release.
My recommendation: copy all templates from allauth to your project, make your own *.po files and ignore all default translations/templates. Do not forget that e-mail templates are in *.txt so you must call makemessages this way: python manage.py makemessages ... --extension html,txt,py.

Why are some strings in Django .po translated while others not?

This is the first time I used Django localization.
I generated the .po files with makemessages and randomly select a
few strings to fill in the translations, just to check if
localization works.
Then I generate the .mo file with
compilemessages.
I go to the web page and only see a string
'Username' translated, most other strings don't get the translated
version displayed.
What is going on here?
EDIT:
I found out why 'Username' is translated, it used the default translation in Django, but why Django didn't use my mo file is beyond me. I followed all the instructions in i18n doc.
I set the LOCALE_PATHS variable in settings.py to the path for my localization files.
I tried different LANGUAGE_CODE settings 'zh-cn', 'zh_CN'(both the setting variable and the directory name).
I tried msgunfmt django.mo, the file is valid.
There are some lines close to the beginning of po file:
#, fuzzy
msgid ""
msgstr ""
I believe this is normal.
I finally got it working after hours of trial and error.
I change my LOCALE_PATHS from:
LOCALE_PATHS = ("/path/to/locale/");
to:
LOCALE_PATHS = ("", "/path/to/locale/");
And it works right away. Maybe it is a bug with Django 1.5 (which I am using), or maybe I configured something wrong.
Anyway, hope this helps someone, and save you hours of time.
=========EDIT===========
As pointed out by #J.C.Leitão, you have to add comma to make the variable a tuple.
It was a rookie mistake of mine. But I think Django could be more friendly to developers if a single string is recognized, too.

django translation not working by django-rosetta

I have installed the django-rosetta for translation, made .po and .mo files through django command
django-admin.py makemessages -l ar
and translate all words through django-rosetta,
now when i change my language to Arabic from English, it show some words in Arabic and some in English, although i have translated all these words in Arabic,
Have you restarted you web server?
This means your project's labels will be translated right away, unfortunately you'll still have to restart the webserver for the changes to take effect. (NEW: if your webserver supports it, you can force auto-reloading of the translated catalog whenever a change was saved. See the note regarding the ROSETTA_WSGI_AUTO_RELOAD variable in conf/settings.py.
-- http://code.google.com/p/django-rosetta/
What kind of server are you running?
I had the same problem with Apache2 and FastCGI. I had to restart the FastCGI Process to see the modifications.
Have you added the local path properly to your settings?
PROJECT_PATH = os.path.split(os.path.abspath(os.path.dirname(__file__)))[0]
LOCALE_PATHS = (
os.path.join(PROJECT_PATH, "locale"),
)
Do you have translations marked as fuzzy? (In Rosetta you can filter to display only fuzzy translations.) If some strings are fuzzy, make sure they are properly translated, uncheck the fuzzy flag and save the translations.
Another reason for translations not showing up could be that some python formatting strings (e.g. 'My %s apple') are not properly reproduced in the translated string. If formatting codes are unbalanced Rosetta should warn you, and the actual translation doesn't get written to the file.

How do I make Django's DATETIME_FORMAT active?

Where should DATETIME_FORMAT be placed for it to have effect
on the display of date-time in the Django admin site
(Django’s automatic admin interface)?
Documentation for DATETIME_FORMAT, on page
http://docs.djangoproject.com/en/1.0/ref/settings/, says:
"The default formatting to use for datetime fields on
Django admin change-list pages -- and, possibly, by
other parts of the system."
Update 1: DATETIME_FORMAT is broken (the value of it is
ignored), despite the documentation. Many years ago it
worked, but since then the Django implementations have been
broken wrt. this feature. It seems the Django community
can't decide how to fix it (but in the meantime I think they
should remove DATETIME_FORMAT from the documentation or add
a note about this problem to it).
I have put these lines into file "settings.py" of the
website/project (not the app), but it does not seem to have
any effect (after restarting the development server):
DATETIME_FORMAT = 'Y-m-d H:i:sO'
DATE_FORMAT = 'Y-m-d'
As an example "June 29, 2009, 7:30 p.m." is displayed when
using Django admin site.
Django version is 1.0.2 final and Python version is 2.6.2
(64 bit). Platform: Windows XP 64 bit.
Stack Overflow question European date input in Django Admin seems to be about the exact opposite problem (and thus an apparent
contradiction).
The full path to file "settings.py" is
"D:\dproj\MSQall\website\GoogleCodeHost\settings.py". I now
start the development server this way (in a Windows command
line window):
cd D:\dproj\MSQall\website\GoogleCodeHost
set DJANGO_SETTINGS_MODULE=GoogleCodeHost.settings
python manage.py runserver 6800
There is no difference. Besides these are positively read
from file "settings.py":
DATABASE_NAME
INSTALLED_APPS
TEMPLATE_DIRS
MIDDLEWARE_CLASSES
"django-admin.py startproject XYZ" does not create file
"settings.py" containing DATETIME_FORMAT or DATE_FORMAT.
Perhaps there is a reason for that?
The sequence "d:", "cd D:\dproj\MSQall\website\GoogleCodeHost",
"python manage.py
shell", "from django.conf import settings",
"settings.DATE_FORMAT", "settings.DATETIME_FORMAT" outputs
(as expected):
'Y-m-d H:i:sO'
'Y-m-d'
So the content of file "settings.py" is being read, but does
not take effect in the Django Admin interface.
With:
USE_L10N = False
DATE_TIME takes effect, since the localization of l10n overrides DATETIME_FORMAT and DATE_FORMAT as documented at: https://docs.djangoproject.com/en/1.9/ref/settings/#date-format
As Ciro Santilli told, localization format overrides DATETIME_FORMAT in settings when USE_L10N = True. But you can still override DATETIME_FORMAT and other date/time formats by creating custom format files as described in Django documentation.
See detailed answer here.
You can override DATE_FORMAT, DATETIME_FORMAT, TIME_FORMAT and other date/time formats when USE_L10N = True by creating custom format files as described in Django documentation.
In summary:
Set FORMAT_MODULE_PATH = 'yourproject.formats' in settings.py
Create directory structure yourproject/formats/en (replacing en with the corresponding ISO 639-1 locale code if you are using other locale than English) and add __init__.py files to all directories to make it a valid Python module
Add formats.py to the leaf directory, containing the format definitions you want to override, e.g. DATE_FORMAT = 'j. F Y'.
Example from an actual project here.
This will solve the particular problem that is not possible
with DATETIME_FORMAT (as it is ignored in the current Django
implementations despite the documentation), is dirty too and
is similar to ayaz's answer (less global - will only affect
the admin site list view):
Right after the line
(date_format, datetime_format,time_format) = get_date_formats()
in file (Django is usually in folder Lib/site-packages in
the Python installation)
django/contrib/admin/templatetags/admin_list.py
overwrite the value of datetime_format (for a
models.DateTimeField in the model):
datetime_format = 'Y-m-d H:i:sO'
And for date-only fields:
date_format = 'Y-m-d'
Restart of the web-server (e.g. development server) or
logging out of the admin interface is NOT necessary for
this change to take effect. A simple refresh in the web-browser
is all what is required.
The two setting directives should be defined in settings.py. Could you ensure that the same settings.py that you are editing is being read when you start the development server?
You could always drop to the Python interactive shell by running python manage.py shell, and run these commands to ensure whether the date/time format values are getting through fine:
from django.conf import settings
settings.DATE_FORMAT
settings.DATETIME_FORMAT
Ok, I forgot to look it up, but ticket #2203 deals with this. Unfortunately, the ticket remains in pending state.
I remember that for a project that used a certain trunk revision of the 0.97 branch of Django, I worked around that by overwriting the date_format and datetime_format values in the get_date_formats() function inside django/utils/translation/trans_real.py. It was dirty, but I had already been using a custom Django of sorts for that project, so didn't see anything going wrong in hacking it trifle more.