2👍
✅
I have a similar working setup, the main difference seems to be that I’m using ugettext_lazy
. That’s because I need to translate these strings in my models or settings when they were accessed, rather than when they were called (which would happen only once: they would only be evaluated on server startup and would not recognize any further changes; e.g. switching the Django admin language).
Reference: https://docs.djangoproject.com/en/1.10/topics/i18n/translation/#lazy-translation
That’s what I use (in this special case, german is the default language and I’m translating into english):
project/urls.py
from django.conf.urls.i18n import i18n_patterns
urlpatterns = i18n_patterns(
url(r'^admin/', admin.site.urls),
)
project/settings.py
from django.utils.translation import ugettext_lazy as _
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
LANGUAGE_CODE = 'de-de'
USE_I18N = True
USE_L10N = True
LANGUAGES = [
('de', _('German')),
('en', _('English')),
]
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale'),
]
app/models.py
from django.utils.translation import ugettext_lazy as _
class Kindergarten(models.Model):
stadt = models.CharField(verbose_name=_(Stadt))
class Meta:
verbose_name = _('Kindergarten')
verbose_name_plural = _('Kindergärten')
Workflow
$ python manage.py makemessages --locale en
... edit project/locale/en/LC_MESSAGES/django.po ...
$ python manage.py compilemessages
Now I can access my translate Django admin (interface + models) via:
Notes
- Pyhton 3.5.2
- Django 1.10.2
Source:stackexchange.com