開發附加元件¶
附加元件 可用來自訂 Weblate 中的在地化工作流程。
- class weblate.addons.base.BaseAddon(storage)¶
Base class for Weblate add-ons.
- classmethod can_install(*, component=None, category=None, project=None) bool¶
Check whether add-on is compatible with given component.
- change_event(change, activity_log_id: int | None = None)¶
Event handler for change event.
- check_change_action(change) bool¶
Early filtering of Change actions before triggering change_event callback.
- component_update(component, activity_log_id: int | None = None)¶
Event handler for component update.
- configure(configuration) None¶
Save configuration.
- daily(component=None, category=None, project=None, activity_log_id: int | None = None)¶
Scope-aware daily entry point.
Override this for project-level logic, or override daily_component() for per-component logic.
- daily_component(component, activity_log_id: int | None = None)¶
Per-component daily processing. Override this for component-level logic.
- classmethod get_add_form(user, *, component=None, category=None, project=None, **kwargs)¶
Return configuration form for adding new add-on.
- classmethod get_api_urls()¶
Return named Django URL patterns for this provider's API.
- get_change_details(compared_configuration)¶
Return a public configuration snapshot and changed field names.
- get_execution_form(data: Mapping[str, object] | None = None) Form | None¶
Provide optional component/change context and preview controls.
- get_integration_instructions(user, request: HttpRequest | None = None) str¶
Render integration instructions independently of the settings form.
- get_public_configuration()¶
Return configuration with non-public values redacted.
- classmethod get_public_configuration_fields() frozenset[str]¶
Return configuration fields which are safe for public use.
- get_settings_form(user, **kwargs)¶
Return configuration form for this add-on.
- manual(component=None, category=None, project=None, activity_log_id: int | None = None)¶
Scope-aware manual entry point.
By default this mirrors the daily handler and lets add-ons opt in explicitly by subscribing to the manual event.
- manual_component(component, activity_log_id: int | None = None)¶
Per-component manual processing.
- post_add(translation, activity_log_id: int | None = None)¶
Event handler after new translation is added.
- post_commit(component, store_hash: bool, activity_log_id: int | None = None)¶
Event handler after changes are committed to the repository.
- post_install(component, store_hash: bool, activity_log_id: int | None = None)¶
Event handler after add-on is installed.
- post_push(component, activity_log_id: int | None = None)¶
Event handler after repository is pushed upstream.
- post_remove(translation, activity_log_id: int | None = None)¶
Event handler after a translation is removed.
- post_update(component, previous_head: str, skip_push: bool, changed_files: list[str], parse_after_update: bool = False, activity_log_id: int | None = None)¶
Event handler after repository is updated from upstream.
- 參數:
previous_head (str) -- HEAD of the repository prior to update, can be blank on initial clone.
skip_push (bool) -- Whether the add-on operation should skip pushing changes upstream. Usually you can pass this to underlying methods as
commit_and_pushorcommit_pending.changed_files (list[str]) -- Files changed by the repository update.
- pre_commit(translation, author: str, store_hash: bool, activity_log_id: int | None = None)¶
Event handler before changes are committed to the repository.
- pre_push(component, activity_log_id: int | None = None)¶
Event handler before repository is pushed upstream.
- pre_update(component, activity_log_id: int | None = None)¶
Event handler before repository is updated from upstream.
- preview(workflow: object, component_id: int | None, change_id: int | None = None, actor=None) dict[str, object]¶
Preview an operation without changing application state, if supported.
- resolve_components(*, component=None, category=None, project=None)¶
Resolve scope to components iterator.
- save_state() None¶
Save add-on state information.
- schedule_manual_run(user_id: int | None = None, *, component_id: int | None = None, change_id: int | None = None) None¶
Queue the default manual handler; providers can consume run context.
- unit_pre_create(unit, activity_log_id: int | None = None)¶
Event handler before new unit is created.
- update_component_state(component, updater: Callable[[dict[str, object]], None]) None¶
Atomically merge component-scoped add-on state into the shared JSON field.
- user()¶
Weblate user used to track changes by this add-on.
附加元件掛勾從 weblate.*.models 模組接收 ORM 物件,包括 Addon, Component, Translation, Category, Project, Unit, Change 和 User 。附加元件設定表單應把 weblate.addons.forms.BaseAddonForm 作為子類。
覆寫 get_integration_instructions(user),即可在設定表單之外獨立轉譯整合說明。請傳回以 Django 範本轉譯的 HTML,其中的文字須經過翻譯,傳入範本的值須經過跳脫處理;若沒有說明,則傳回空字串。user 引數代表正在檢視頁面的管理員,可用來檢查連結與動作的權限。
說明會顯示在安裝與設定表單上方的獨立面板。若說明需要附加元件已安裝,請在尚未設定 self.instance.pk 時傳回空字串。只提供說明的附加元件可不設定 settings_form。設定 stay_on_create = True,即可在安裝完成後,將管理員重新導向至已安裝的附加元件及其說明。
在請求之外儲存設定時,請使用 記錄操作者。BaseAddon.create(..., acting_user=user) 會將完整安裝操作納入操作者脈絡。create_object() 會傳回尚未儲存的物件,且不再接受 acting_user;請改為將後續儲存操作納入該脈絡。掛鉤執行時使用附加元件機器人,安裝與設定則保留最初發起操作的人員。
這裡是一個範例附加元件:
# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import annotations
from typing import TYPE_CHECKING, ClassVar
from django.utils.translation import gettext_lazy
from weblate.addons.base import BaseAddon
from weblate.addons.events import AddonEvent
if TYPE_CHECKING:
from weblate.addons.base import CompatDict
from weblate.trans.models import Translation
class ExampleAddon(BaseAddon):
# Filter for compatible components, every key is
# matched against property of component
compat: ClassVar[CompatDict] = {
"file_format": {"po", "po-mono"},
}
# List of events add-on should receive
events: ClassVar[set[AddonEvent]] = {
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: Translation | None,
author: str,
store_hash: bool,
activity_log_id: int | None = None,
) -> None:
return
自動化作業文件¶
自動化參考資料 以 weblate/automation/schema.py 中的結構描述作為參考來源。內建動作會在 weblate/automation/operations.py 註冊,並由此提供設定和結果結構描述、驗證及執行邏輯。此註冊表僅供內部使用,不會載入第三方動作。新增動作時,請設定其標題,以及首次加入 Weblate 的版本。日後擴充動作時,請保留該版本。控制區塊會在工作流程結構描述中保留 title 和 x-version-added 中繼資料。
請在註冊表項目中宣告動作支援的字串範圍。預設為 component;如果執行時可接受單位選擇,則可加入 trigger 和 result。請將範圍傳給正規化處理,讓查詢預設值和必要條件符合所選範圍。請透過執行階段收集器回報受影響的單位 ID,不要將它們放進公開結果結構描述或活動記錄。
執行 make -C docs update-automation-schema update-automation-docs,更新可下載的結構描述和參考文件。在 docs/admin/automation.rst 中對應的 AUTOGENERATED END 標記後方新增說明和範例。list_automation 產生器會保留這些手動新增內容。請維持章節識別碼穩定,讓說明持續對應到各項作業。
為附加元件設定加上型別¶
附加元件設定儲存在 Addon.configuration JSON 欄位中,因此模型會以原始 JSON 保留持久化資料。附加元件實作可透過參數化 BaseAddon 與 BaseAddonForm,為自己的設定指定型別。
當儲存的 JSON 與執行階段結構可能不同時,請使用兩個 TypedDict 類別:一個寬鬆且通常設為 total=False 的儲存設定,用於舊值或缺少的值;另一個則是由 normalize_configuration() 傳回的完整執行階段設定。附加元件的執行階段程式碼應讀取 self.configuration 或 self.get_configuration(),以取得正規化的預設值,而非原始持久化 JSON。
對於儲存與執行階段結構相同的簡單附加元件,請定義單一 TypedDict,並將它用於兩個 BaseAddon 型別參數。表單的 serialize_form() 傳回型別應與儲存設定型別一致。
公開附加元件設定¶
沒有附加元件管理權限,也可能看得到附加元件的變更歷程。請在表單的 public_configuration_fields 屬性中列出可安全公開的設定欄位。未明確列出的欄位仍會保留在快照中,但值為 null,並標示為已遮蔽。預設為空集合,以免意外公開新加入的設定。將結構化欄位加入允許清單,會公開其中所有巢狀值;只有在該欄位可能包含的每個值都可安全公開時,才應將它列入。
每當設定會在受信任的附加元件管理程式碼之外公開時,請使用 BaseAddon.get_public_configuration()。若內部作業有意複製運作中的附加元件,仍可繼續使用已儲存的設定。
組件掛載式附加元件 API¶
請宣告 api_name,並從 get_api_urls() 傳回具名的 Django URL 模式。使用一般 Django 轉換器和 DRF 檢視。無論附加元件是否已安裝到組件,每個在 WEBLATE_ADDONS 中啟用的提供者都會註冊這些模式。
路由會掛載於 /api/components/<project>/<component>/addons/<api_name>/。對於分類組件,請在組件路徑區段中編碼完整的分類和組件路徑,方式與組件 REST API 相同。請使用安裝項目的 api_url,不要自行組合此 URL。API 名稱必須包含 1 到 64 個 ASCII 字母、數字、底線或連字號,並且在已啟用的提供者類別中全域唯一。Weblate 會在啟動時驗證這些宣告,不會查詢資料庫,並拒絕無效或衝突的名稱。提供者必須宣告 needs_component = True、repo_scope = False 和 multiple = False。同一提供者可安裝在多個組件中,但每個組件只能安裝一次。
請繼承 weblate.addons.api.InstalledAddonAPIView,並將其 addon_name 設為提供者的內部附加元件名稱。基礎檢視會驗證要求、檢查組件存取權,並將安裝項目設為 self.addon。如果安裝項目不存在或不相容,會回傳 HTTP 404。預設的 permission 為 component.edit,並會在剖析要求資料前檢查。預設 JSON 剖析器會將要求大小限制在 5 MiB,即使要求沒有 Content-Length 標頭也一樣。
請在檢視方法中使用一般 DRF 序列化器驗證。使用 drf_spectacular.utils.extend_schema 和序列化器欄位說明文字,記錄完整的合約:OpenAPI 會探索與執行階段路由相同的檢視。使用 api:<api_name>:<pattern_name> 反向解析端點,並提供 project__slug、slug 及任何端點參數。API 名稱屬於公開合約,實作變更時應維持穩定。變更提供者註冊後請重新啟動 Weblate;覆寫註冊的測試必須重建 URL 設定並清除 Django URL 快取。
現有的 /api/addons/<id>/ 管理 API 仍可使用。唯讀欄位 api_name 和 api_url 會反映目前啟用之提供者的宣告。若提供者已停用或不相容,這兩個欄位都會是 null。
Kotlin SDK 資源測試¶
請在已設定 測試環境 的情況下執行 uv run pytest weblate/kotlin_sdk/tests.py,測試資源擷取、準備和發布功能,包括 Kotlin SDK API。
二進位寫入器測試、AAPT2 驗證和 Android 儀器測試由 arsc-writer 維護。操作說明請參閱其 開發指南。