简体   繁体   中英

Django translation not working in my case

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.

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. 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.

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

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

ie, 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM