Developing add-ons

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

class weblate.addons.base.BaseAddon(storage=None)

Weblate 附加組件的基礎類。

classmethod can_install(component, user)

Check whether add-on is compatible with given component.

configure(settings)

Save configuration.

daily(component)

每日觸發鉤子。

classmethod get_add_form(user, component, **kwargs)

Return configuration form for adding new add-on.

get_settings_form(user, **kwargs)

Return configuration form for this add-on.

post_add(translation)

Hook triggered after new translation is added.

post_commit(component)

Hook triggered after changes are committed to the repository.

post_push(component)

Hook triggered after repository is pushed upstream.

post_update(component, previous_head: str, skip_push: bool)

在倉庫從上游更新之後觸發鉤子。

參數
  • previous_head (str) – 倉庫更新前的 HEAD,在初始克隆時可以是空白的。

  • skip_push (bool) – Whether the add-on operation should skip pushing changes upstream. Usually you can pass this to underlying methods as commit_and_push or commit_pending.

pre_commit(translation, author)

Hook triggered before changes are committed to the repository.

pre_push(component)

在倉庫推送上游之前觸發鉤子。

pre_update(component)

在倉庫從上游更新之前觸發鉤子。

save_state()

Save add-on state information.

store_post_load(translation, store)

Hook triggered after a file is parsed.

它接收文件格式類的事件作為參數。

這對修復該文件格式類參數有用,例如,調整文件如何存儲。

unit_pre_create(unit)

創建新的單元前觸發的鉤子。

Here is an example add-on:

#
# Copyright © 2012–2022 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#


from django.utils.translation import gettext_lazy as _

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


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 = (EVENT_PRE_COMMIT,)
    # Add-on unique identifier
    name = "weblate.example.example"
    # Verbose name shown in the user interface
    verbose = _("Example add-on")
    # Detailed add-on description
    description = _("This add-on does nothing it is just an example.")

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