Django i18n: Common causes for translations not appearing

前端 未结 9 2106
心在旅途
心在旅途 2020-12-30 23:16

I am making a multilingual Django website. I created a messages file, populated and compiled it. I checked the site (the admin in this case,) in my wanted language (Hebrew)

9条回答
  •  天涯浪人
    2020-12-30 23:45

    I'm trying to provide a complete check list:

    • In settings.py, are USE_I18N, USE_L10N, LANGUAGE_CODE, and LOCALE_PATHS set properly?

      • See this list for all allowed values of language identifiers. Note that Simplified Chinese is specified by zh-hans, not zh-cn.
    • In settings.py, is django.middleware.locale.LocaleMiddleware included in MIDDLEWARE in correct order?

    • Have you (re)run django-admin makemessages -l with the correct local name, from the correct place?

      • You can see an incomplete list of allowed locale name by running ls your/path/to/python/site-packages/django/conf/locale/ on your machine, or by taking a look at the source code
      • Note that you have to use _ rather than - here. For example, to specify simplified Chinese, execute django-admin makemessages -l zh_Hans, not zh_CN or zh_hans or zh-Hans or anything else.
    • Have you removed all fuzzy tags in your PO file(s)?

    • Have you (re)compiled the OP file(s) with django-admin compilemessages?

    • Have you restarted the web server?

    Additional notes:

    • If some of your translation is overridden by Django's default translation, use contextual markers to bypass it. For example,

    models.py

    first_name = models.CharField(
        pgettext_lazy('override default', 'first name'),
        max_length=30
    )
    
    last_name = models.CharField(
        pgettext_lazy('override default', 'last name'),
        max_length=150
    )
    

    django.po

    #: models.py:51
    msgctxt "override default"
    msgid "first name"
    msgstr "姓"
    
    #: models.py:55
    msgctxt "override default"
    msgid "last name"
    msgstr "名"
    

    and you'll see , instead of the default 姓氏, 名字.

提交回复
热议问题