开发附加组件
附加组件 是在 Weblate 中自定义本地化工作流的方法。
- class weblate.addons.base.BaseAddon(storage=None)
Weblate 附加组件的基础类。
- classmethod can_install(component, user)
检查附加组件是否与给定部件兼容。
- configure(settings)
保存配置。
- daily(component)
每日触发钩子。
- classmethod get_add_form(user, component, **kwargs)
返回用于添加新附加组件的配置表单。
- get_settings_form(user, **kwargs)
返回此附加组件的配置表单。
- post_add(translation)
添加新的翻译后触发钩子。
- post_commit(component)
更改提交到仓库后触发钩子。
- post_push(component)
仓库推送到上游之后触发钩子。
- post_update(component, previous_head: str, skip_push: bool)
在仓库从上游更新之后触发钩子。
- 参数
previous_head (str) – 仓库更新前的 HEAD,在初始克隆时可以是空白的。
skip_push (bool) – 附加组件操作是否应该跳过将更改推送到上游。通常可以将这个作为``commit_and_push``或
commit_pending
传递到底层方法。
- pre_commit(translation, author)
更改被提交到仓库前触发钩子。
- pre_push(component)
在仓库推送上游之前触发钩子。
- pre_update(component)
在仓库从上游更新之前触发钩子。
- save_state()
保存附加组件状态信息。
- store_post_load(translation, store)
文件解析后触发钩子。
它接收文件格式类的事件作为参数。
这对修复该文件格式类参数有用,例如,调整文件如何存储。
- unit_pre_create(unit)
创建新的单元前触发的钩子。
这里是一个附加组件的示例:
#
# 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