Weblate 内部

备注

这一章将给出 Weblate 内部的基本概况。

Weblate 从 Django 得到其多数代码架构,并基于它。

目录结构

Weblate 主仓库目录结构的速览:

docs

本文档的源码,可使用 Sphinx 来构建。

dev-docker

运行开发服务器的 Docker 代码,请参见 在 Docker 中本地运行 Weblate。

weblate

Weblate 作为 Django 应用程序的源代码,请参见 Weblate 内部。

weblate/static

客户端文件(CSS、Javascript 和图片),请参见 Weblate 前端。

模块

Weblate 包括几个 Django 应用(一些是可选的,请参见 可选的 Weblate 模块):

accounts

用户账户、简介和通知。

addons

微调 Weblate 行为的附加组件,请参见 附加组件。

api

基于 Django REST 框架 的 API。

auth

认证和权限。

billing

可选的 账单 模块。

checks

翻译字符串 质量检查 模块。

fonts

字体渲染检查模块。

formats

基于 translate-toolkit 的文件格式抽象层。

gitexport

可选的 Git 导出器 模块。

lang

定义语言和复数模型的模块。

legal

可选的 法律组件 模块。

machinery

机器翻译服务的集成。

memory

内置的翻译记忆库,请参见 翻译记忆库。

screenshots

屏幕截图管理与 OCR 模块。

trans

处理翻译的主模块。

utils

各种帮助功能。

vcs

版本控制系统抽象概念。

wladmin

Django 管理界面定制。

翻译原文

A unit has a canonical file source and may have a translation from another language as its effective source. Keep Unit.source and Unit.source_unit canonical: repository synchronization and file formats depend on this. Use effective_source and its language and plural accessors for content shown to translators. Do not replace source on a copied unit to simulate an effective source; other fields and related objects still describe the canonical source.

Use Unit.source_snapshot when an operation needs to preserve the source text, language, plural rules, and terminology metadata together. Its identity defines source changes for dependency reconciliation, editor conflict detection, and AI diagnostics. For historical rendering and reports, read the snapshot recorded on the change, rather than reconstructing it from the current unit.

A missing configured parent remains a custom source: its text is empty, its language is retained, and its child is blocked. effective_source_unit is then None. Only removing the workflow or selecting the canonical language restores the canonical source.

Text-only accessors do not load language or plural metadata. For batch consumers that need the complete effective source, use UnitQuerySet.prefetch_translation_parent() alongside their existing unit prefetches. prefetch_source() already includes this. Ordinary searches keep the canonical query path and ordinary units retain their existing content hashes.

Source-dependent writes enter source_operation() before locking unit rows. The outer operation takes a shared project gate and reads its workflow mapping in one database round trip, including for projects without custom workflows. Workflow changes take source_project_gate(..., exclusive=True) before validation or persistence. Configuration changes affecting several projects acquire their gates in ascending project ID order. Never upgrade a shared gate to an exclusive one inside an operation.

The database gate function locks first and reads workflows in a separate statement, so a waiter sees configuration committed while it waited. Write paths use this transaction-scoped view rather than cached model settings. Nested operations reuse it; rolled-back savepoints discard their dependency work. Queued dependencies are reconciled before commit and external memory updates are scheduled after commit. Source-change history is written in bulk, preserving individual records while batching their notification dispatch.

Acquire any required repository locks before project gates, then component and unit row locks. Reconciliation itself only changes database state and does not acquire repository locks. Apply this boundary to bulk imports, automatic translation, file-save failures, deletion, and restoration as well as editor writes. Language and plural-rule changes require exclusive gates before their updates. Historical migrations use historical models without these runtime helpers.

Machinery source selection happens before mapping language codes to a provider. Use get_unit_source_language() for translation and uses_custom_source() for workflow-specific glossary handling. Provider codes can merge distinct language variants, so code equality cannot determine whether a workflow source was selected. Explicit component or secondary source selection takes precedence over the workflow source.

后台任务间隔

提示

此部分描述 Celery 任务内部情况。使用 Celery 的后台任务 描述如何配置 Celery 来运行这些任务。

Weblate 使用 Celery 在后台执行任务。一些任务由事件触发,而一些任务由计划触发。

Celery Beat 用于安排任务时间,django-celery-beat 用来在数据库中保存定期的任务计划。在每一个 Django 应用中,任务计划在 tasks.py 中进行配置。

使用数个队列运行任务;在 settings.py 中配置路由。队列被设计为隔开不同种类的工作负担:

celery

处理后台任务的默认队列。

notify

传送 Weblate 内事件及身份认证或注册的通知邮件。这是个单独队列以让邮件顺利传送,即使积压了其他任务。

memory

更新翻译记忆库条目。导入新字符串时更新队列可能较长,漫长的处理在这里不是十分重要,因此单独队列避免拦截其他任务。

backup

备份任务无法并行处理,单一的专门在worker 让备份更轻松。

translate

已知自动翻译任务会花很长时间,因为要访问数个外部服务。