附加元件

Add-ons provide ways to customize and automate the translation workflow. Admins can add and manage add-ons from the ManageAdd-ons menu of each respective translation project or component. Add-ons can be also installed site-wide in 管理介面.

提示

You can also configure add-ons using API, DEFAULT_ADDONS, or install_addon.

../_images/addons.webp

內建附加元件

自動翻譯

Add-on ID:

weblate.autotranslate.autotranslate

組態:

mode

自動翻譯模式

Available choices:

suggest – Add as suggestion

translate – Add as translation

fuzzy – Add as 「Needing edit」

filter_type

搜尋篩選

請注意:翻譯所有字串會捨棄所有現有翻譯。

Available choices:

all – All strings

nottranslated – Untranslated strings

todo – Unfinished strings

fuzzy – Strings marked for edit

check:inconsistent – 錯誤檢查:不一致

auto_source

自動翻譯的來源

Available choices:

others – Other translation components

mt – Machine translation

component

組件

輸入要用作來源的組件 slug。留空則使用本專案的所有組件。

engines

機器翻譯引擎

threshold

分數閾值

觸發:

更新元件(每日)

使用機器翻譯或其他組件自動翻譯字串。

已觸發的:

  • 當新字串出現在元件中。

  • 每個元件的一個月內,可以使用 background_tasks 進行組態。

JavaScript 在地化 CDN

Added in version 4.2.

Add-on ID:

weblate.cdn.cdnjs

組態:

threshold

最低翻譯數

翻譯收錄的最低門檻。

css_selector

CSS 選擇器

用來偵測可在地化元素的 CSS 選擇器。

cookie_name

語言 Cookie 名稱

儲存語言偏好設定的 Cookie 名稱。

files

從 HTML 檔案擷取字串

在目前儲存庫或遠端網址中,要剖析其可翻譯字串的檔案名稱列表。

觸發:

每日、儲存庫送交後、儲存庫更新後

將翻譯發佈給內容遞交網路,供 JavaScript 或 HTML 在地化處理使用。

可以用於在地化靜態 HTML 網頁,或者在 JavaScript 程式碼中載入在地化文件。

為您的元件產生一個唯一的 URL,您可以將其包含在 HTML 頁面中以在地化它們。請參見 使用 Weblate CDN 翻譯 HTML 和 JavaScript 以了解詳情。

備註

This add-on requires additional configuration on the Weblate server. LOCALIZE_CDN_PATH configures where generated files will be written (on a filesystem), and LOCALIZE_CDN_URL defines where they will be served (URL). Serving of the files is not done by Weblate and has to be set up externally (typically using a CDN service).

This add-on is configured on Hosted Weblate and serves the files via https://weblate-cdn.com/.

移除空白字串

Added in version 4.4.

Add-on ID:

weblate.cleanup.blank

組態:

This add-on has no configuration.

觸發:

儲存庫送交後、儲存庫更新後

從翻譯檔中移除沒有譯文的字串。

使用此方法可以使翻譯檔中不存在任何空字串(例如,如果您的在地化程式庫將它們顯示為缺失,而不是退回到來源字串)。

清理翻譯檔

Add-on ID:

weblate.cleanup.generic

組態:

This add-on has no configuration.

觸發:

儲存庫送交前、儲存庫更新前

更新所有翻譯檔以符合單語言基礎檔。對大多數檔案格式而言,這代表會移除基礎檔中不再存在的失時效翻譯金鑰。

加入遺漏語言

Add-on ID:

weblate.consistency.languages

組態:

This add-on has no configuration.

觸發:

每日、儲存庫新增後

確保對一個專案內所有組件使用一致的一組語言。

每隔24小時,和在 Weblate 中加入新語言時,會檢查一次缺失的語言。

不像其他多數附加元件,這個附加元件影響整個專案。

提示

自動翻譯 自動翻譯新增加的字串。

組件探索

Add-on ID:

weblate.discovery.discovery

組態:

match

用來比對出翻譯檔的正則表達式

file_format

檔案格式

name_template

自訂組件名稱

base_file_template

定義單語的基礎檔名

保留雙語翻譯檔空白。

new_base_template

定義新翻譯的基礎檔

用來建立新翻譯的檔案名稱。例如 gettext,請選擇 .pot 檔。

intermediate_template

中間語言檔案

中間翻譯檔的檔名。大部分情況下,這即是開發者提供的翻譯檔,會在建立實際來源字串時使用。

language_regex

語言篩選

掃描檔案遮罩時用以篩選過濾翻譯檔的正規表示式。

copy_addons

從主元件複製附加元件至新建立的元件

remove

移除檔案不存在的組件

confirm

我能確認上述符合專案看起來正確

觸發:

儲存庫更新後

根據版本控制系統中的檔案更動自動加入或移除組件專案。

該匹配是使用允許複雜組態的正則表達式完成的,但這樣做需要一些知識。一些常見用例的例子可以在附加元件幫助部分找到。

The regular expression to match translation files has to contain two named groups to match component and language. All named groups in the regular expression can be used as variables in the template fields.

You can use Django template markup in all filename fields, for example:

{{ component }}

組件檔名比對

{{ component|title }}

組件檔名首字母大寫

{{ path }}: {{ component }}

Custom match group from the regular expression

一旦點選了 Save,將顯示匹配組件的預覽,可以檢查組態是否匹配於自己的需要:

../_images/addon-discovery.webp

Component discovery examples

One folder per language

各語言各一個資料夾包含組件的翻譯檔。

Regular expression:

(?P<language>[^/.]*)/(?P<component>[^/]*)\.po

Matching files:
  • cs/application.po

  • cs/website.po

  • de/application.po

  • de/website.po

Gettext語言環境佈局

儲存 gettext PO 檔的常用結構。

Regular expression:

locale/(?P<language>[^/.]*)/LC_MESSAGES/(?P<component>[^/]*)\.po

Matching files:
  • locale/cs/LC_MESSAGES/application.po

  • locale/cs/LC_MESSAGES/website.po

  • locale/de/LC_MESSAGES/application.po

  • locale/de/LC_MESSAGES/website.po

Complex filenames

檔名同時使用組件名稱和語言名稱。

Regular expression:

src/locale/(?P<component>[^/]*)\.(?P<language>[^/.]*)\.po

Matching files:
Repeated language code

路徑和檔名中都使用語言名稱。

Regular expression:

locale/(?P<language>[^/.]*)/(?P<component>[^/]*)/(?P=language)\.po

Matching files:
  • locale/cs/application/cs.po

  • locale/cs/website/cs.po

  • locale/de/application/de.po

  • locale/de/website/de.po

Split Android strings

Android 資源字串,切分為多份檔案。

Regular expression:

res/values-(?P<language>[^/.]*)/strings-(?P<component>[^/]*)\.xml

Matching files:
  • res/values-cs/strings-about.xml

  • res/values-cs/strings-help.xml

  • res/values-de/strings-about.xml

  • res/values-de/strings-help.xml

Matching multiple paths

使用 Java properties 翻譯的多模組 Maven 專案管理。

Regular expression:

(?P<originalHierarchy>.+/)(?P<component>[^/]*)/src/main/resources/ApplicationResources_(?P<language>[^/.]*)\.properties

組件名稱:

{{ originalHierarchy }}: {{ component }}

Matching files:
  • parent/module1/submodule/src/main/resources/ApplicationResources_fr.properties

  • parent/module1/submodule/src/main/resources/ApplicationResource_es.properties

  • parent/module2/src/main/resources/ApplicationResource_de.properties

  • parent/module2/src/main/resources/ApplicationResource_ro.properties

提示

元件發現附加用途 內部網址。它是在多個元件之間共享 VCS 設定的便捷方式。連結元件使用透過填寫 webbleate:// project / main-component 中設定的主要元件的本機儲存庫 來源碼儲存庫 欄位(ManageSettingsVersion control system)。這也可以節省時間和系統資源。

大量編輯

Add-on ID:

weblate.flags.bulk

組態:

q

查詢

state

要設定的狀態

Available choices:

-1 – Do not change

10 – Needs editing

20 – Translated

30 – Approved

add_flags

要加入的翻譯旗標

remove_flags

要移除的翻譯旗標

add_labels

要加入的標籤

remove_labels

要移除的標籤

觸發:

組件更新

大量編輯字串的旗標、標籤或狀態。

要自動化標籤操作,從搜尋字串 ``NOT has:label``入手,並新增標籤直到所有字串都有所需的標籤為止。也可以完成 Weblate 元資料的其他自動化操作。

範例:

自動標籤於新字串

搜尋查詢

NOT has:label

要加入的標籤

recent

Marking all App store 中介資料檔案 changelog strings read-only

搜尋查詢

language:en AND key:changelogs/

要加入的翻譯旗標

read-only

將未變動的翻譯標記為「需要編輯」

Add-on ID:

weblate.flags.same_edit

組態:

This add-on has no configuration.

觸發:

元件建立前

每當新的可翻譯字串從 VCS 匯入且符合來源字串時,會在 Weblate 中標記為需要編輯。這對於未翻譯字串有來源字串的檔案格式來說特別有用。

提示

您或許也想要加強 未更動的翻譯 的檢查透過加入 strict-same 標記到 翻譯旗標 中。

也參考

翻譯狀態

標記新來源字串為「需要編輯」

Add-on ID:

weblate.flags.source_edit

組態:

This add-on has no configuration.

觸發:

元件建立前

每當新來源字串由 VCS 匯入時,會在 Weblate 中標記為需要編輯。這樣您可以輕鬆過濾並編輯開發者寫的來源字串。

也參考

翻譯狀態

將新翻譯標記為「需要編輯」

Add-on ID:

weblate.flags.target_edit

組態:

This add-on has no configuration.

觸發:

元件建立前

每當新的可翻譯字串從 VCS 匯入時,會在 Weblate 中標記為需要編輯。這樣您可以輕鬆過濾並編輯開發者建立的翻譯。

也參考

翻譯狀態

用原文填充唯讀字串

Added in version 4.18.

Add-on ID:

weblate.generate.fill_read_only

組態:

This add-on has no configuration.

觸發:

更新元件(每日)

將來源字串作為唯讀字串的翻譯填充。

統計資料產生器

Add-on ID:

weblate.generate.generate

組態:

filename

產生的檔案名稱

template

產生的檔案內容

觸發:

儲存庫送交前

產生內含翻譯狀態詳細資訊的檔案。

您可以使用 Django 範本在檔名或內容中,參閱: 模板標記 取得更多標記描述細節。

例如為每一個翻譯產生簡介檔案:

產生的檔案名稱

locale/{{ language_code }}.json

內容
{
   "language": "{{ language_code }}",
   "strings": "{{ stats.all }}",
   "translated": "{{ stats.translated }}",
   "last_changed": "{{ stats.last_changed }}",
   "last_author": "{{ stats.last_author }}",
}

也參考

模板標記

將原文預先填充進翻譯

Added in version 4.11.

Add-on ID:

weblate.generate.prefill

組態:

This add-on has no configuration.

觸發:

更新元件(每日)

用來源字串填充翻譯字串。

所有在此元件中未翻譯的字串將會使用來源字串取代,並標記為需要編輯。請使用此功能當您不允許有空字串於翻譯檔案。

偽語系產生

Added in version 4.5.

Add-on ID:

weblate.generate.pseudolocale

組態:

source

來源字串

target

目標翻譯

本翻譯中的所有字串都將被覆寫

prefix

固定字串前綴

var_prefix

可變字串前綴

suffix

固定字串後綴

var_suffix

可變字串後綴

var_multiplier

可變部分乘數

重複可變部分的次數多寡,取決於來源字串的長度。

include_readonly

納入唯讀字串

觸發:

更新元件(每日)

藉由在來源字串加前綴與後綴,來自動產生偽語系翻譯。

偽 locale 對於尋找未準備好進行在地化的字串很有用。這是透過修改所有可翻譯的來源字串來實現的,使得在用偽 locale 語言執行應用程式時很容易發現未修改的字串。

發現在地化對應物可能不適合組態的字串也是可能的。

使用變數部分可以查找在在地化之後可能無法適合使用者介面的字串 - 它基於來源字串長度來擴充。變數部分可以藉由重複字數來表達。例如:使用 _ 當作 您好 的後綴字及其數量代表長度可為 您好___________ - 這裡的 _ 重複幾次代表相對於來源字串多少字。

字串將以下述模式產生:

Fixed string prefix Variable string prefix Source string Variable string suffix Fixed string suffix

提示

可以使用真正的語言進行檢測,但在 Weblate 中有專用的假語言環境——en_XAar_XB

提示

您可以使用這個擴充套件來開始翻譯現有或相似的語言。一旦您加入翻譯到這元件,也將會套用擴充套件。例如:您有 fr 且想加入 fr_CA 翻譯,只需要將 fr 設定來源語言,fr_CA 為目標翻譯,並將前字尾留空。

在填滿新翻譯後解除安裝擴充套件以防止 Weblate 更改複製後的翻譯。

評註中的貢獻者紀錄

Add-on ID:

weblate.gettext.authors

組態:

This add-on has no configuration.

觸發:

儲存庫送交前

更新 PO 檔標頭中的評註部分以納入貢獻者姓名及貢獻年份。

PO 檔案頭看上去是這樣的:

# Michal Čihař <michal@weblate.org>, 2012, 2018, 2019, 2020.
# Pavel Borecki <pavel@example.com>, 2018, 2019.
# Filip Hron <filip@example.com>, 2018, 2019.
# anonymous <noreply@weblate.org>, 2019.

更新「configure」檔案中的 ALL_LINGUAS 變數

Add-on ID:

weblate.gettext.configure

組態:

This add-on has no configuration.

觸發:

repository post-add, daily

當新的翻譯新增時,更新 configureconfigure.in 或任何 configure.ac 檔案中的 ALL_LINGUAS 變數。

自訂 gettext 輸出

Add-on ID:

weblate.gettext.customize

組態:

width

長列換列

預設情況下,gettext 會在第 77 個字元和新列字元處換列;加上 –no-wrap 參數後,則僅在新列字元處換列。

Available choices:

77 – Wrap lines at 77 characters and at newlines (xgettext default)

65535 – Only wrap lines at newlines (like 『xgettext –no-wrap』)

-1 – No line wrapping

觸發:

儲存後載入

允許自訂 gettext 輸出行為,例如換列。

提供了後面的選項:

  • 於第 77 個字元處與新列處換列

  • 僅在新列處換列

  • 不要換行

備註

By default gettext wraps lines at 77 characters and at newlines. With the --no-wrap parameter, wrapping is only done at newlines.

更新 LINGUAS 檔案

Add-on ID:

weblate.gettext.linguas

組態:

This add-on has no configuration.

觸發:

repository post-add, daily

當新增翻譯時更新 LINGUAS 檔。

產生 MO 檔案

Add-on ID:

weblate.gettext.mo

組態:

path

產生的 MO 檔路徑

如果未指定,將使用 PO 檔的位置。

fuzzy

包含需要編輯的字串

需要編輯的字串 (fuzzy) 通常無法直接被用為譯文。

觸發:

儲存庫送交前

每次 PO 檔有變動時便自動產生 MO 檔。

產生的 MO 檔案的位置可以客製化,並且其欄位使用 模板標記

備註

If a translation is removed, its PO file will be deleted from the repository, but the MO file generated by this add-on will not. The MO file must be removed from the upstream manually.

更新 PO 檔以符合 POT (msgmerge)

Add-on ID:

weblate.gettext.msgmerge

組態:

previous

保留翻譯字串上次的 msgid

no_location

移除翻譯字串的位置

fuzzy

使用模糊比對

觸發:

儲存庫更新後

Updates all PO files (as configured by 檔案遮罩) to match the POT file (as configured by 新翻譯的模板) using msgmerge.

Triggered whenever new changes are pulled from the upstream repository. Most msgmerge command-line options can be set up through the add-on configuration.

匯併 Git 送交

Add-on ID:

weblate.git.squash

組態:

squash

送交匯併

Available choices:

all – All commits into one

language – Per language

file – 每一檔案

author – 每一作者

append_trailers

將額外資訊加入至壓縮送交的訊息

額外資訊的列看起來類似於 RFC 822 郵件信箱標頭,位於送交訊息的其他自由格式部分的末尾,例如「Co-authored-by: …」。

commit_message

送交訊息

此送交訊息將用來取代從壓縮送交組合而成的送交訊息。

觸發:

儲存庫送交後

推送更動前先匯併 Git 送交。

以下模式之一中,Git送交可以在推送更改之前被壓縮:

  • 所有送交匯併為一

  • 每個語言

  • 每個檔案

  • 每位作者

Original commit messages are kept, but authorship is lost unless Per author is selected, or the commit message is customized to include it.

The original commit messages can optionally be overridden with a custom commit message.

預告(送交行像 Co-authored-by: )可選地從原始送交訊息中去掉,並且新增在去掉的送交訊息後面。這還可以為每一位翻譯者產生適當的 Co-authored-by: 信譽。

自訂 JSON 輸出

Add-on ID:

weblate.json.customize

組態:

sort_keys

短 JSON 金鑰

indent

JSON 縮排

style

JSON 縮排類型

Available choices:

spaces – 空格

tabs – Tabs

觸發:

儲存後載入

允許調整 JSON 輸出行為,例如縮排或排序。

格式化 Java properties 檔案

Add-on ID:

weblate.properties.sort

組態:

This add-on has no configuration.

觸發:

儲存庫送交前

格式化並排序 Java properties 檔案。

  • 將新行合併為 Unix ones。

  • Unicode 跳脫符號的大寫格式(如果有的話)。

  • 替除空白行與評註。

  • 依鍵值排序字串。

  • 移除重複字串。

陳舊評註移除

Add-on ID:

weblate.removal.comments

組態:

age

保留的天數

觸發:

每日

設定移除評註的時間間隔。

這可以用於刪除可能變得過時的陳舊註釋。小心使用,因為陳舊的註釋不意味著失去了重要性。

陳舊建議移除

Add-on ID:

weblate.removal.suggestions

組態:

age

保留的天數

votes

最低投票數

移除之最低票數。當停用投票時,這個欄位不會有效果。

觸發:

每日

設定移除建議的時間間隔。

此附加元件在與建議投票一道用來刪除在給定的時間內沒有得到足夠的正面投票的建議方面非常有用。

更新 RESX 檔案

Add-on ID:

weblate.resx.update

組態:

This add-on has no configuration.

觸發:

儲存庫更新後

更新所有翻譯檔以符合單語言上游基底檔。未使用的字串將移除,新增加的字串將根據來源字串複製加入。

提示

如果只想刪除陳舊的翻譯鍵,那麼使用 清理翻譯檔

自訂 XML 輸出

Added in version 4.15.

Add-on ID:

weblate.xml.customize

組態:

closing_tags

包含空白 XML 標記的結束標記

觸發:

儲存後載入

允許調整 XML 輸出行為,例如結束標記。

自訂 YAML 輸出

Add-on ID:

weblate.yaml.customize

組態:

indent

YAML 縮排

width

長列換列

Available choices:

80 – 於第 80 個字元處折行

100 – 於第 100 個字元處折行

120 – 於第 120 個字元處折行

180 – 於第 180 個字元處折行

65535 – 不要換列

line_break

換行符號

Available choices:

dos – DOS (\r\n)

unix – UNIX (\n)

mac – MAC (\r)

觸發:

儲存後載入

允許調整 YAML 輸出行為,例如列長或換列。

自訂附加元件列表

附加元件列表由 WEBLATE_ADDONS 進行組態。要新增其他附加元件,只需在這個設定中包含絕對類別名稱即可。

Writing add-on

您也可以編寫自己的附加元件,建立子類別 :class:`weblate.addons.base.BaseAddon`來定義附加元件元資料,接著實施 callback 來進行處理。

也參考

開發附加元件

Executing scripts from add-on

附加元件還可以用於執行外部腳本。這曾經整合在 Weblate 中,但現在必須寫一些代碼,將腳本包裹在附加元件中。

# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later

"""Example pre commit script."""

from django.utils.translation import gettext_lazy

from weblate.addons.events import AddonEvent
from weblate.addons.scripts import BaseScriptAddon


class ExamplePreAddon(BaseScriptAddon):
    # Event used to trigger the script
    events = (AddonEvent.EVENT_PRE_COMMIT,)
    # Name of the addon, has to be unique
    name = "weblate.example.pre"
    # Verbose name and long description
    verbose = gettext_lazy("Execute script before commit")
    description = gettext_lazy("This add-on executes a script.")

    # Script to execute
    script = "/bin/true"
    # File to add in commit (for pre commit event)
    # does not have to be set
    add_file = "po/{{ language_code }}.po"

安裝方法請參見 自訂的品質檢查、附加元件和自動修復

對於任何給定的組件,目前路徑設定為版本控制系統( VCS )儲存庫的根目錄時,執行指令稿。

此外,可以存取後面的環境參數:

WL_VCS

使用的版本控制系統。

WL_REPO

上游儲存庫的網址。

WL_PATH

版本控制系統( VCS )儲存庫的絕對路徑。

WL_BRANCH

目前組件組態的儲存庫分支。

WL_FILEMASK

目前元件的File mask。

WL_TEMPLATE

單語言翻譯模板的檔名(可以為空)。

WL_NEW_BASE

建立新的翻譯所使用檔案的檔名(可以為空)。

WL_FILE_FORMAT

在目前專案中使用的檔案格式。

WL_LANGUAGE

目前處理的翻譯的語言(對於元件級別的鉤子不可用)。

WL_PREVIOUS_HEAD

更新後的上個 HEAD (僅在執行更新後鉤子後可用)。

WL_COMPONENT_SLUG

組件標識串用於建構 URL。

WL_PROJECT_SLUG

專案標識串用於建構 URL。

WL_COMPONENT_NAME

組件名稱。

WL_PROJECT_NAME

專案名稱。

WL_COMPONENT_URL

組件 URL。

WL_ENGAGE_URL

專案參與 URL。

也參考

組件組態

Post-update repository processing

當 VCS 的上游源發生變化時,可用於更新翻譯檔案。為了實現這個功能,請記住 Weblate 只看到送交給版本控制系統( VCS )的檔案,所以需要同意更改作為指令碼的一部分。

例如 Gulp,可以使用後面的程式碼來執行:

#! /bin/sh
gulp --gulpfile gulp-i18n-extract.js
git commit -m 'Update source strings' src/languages/en.lang.json

Pre-commit 翻譯處理

在將翻譯送交到儲存庫之前,使用送交指令稿自動變更翻譯。

它作為組成目前翻譯檔名的單一參數而透過。