2👍
The way I went about it is by using the exact language code that django uses in it’s own translation files (and not by the link provided inside settings.py), assuming this language is supported (if not things get complicated, since you have to provide your own translation files to django as well).
I found this code by going to $DJANGO_DIR/conf/locale and looking at the folder’s name (for me it was at /usr/local/lib/python2.6/dist-packages/django/conf/locale, but it may differ depending on OS and such).
71👍
Just add the paths of the locale files generated to the settings.py
file like the following
LOCALE_PATHS = ( "/xxx/xxx/Projects/xxx/sites/avb/locale/",)
- [Django]-How can I detect Heroku's environment?
- [Django]-Django – how to unit test a post request using request.FILES
- [Django]-Success_url in UpdateView, based on passed value
28👍
This is a full solution that I have been using from Django 1.4 and still in 1.7.1:
In settings.py …
Add to MIDDLEWEAR_CLASSES, locale, it enables language selection based on request:
'django.middleware.locale.LocaleMiddleware',
Add LOCALE_PATHS, this is where your translation files will be stored:
LOCALE_PATHS = (
os.path.join(PROJECT_PATH, 'locale/'),
)
Enable I18N:
USE_I18N = True
Set LANGUAGES that you will be translating the site to:
ugettext = lambda s: s
LANGUAGES = (
('en', ugettext('English')),
('fr', ugettext('French')),
('pl', ugettext('Polish')),
)
Add i18n template context processor to TEMPLATE_CONTEXT_PROCESSORS, requests will now include LANGUAGES and LANGUAGE_CODE:
'django.core.context_processors.i18n',
In urls.py :
In url_patterns, add the below, it will enable the set language redirect view:
url(r'^i18n/', include('django.conf.urls.i18n')),
See Miscellaneous in Translations for more on this.
Add the following imports, and encapsulate the urls you want translated with i18n_patterns. Here is what mine looks like:
from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import ugettext_lazy as _
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^i18n/', include('django.conf.urls.i18n')),
)
urlpatterns += i18n_patterns('',
(_(r'^dual-lang/'), include('duallang.urls')),
(r'^', include('home.urls')),
)
Now anywhere you use text and want to convert it, import lazytext and wrap every string with it like so _(‘text’), you can even go to your other urls.py files and do url translation like so:
url(_(r'^dual_language/$'), landing, name='duallang_landing'),
You can wrap text that you want translated in your other files, such as models.py, views.py etc.. Here is an example model field with translations for label and help_text:
name = models.CharField(_('name'), max_length=255, unique=True, help_text=_("Name of the FAQ Topic"))
In your html templates…
Do same for your templates and load the i18n templatetag and use trans and transblock on the static stuff you want to translate. Here is an example:
{% load i18n %}
{% trans "This is a translation" %}<br><br>
{% blocktrans with book_t='book title'|title author_t='an author'|title %}
This is {{ book_t }} by {{ author_t }}. Block trans is powerful!
{% endblocktrans %}
Now run a makemessages for each of your locales:
./manage.py makemessages -l pl
And now all is left is to go into your /locales folder, and edit each of the .po files. Fill in the data for each msgstr. Here is one such example of that:
msgid "English"
msgstr "Angielski"
And finally compile the messages:
./manage.py compilemessages
For model instance data translation you can use some of the reusable packages available like
There is a lot more to learn with translations and internationalization is closely related to this topic, so check out the docs for it too. I also recommend checking out some of the internationalization packages available for Django like django-rosetta, and django-linguo. They help translate model content, django-rosetta does not create new entries for this in your database, while django-linguo does.
If you followed this you should be off to a good start. I believe this is the most standardized way to get your site running in multiple languages. Cheers!
- [Django]-How to repeat a "block" in a django template
- [Django]-Django: Add non_field_error from view?
- [Django]-How to get a favicon to show up in my django app?
20👍
I may be wrong – as the only time I used translation stuff was on a test project many moons ago – but I think you don’t want this:
$ django-admin.py makemessages -l he-il -e html
But rather this:
$ django-admin.py makemessages -l he_il -e html
Notice the underscore in he_il
.
I was having issues with pt-BR too, until I made the messages file with pt_br instead. Then things started working…
Yeah, it is not obvious and I couldn’t find documentation about it anywhere.
Hope that helps.
- [Django]-What does "'tests' module incorrectly imported" mean?
- [Django]-Write only, read only fields in django rest framework
- [Django]-How to add new languages into Django? My language "Uyghur" or "Uighur" is not supported in Django
15👍
I have the same problem. But I solve it by putting “Language:” to .po file. In my case .po file does not contain the “Language:” attribute, it looks like…
"Language-Team: LANGUAGE \n" "Language: \n" "MIME-Version: 1.0\n"
but when I put language code (in my case ‘ru’ or ‘en’)
"Language-Team: LANGUAGE \n" "Language: ru\n" "MIME-Version: 1.0\n"
it works for me
- [Django]-Django Password Generator
- [Django]-How do I render jinja2 output to a file in Python instead of a Browser
- [Django]-Import data from excel spreadsheet to django model
5👍
I had the same issues, it seems that your locale path have to end with a slash :
LOCALE_PATHS = (
'/dir/to/my/locale/',
)
- [Django]-What is the clean way to unittest FileField in django?
- [Django]-Django query get last n records
- [Django]-Django post_save signals on update
2👍
Yes you do need to make message files as celopes suggests and then compile them
python manage.py compilemessages
But you will still have a problem.
Disable LocaleMiddleware for a bit, i.e. remove this
django.middleware.locale.LocaleMiddleware
from your middleware list. Don’t use it if you do not need to switch the language at run time, but if you do need it, then there is a solution. I had the same problem before and someone explained this to me.
Also I had this weird issue before. Makemessages command would choke on strings wrapped with backslash in .py files.
- [Django]-How to unit test file upload in django
- [Django]-Django excluding specific instances from queryset without using field lookup
- [Django]-Querying django migrations table
1👍
I had very the same issue, i tried to switch my language and django said no go. No error, no warning, but django switched language to pl-pl (in my case). However removing all folders from locale and executing command:
django-admin.py makemessages -l pl_PL (underscore instead of dash and capital letter for second PL, worked this issue out).
I hope it helps some guys out there.
- [Django]-How can I create a deep clone of a DB object in Django?
- [Django]-How to add multiple arguments to my custom template filter in a django template?
- [Django]-How to set django model field by name?
0👍
I had the same issue. I explained it with details in this link under another similar question.
Briefly, my problem has been solved by reloading the server with this code:
sudo /etc/init.d/uwsgi reload
Now, everytime I change the phrases and after compiling language files, I reload the server too to see the changes.
- [Django]-How can I build multiple submit buttons django form?
- [Django]-Creating a dynamic choice field
- [Django]-Need to convert a string to int in a django template