1

I try to translate my django site to another languages but translation in python doesn't work. But translation in templates using trans tag, works as expected.

I have tried ugettext, gettext, gettext_lazy and ugettext_lazy, and every time I got original untranslated strings.

My sources all in utf-8 encoding, original strings in Ukrainian language

Paco
  • 4,520
  • 3
  • 29
  • 53
patriotyk
  • 497
  • 3
  • 13
  • Did you change the language setting in the `settings.py` file to specify your language? – Paco Mar 24 '15 at 10:10
  • Yes, switching language works well. Translation in templates works well, and I got translated strings, it doesn't work only in python files. I set USE_I18N = True, middleware, list of languages, active language, and path to translations in settings file. – patriotyk Mar 24 '15 at 10:19
  • What do you mean, "it doesn't work only in python files"? You get english strings? Is it your own code that isn't translated? If so, you'll need to create translation files: https://docs.djangoproject.com/en/1.7/topics/i18n/translation/#localization-how-to-create-language-files – Paco Mar 24 '15 at 10:25
  • 1
    "it doesn't work only in python files" means that ugettext, gettext, gettext_lazy and ugettext_lazy instead of translated strings returns original strings. Of course I did translation file and compiled it, why do you thing I didn't, when I wrote that everything works with "trans" template tag? – patriotyk Mar 24 '15 at 10:50
  • Can you post the code? Surely there is something you're missing. – brunofitas Mar 24 '15 at 14:49

3 Answers3

0

You want to check this

Since you didn't post your code, I assume it has something to do with the alias.

Use

from django.utils.translation import ugettext_lazy as _

translated_string = _('original_string')

And you should not have a problem

"You can only use the _ alias for ugettext and ugettext_lazy (or any other related translation function) in Django or else it won't be recognized by makemessages command. The technical explanation can be found in Robert Lujo's answer."

Community
  • 1
  • 1
brunofitas
  • 2,983
  • 1
  • 20
  • 26
  • I did it as you wrote. And in my case the translated_string will always 'original_string'. Later I will post the code – patriotyk Mar 24 '15 at 18:03
0

I think the problem lies in your MIDDLEWARE_CLASSES. The thing is, there are some middlewares that might change your request, including a language prefix. Especially, when you use AJAX calls for querying extra template data, translated by ugettext, gettext, etc.

potar
  • 468
  • 6
  • 6
  • No, I have resolved this issue, but didn't update this thread. I did translation from Ukrainian language, so original strings must be Unicode objects. Otherwise ugettext_lazy will not work if string contains non Latin symbols. – patriotyk Oct 23 '15 at 10:47
0

The ugettext_lazy will not work if string contains non Latin symbols. So in my case the original strings must be the Unicode objects.

patriotyk
  • 497
  • 3
  • 13