当前位置: 首页 > 工具软件 > Wagtail > 使用案例 >

wagtail 实现中英文

常心水
2023-12-01

1、在settings中设置使用本地自定义的语言

MIDDLEWARE = [
    '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',
    'django.middleware.security.SecurityMiddleware',

    'wagtail.core.middleware.SiteMiddleware',
    'wagtail.contrib.redirects.middleware.RedirectMiddleware',
]

2、模板中使用翻译

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(PROJECT_DIR, 'templates'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.template.context_processors.i18n',  # 添加
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

3、设置本地语言选项及模板翻译目录

from django.utils.translation import gettext_lazy as _

LOCALE_PATHS = (
    os.path.join(BASE_DIR, "locale"),
)

LANGUAGES =  WAGTAILADMIN_PERMITTED_LANGUAGES =[
                               ('en-us', _('English')),
                               ('zh-cn', _('Chinese')),
                            ]

4、model 编码方法

from django.db import models
from wagtail.core.fields import RichTextField
from wagtail.core.models import Page
from django.utils import translation
from wagtail.admin.edit_handlers import FieldPanel
from django.utils.translation import gettext_lazy as _


class TranslatedField:
    def __init__(self, en_field, zh_field):
        self.en_field = en_field
        self.zh_field = zh_field

    def __get__(self, instance, owner):
        if translation.get_language() == 'zh-cn':
            return getattr(instance, self.zh_field)
        else:
            return getattr(instance, self.en_field)
        
class BlogPage(Page):
    title_zh = models.CharField('标题(中文)', max_length=255)
    body_en = RichTextField('content')
    body_zh = RichTextField('详细内容')
    translated_title = TranslatedField(
        'title',
        'title_zh',
    )
    body = TranslatedField(
        'body_en',
        'body_zh',
    )

    content_panels = Page.content_panels + [
        FieldPanel('title_zh'),
        FieldPanel('body_en', classname="full"),
        FieldPanel('body_zh', classname="full"),
    ]

    class Meta:
        verbose_name = '测试'

5、模板使用方法

{% load wagtailcore_tags %}
{% load staticfiles %}
{% load i18n %}
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    {% trans "Simple Blog" as title %}
    <title>{{ title}}</title>
</head>
<body class="customize-support">
    <h1>{{title}}</h1>
    <h2>{{ page.translated_title }}</h2>
    <h3>{{ page.body|richtext }}</h3>
</body>
</html>

6、在模板中写完之后还要编译

python manage.py makemessages -l zh_CN   ----> processing locale zh_CN

手动翻译之后

python manage.py compilemessages  -----> processing file django.po in /path/to/locale/zh_CN/LC_MESSAGES

7、手动翻译部分

#: home/templates/home/blog_page.html:7
msgid "Simple Blog"
msgstr "博客"

#: lvjingiant/settings/base.py:175
msgid "English"
msgstr ""

#: lvjingiant/settings/base.py:176
msgid "Chinese"
msgstr ""

 类似资料: