定制 Weblate

使用 Django 和 Python 进行扩展和定制。将你的更改贡献给上游,让每个人都能受益。这降低了您的维护成本;当改变内部接口或重构代码时,Weblate 的代码会被照顾到。

提示

你也可以在 外观自定义 中定制 Weblate 外观。

警告

内部接口与模板都不被认为是稳定的 API。请每次升级都检查你的定制,接口或其语义可能未经通知就进行更改。

建立 Python 模块

如果不熟悉 Python,你可以查看 适合初学者的 Python(英文),它解释了其基本内容并指向了高级教程。

要写入有定制 Python 代码(被称为模块)的文件,需要一个地方存储它,或者在系统路径(通常像 /usr/lib/python3.12/site-packages/ 的地方),或者在 Weblate 目录下,同样也添加到解释程序搜索路径下。

提示

使用 Docker 时,你可将 Python 模块置于 /app/data/python/ (见 Docker 容器卷) 以便它们可以由 Weblate 从 设置覆盖文件 等处进行加载。

更好地是,将你的定制化转变为适当的 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. Create a folder for the Python module: 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 的示例,它涵盖了下面描述的所有题目。

Custom quality checks, add-ons, automatic suggestions and auto-fixes

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",)