[英]Django - custom translation (.mo file) strings doesn't work
I tried to set up translation for Django. 我试图为Django设置翻译。 Built-in Django strings are correctly translated (eg, in
/admin/
), so language selection works, but I can't get it to use the custom compiled .mo file. 内置Django字符串已正确翻译(例如,在
/admin/
),因此可以进行语言选择,但是我无法使用自定义的已编译.mo文件。
I went step by step according this answer , and some others, but without success - it looks like I have everything like in that one. 我按照这个答案以及其他一些答案逐步进行,但没有成功-看来我拥有该答案中的所有内容。
To points out things I checked up: 指出我检查过的事情:
LocaleMiddleware
is before CommonMiddleware
and after SessionMiddleware
LocaleMiddleware
在CommonMiddleware
之前和SessionMiddleware
之后 settings.py: settings.py:
LOCALE_PATHS = (
os.path.join(BASE_DIR,'locale'),
)
LANGUAGES = (
('en', _('English')),
('cz', _('Czech')),
)
...
MIDDLEWARE_CLASSES = [
'thermostat.middleware.thermostatMiddleware.ForceDefaultLanguageMiddleware',
'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.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.i18n',
],
},
},
]
...
LANGUAGE_CODE = 'cz'
USE_I18N = True
USE_L10N = True
USE_TZ = True
...
(The ForceDefaultLanguageMiddleware
only removes HTTP_ACCEPT_LANGUAGE
header from request.META
.) (
ForceDefaultLanguageMiddleware
仅从request.META
删除HTTP_ACCEPT_LANGUAGE
标头。)
In code, I'm using ugettext_lazy, and the .po file is generated as expected with django-admin makemessages -a
. 在代码中,我使用的是ugettext_lazy,并且按预期方式使用
django-admin makemessages -a
生成了.po文件。 I translate it, run django-admin compilemessages
, which prints processing file django.po in /[path to project]/locale/cz/LC_MESSAGES
- the path is correct, the .po file really exists there. 我翻译它,运行
django-admin compilemessages
,它processing file django.po in /[path to project]/locale/cz/LC_MESSAGES
中打印processing file django.po in /[path to project]/locale/cz/LC_MESSAGES
路径正确,.po文件确实存在。
For example, I marked a model for translation like this: 例如,我标记了这样的翻译模型:
class Probe(models.Model):
class Meta:
verbose_name = _('Probe')
verbose_name_plural = _('Probes')
name = models.CharField(_('Name'), max_length=200)
And this is where I ends - no matter what I tried, Django translates only is built-in messages and ignores custom strings. 这就是我的终点-不管我尝试什么,Django仅翻译内置消息并忽略自定义字符串。 So in administration, I have translated all the buttons, but not the field and model names.
因此,在管理中,我翻译了所有按钮,但没有翻译字段和模型名称。 Any ideas what I do wrong?
有什么想法我做错了吗?
OK, so as I was just about to send the question, I found the answer. 好的,正当我要发送问题时,我找到了答案。
I was using LANGUAGE_CODE = 'cz'
, and Django took it as an alias to 'cs'
(this is ok), but for some reason, it didn't load the custom .mo file. 我使用的是
LANGUAGE_CODE = 'cz'
,而Django将其作为'cs'
的别名(没关系),但是由于某些原因,它没有加载自定义.mo文件。 When I changed the language code to cs
as it is in django/conf/locale/
path, it began to work. 当我将语言代码更改为
django/conf/locale/
路径中的cs
,它开始起作用。
I do not understand why this happens, but at least it works. 我不明白为什么会这样,但至少可以奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.