2

I feel bad for asking this as it was asked so many times:

django - how to make translation work?

django internationalization and translations issue

How to setup up Django translation in the correct way?

http://askbot.org/en/question/8948/weve-edited-djangopo-files-but-translations-do-not-work-why/

I want to have English (default) and Slovenian languages. My settings are like this:

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
)
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/Belgrade'
USE_I18N = True
USE_L10N = True
USE_TZ = True
from django.utils.translation import ugettext_lazy as _
LANGUAGES = (
  ('si', _('Slovenian')),
  ('en-us', _('English')),
)
LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)

Urls.py:

urlpatterns = i18n_patterns('',
    url(r'^', include('analytics.urls')),
    url(r'^login', RedirectView.as_view(url='/admin/login', permanent=False)),
    url(r'^admin/', include(admin.site.urls)),
)

Templates:

<div class="time_range">{% trans "Time range:" %}</div>

I compiled messages to .po file and now according to the documentation one would expect this to start working. But no luck for me. If I visit the url with /si/ prefix I still see the english strings.

Community
  • 1
  • 1
user568021
  • 1,426
  • 5
  • 28
  • 55
  • code of `urls.py` is wrong (extra `[` symbol). Is it just a typo or maybe this file is not imported at all? – zymud Sep 15 '15 at 10:52
  • oh.. sorry this was just a typo.. – user568021 Sep 15 '15 at 12:31
  • I've got that error once, because I was using spanish with english and nano (the console text editor) then after a several tries I can see some weird behavior of some non-ascii characters in the .po file, I fixed those and everything work, could you put your django.po file? or perhaps translate only one string to some non-sense like 'this is to test i18n in django' (without non-ascii characters) and see if it works? – diegueus9 Sep 16 '15 at 01:04
  • 1
    you need to makemesages and compilemessages after having translated each of them. You can have a look to django-rosetta. But if you haven't tranlated them, of course it'll still show the english text – DevLounge Sep 18 '15 at 22:42

2 Answers2

6

After creating your .po files, there are two more steps:

  1. Translate the messages in your .po file
  2. Run ./manage.py compilemessages to compile your .po file into a .mo file

To quote the Django translation documentation, 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 compilemessages utility.

Joey Wilhelm
  • 5,729
  • 1
  • 28
  • 42
2

I'm not sure if this is significant, but in my internationalized application I have this:

LANGUAGES = (
    ("nl", "Nederlands"),
    ("en", "English"),
    )   

i.e., without the _() around the strings. Your code fragment doesn't show how you define _, the documentation says you have to alias it to ugettext_lazy(). Maybe it makes a difference.

What also would make it easier to help you, is if you provide the source code to a minimal Django application which demonstrates your problem (on github, for example).

Michiel Overtoom
  • 1,609
  • 13
  • 14
  • I define _ with this import: from django.utils.translation import ugettext_lazy as _ – user568021 Sep 15 '15 at 14:10
  • In that case it's according to the docs. I'm inclined to look into your problem in detail, even debug it manually, but it would help greatly if you could provide some minimal Django application which demonstrates your problem. Is that possible? – Michiel Overtoom Sep 15 '15 at 17:02