Internationalization of python/django program: how to implement placeholder in .po file? - django

E.g. in my django source I have:
{% blocktrans %}You are a member since {{sincewhen}}{% endblocktrans %}
What should go into the .po file? (django docs don't explain this part as far as I could find)
Is .po format for placeholders universal for use in programs written in different programming languages?
edit: I've tried commands
django-admin.py makemessages -l de -e=html,py
django-admin.py compilemessages
This did not work because of a bug that I found and described here.
After this was fixed I could get the message files generated with the command above (if you have other file extensions - modify -e option accordingly.

Read this section: Message files
It explains how to generate .po files and gives an example:
django-admin.py makemessages -l de
Make sure you read that whole section to get an idea of how to generate/edit these files. Also, if you're looking for a great tool to help you manage your translations (and even use Google translations to use as a starting place), check out django-rosetta.

Use:
python manage.py makemessages -l <target_language>
(or makemessages -a for all languages)
That'll automatically create all the msgid's in the po's, all that's left for you is to fill out the msgstr's.
.po is the standard format used by the gettext library, which is available in virtually every modern programming language.

Related

Is running makemessages necessary every time?

I'm reading the django documentation for makemessages but the wording is not very clear:
Runs over the entire source tree of the current directory and pulls out all strings marked for translation. It creates (or updates) a message file in the conf/locale (in the Django tree) or locale (for project and application) directory. After making changes to the messages files you need to compile them with compilemessages for use with the builtin gettext support.
In the text above it doesn't seem clear to me what it means that makemessages would "pull out" strings marked for translation. (pull them out for what? where to?)
Meanwhile the compilemessages description makes sense as this simply compiles the messages once I've changed the language files.
When, if ever, should I use makemessages if compilemessages does the job I am seeking? What is the point of makemessages?
makemessages is used to scan through your apps looking for strings marked for translation. Once it has theses strings it puts them in a po file. If you then add further strings for translating to your app then you will need to run makemessages to generate an updated po file. compilemessages uses the contents of the po file to create a mo file, so the po file must be updated first.
makemessages accepts a param for the locale:
pipenv run django-admin makemessages --locale=de
This allows you to create a po file for each of the locales your app supports.

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 django-admin.py makemessages -l en

I want to use multiple languages in my app
I do not want a locale file for english inside LOCALE folder but by mistake i have create this using this
django-admin.py makemessages -l en
so Please tell me how can I remove this 'en' folder which I have creates.
Because now if i try to create language folder for another language using this django-admin.py makemessages -l es it does not create.
WHY SO???
First of all there is no relation between having an english "en" directory in locale, and "es" not being created.
Secondly, if you want to remove en you could just remove that by rm -r en/
And es, as you are trying to do, has several variants.
probably you could try giving a a particular variant. This will help you with language codes. http://www.science.co.il/language/locale-codes.asp

Which Django internationalization files are being used?

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.

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