Which Django internationalization files are being used? - django

I can switch languages in my Django application by changing LANGUAGE_CODE in the Settings.py file for the application.
But I'm not sure where the actual text is coming from.
In path-to-django/contrib/auth/locale/, there are directories for many languages containing the translations of the text I'm displaying. But if I move an .mo file for a particular language to a new name, I still see text for that language -- even after I restart Django. So where does the text actually come from?
Also, for the 'en' locale, the translated text is always "" (empty string). Does ugettext_lazy just return its input string in that case? If not, where does the English text come from?

It is a difference, if you speak of translation in the django admin or within your application. The path you mentioned .../contrib/auth/locale refers to translations in the django admin.
For special translation within your application you should have a locale/ folder in your project. This folder is created when you run the django special script named "django-admin.py makemessages".
The script runs over your project source tree or your application
source tree and pulls out all strings marked for translation. It
creates (or updates) a message file in the directory
locale/LANG/LC_MESSAGES. In the de example, the file will be
locale/de/LC_MESSAGES/django.po.
For detailed explanation, please look at django i18n documentation
After you have created your message files (*.po) and after you have written your own translations in the message files, don't forget to compile them:
Compiling message files
After you create your message file -- and each
time you make changes to it -- you'll need to compile it into a more
efficient form, for use by gettext. Do this with the django-admin.py
compilemessages utility.
This tool runs over all available .po files and creates .mo files,
which are binary files optimized for use by gettext. In the same
directory from which you ran django-admin.py makemessages, run
django-admin.py compilemessages like this:
django-admin.py compilemessages
That's it. Your translations are ready for use.

It turns out there was a system-wide Django installation that was being used, rather than my local installation.
By creating a locale directory within my app, I'm able to override the strings used in the system-wide installation. I just modify the .po file there, and compile it.

Related

Django makemessage not updating .po files for installed apps

I'm working with a current project that has existing .po files in "locale" directories in multiple installed app directories. Currently each locale directory is explicitly mentioned in the LOCALE_PATHS even though the docs state it will search for locale directories in each installed app. I wanted to remove the values in LOCALE_PATHS and just let normal discovery work, but it's not working.
If I clear out LOCALE_PATHS and run manage.py makemessages it appears like it's doing something (time is spent processing), but no file changes occur. If I do makemessages --keep-pot then I can see all the .pot files are actually being created, but it's not actually creating the .po files for each language. Only if I explicitly pass a -l de I then get an updated .po file for the language stated and a message stating "processing locale de". It SHOULD be able to look at the LANGUAGES setting or what files already exist and properly update them, but that appears to only happen if every locale directory is explicitly added into LOCALE_PATHS. If I have all the locale paths in LOCALE_PATHS then I can just run manage.py makemessages and all .po files are properly updated.
This is on Django 3.0
UPDATE: I upgraded to Django 3.2 LTS and still have the same issue except now I must add -a to makemessages or it does nothing. Previously it seemed to assume a -a if not provided.

Django i18n's makemessages doesn't find .djhtml and .djt templates

I'm using i18n on Django to make my website multilingual.
I've recently changed my templates' extensions from html to djhtml, so that Emacs will do syntax highlighting, but after this change, python manage.py makemessages doesn't find the djhtml files (or doesn't consider them as templates) and doesn't create the entries on my django.po file for the translations in them. It even comments out translations that I previously had on html files.
If I make an exact copy of the djhtml file in the same folder, but with a html extension, then it works normally.
The same thing happens with djt as well.
How to I configure i18n to consider djhtml and djt files as templates as well?
I found the answer looking through Django's code.
You can use the -e or --extension to select which file extensions it's supposed to check.
From their own code, this flag determines:
The file extension(s) to examine (default: "html,txt,py", or "js" if
the domain is "djangojs"). Separate multiple extensions with commas,
or use -e multiple times.
So you can use it like so to detect djhtml and djt:
python manage.py makemessages -e 'html,txt,py,djt,djhtml'

Issues with multiple languages (django.po file)

I am using the concept of multiple languages in my app and for that I have done all those things which are written in the doc.
But when I creates a locale file inside my project and then the respective language folder "es" and thus a by default django.po file is created which contains all the lines which I can edit .
e.g.
#: customer_reg/my_new_env/lib/python2.6/site-packages/django/conf/global_settings.py:48
msgid "Arabic"
msgstr ""
But this django.po file does not contain my app templates files which I can edit.
As i said I have created this locale file inside my project parallel to my app,I don't know why it happens.
So I have to create this inside my app parallel to my models and views ?? OR there is any another problem ??
Create the locale directory in your app, at the same level where your settings.py file exists.
Now from the previous level i.e. where you created the locale directory, type the cmd to generate a source file[.po].
cmd like ./manage.py makemessages -l [language code]
Check the language code from http://translate.sourceforge.net/wiki/l10n/pluralforms
Tada..!
this should work for you, will incude your app templates.
Let me know if this doesnt work for u.

How do I make django translate certain files?

I'm running django-admin.py makemessages -l es from within my app directory to create trnaslation strings. The result includes only those texts that are located in my app directory. My templates directory to that app is located outside the app's directory. How do I ask django to translate my template files too?
I didn't want to run the above command from within the project's dir, because my project contains certain folders that I do not want to translate.
Never mind, I found the answer. You have to create symlinks to the folders you want to get translated (i.e. templaets) and copy those symlinks to you apps directory and run the above command with --symlinks included.
If i understand correctly you'll need to use django's trans and blocktrans
template tags to translate certain strings of text.

Django: Internationalization, makemessages doesn't create po files

I have an application located in one folder, and templates for it in another one...
I have added translation strings to the templates (which are stored in templates directory, I have one directory for all templates in my application)
When I go to the application folder and run a script there:
silver:articles oleg$ django-admin.py makemessages -l ru
processing language ru silver:articles
oleg$
I am getting empty
silver:articles oleg$ ls locale/ru/LC_MESSAGES/
silver:articles oleg$
And when I am running this command for example in project root, I am getting po file full made from python files (which seems strange to me because I thought it should be created from htmls)
makemessages always looks for strings marked for translation in python code files.
except for that, it looks in all .html files. maybe your templates have another extension? If that's the case you can use -e to specify other extension:
django-admin.py makemessages -l=ru -e=html,htm,txt