客製 Weblate

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

提示

You can also customize Weblate look in 外觀自訂.

警告

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.

提示

When using Docker, you can place Python modules in /app/data/python/ (see Docker 容器 volumes), so they can be loaded by Weblate, for example from a settings override file.

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

  1. 為您的套件建立資料夾(我們會使用 weblate_customization )。

  2. Within it, create a pyproject.toml file to describe the package:

    [build-system]
    requires = ["uv_build>=0.8.18,<0.9.0"]
    build-backend = "uv_build"
    
    [project]
    name = "weblate-customization"
    version = "0.1.0"
    description = "Add your description here"
    requires-python = ">=3.13"
    dependencies = []
    
  3. 為 Python 模組建立資料夾:src/weblate_customization

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

  5. This package can now be installed using uv pip install -e. More info to be found in Editable packages documentation.

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

您的套件結構應該看起來像這樣:

weblate_customization
├── pyproject.toml
└── src
    └── weblate_customization
        ├── __init__.py
        ├── addons.py
        └── checks.py

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

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

To install your code for 自訂自動修正, 撰寫自訂檢查, 自訂機器翻譯 or 編寫附加元件 in Weblate:

  1. Place the files into your Python module containing the Weblate customization (see 建立 Python 模組 or 自訂程式碼).

  2. Add its fully-qualified path to the Python class in the dedicated settings:

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

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

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

# Automatic suggestions
WEBLATE_MACHINERY += ("weblate_customization.machinery.SampleTranslation",)