开发附加组件#

附加组件 是在 Weblate 中自定义本地化工作流的方法。

class weblate.addons.base.BaseAddon(storage: Addon)#

Weblate 附加组件的基础类。

classmethod can_install(component: Component, user: User | None)#

检查附加组件是否与给定部件兼容。

component_update(component: Component) None#

Event handler for component update.

configure(configuration) None#

保存配置。

daily(component: Component) None#

Event handler daily.

classmethod get_add_form(user: User | None, component: Component, **kwargs)#

返回用于添加新附加组件的配置表单。

get_settings_form(user: User | None, **kwargs)#

返回此附加组件的配置表单。

post_add(translation: Translation) None#

Event handler after new translation is added.

post_commit(component: Component) None#

Event handler after changes are committed to the repository.

post_push(component: Component) None#

Event handler after repository is pushed upstream.

post_update(component: Component, previous_head: str, skip_push: bool, child: bool) None#

Event handler after repository is updated from upstream.

参数:
  • previous_head (str) – 仓库更新前的 HEAD,在初始克隆时可以是空白的。

  • skip_push (bool) – 附加组件操作是否应该跳过将更改推送到上游。通常可以将这个作为``commit_and_push``或 commit_pending 传递到底层方法。

pre_commit(translation: Translation, author: User) None#

Event handler before changes are committed to the repository.

pre_push(component: Component) None#

Event handler before repository is pushed upstream.

pre_update(component: Component) None#

Event handler before repository is updated from upstream.

save_state() None#

保存附加组件状态信息。

store_post_load(translation: Unit, store: TranslationFormat) None#

Event handler after a file is parsed.

它接收文件格式类的事件作为变量。

这对修复该文件格式类参数有用,例如,调整文件如何存储。

unit_pre_create(unit: Unit) None#

Event handler before new unit is created.

user()#

被此附加组件用来追踪变化的 Weblate 用户。

class weblate.addons.base.Addon#

ORM object for an add-on.

class weblate.addons.base.Component#

ORM object for a component.

class weblate.addons.base.Translation#

ORM object for a translation.

class weblate.addons.base.Project#

ORM object for a project.

class weblate.addons.base.Unit#

ORM object for an unit.

class weblate.addons.base.User#

ORM object for an user.

class weblate.addons.base.TranslationFormat#

Translation file wrapper.

这里是一个附加组件的示例:

# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later

from django.utils.translation import gettext_lazy

from weblate.addons.base import BaseAddon
from weblate.addons.events import AddonEvent


class ExampleAddon(BaseAddon):
    # Filter for compatible components, every key is
    # matched against property of component
    compat = {"file_format": {"po", "po-mono"}}
    # List of events add-on should receive
    events = (AddonEvent.EVENT_PRE_COMMIT,)
    # Add-on unique identifier
    name = "weblate.example.example"
    # Verbose name shown in the user interface
    verbose = gettext_lazy("Example add-on")
    # Detailed add-on description
    description = gettext_lazy("This add-on does nothing it is just an example.")

    # Callback to implement custom behavior
    def pre_commit(self, translation, author) -> None:
        return