客製 Weblate#

使用 Django 和 Python 擴展與客製。將您的更改貢獻給上游,使每人都能夠受益。這降低了您的維護成本; Weblate 中的代碼對更改內部界面或重構編碼時的情況。

提示

You can also customize Weblate look in Appearance customization.

警告

Neither internal interfaces nor templates are considered a stable API. Please review your customizations for every upgrade, the interfaces or their semantics might change without notice.

建立 Python 模組#

如果不熟悉 Python,您可以查看 Python For Beginners,它解釋了其基本內容並指向了高級教程。

To write a file with custom Python code (called a module), a place to store it is needed, either in the system path (usually something like /usr/lib/python3.12/site-packages/) or in the Weblate directory, which is also added to the interpreter search path.

提示

使用 Docker時,您可以將 Python 模組放置在 /app/data/python/ 中(請參見 Docker 容器 volumes),這樣一來它們可以由 Weblate 載入,舉例來說來自 設定覆寫檔

更好地是,將您的客製化轉變為適當的 Python 包:

  1. 為您的包建立文件夾(我們會使用 weblate_customization )。

  2. 在裡面新建`setup.py` 文件來描述包:

    from setuptools import setup
    
    setup(
        name="weblate_customization",
        version="0.0.1",
        author="Your name",
        author_email="yourname@example.com",
        description="Sample Custom check for Weblate.",
        license="GPLv3+",
        keywords="Weblate check example",
        packages=["weblate_customization"],
    )
    
  3. 建立客製代碼的 Python 模組( 也被成為 weblate_customization )的資料夾。

  4. 在裡面建立 __init__.py 檔案來確認 Python 可以匯入模組。

  5. 現在可以使用 pip install -e 安裝這個包。更多資訊可以在 Editable installs 中找到。

  6. 模組一旦安裝,就可以用在 Weblate 組態中(例如 weblate_customization.checks.FooCheck )。

您的軟體包結構應該看起來像這樣:

weblate_customization
├── setup.py
└── weblate_customization
    ├── __init__.py
    ├── addons.py
    └── checks.py

可以在 <https://github.com/WeblateOrg/customize-example> 找到客製 Weblate 的例子,它涵蓋了下面描述的所有題目。

自訂的品質檢查、附加元件和自動修復#

要在 Weblate 中安裝您的 自訂自動修正撰寫自定義查核Writing add-on 代碼:

  1. 將檔案放置到您的包含 Weblate 客製的 Python 模組中(請參見 建立 Python 模組 )。

  2. 在專用設定( WEBLATE_ADDONSCHECK_LISTAUTOFIX_LIST )中將其完全合法的路徑新增到 Python 類中:

# Checks
CHECK_LIST += ("weblate_customization.checks.FooCheck",)

# Autofixes
AUTOFIX_LIST += ("weblate_customization.autofix.FooFixer",)

# Add-ons
WEBLATE_ADDONS += ("weblate_customization.addons.ExamplePreAddon",)