檢查和修復

自訂自動修正

還可以應用除了自動修正以外自己的自動修正,並將它們包括到 AUTOFIX_LIST

自動修復很強大,但可能導致損壞;寫指令碼的時候要小心。

例如,後面的自動修復會將每次出現的字串 foo 在翻譯中替換為 bar

from weblate.trans.autofixes.base import AutoFix


class ReplaceFooWithBar(AutoFix):
    """Replace foo with bar."""

    # Might be localized using gettext_lazy
    name = "Foobar"

    def fix_single_target(self, target, source, unit):
        if "foo" in target:
            return target.replace("foo", "bar"), True
        return target, False

為了安裝客製的檢查,在 AUTOFIX_LIST 中為 Python 類提供完全合規的路徑,請參閱 自訂的品質檢查、附加元件與自動修復

使用標誌自訂行為

You can fine-tune Weblate's behavior by using flags. The flags provide visual feedback to the translators and help them to improve their translation. The flags are merged from following sources:

The flags are comma-separated; if they have parameters, they are separated with colon. You can use quotes to include whitespaces or special characters in the string. For example:

placeholders:"special:value":"other value", regex:.*

單引號或是雙引號是可接受的,特殊符號就會被使用反斜線 \ 挑脫處理:

placeholders:"quoted \"string\"":'single \'quoted\''
placeholders:r"^#*"

To verify that translators do not change the heading of a Markdown document. A failing check will be triggered if the string ### Index is translated as # Indice.

placeholders:r"\]\([^h].*?\)"

To ensure that internal links are not being translated (i.e. [test](../checks) does not become [test](../chequeos).

The flags defined on a higher level can be discarded using the discard:NAME syntax. For example, if a component is configured to safe-html, you can add discard:safe-html to the string flags to skip it for this particular string.

這裡是現在能接受的標記的清單:

read-only

字串應該唯讀,並且不能在 Weblate 中編輯。請參閱 唯讀字串

terminology

Used in 詞彙表. Copies the string into all glossary languages so it can be used consistently in all translations. Also useful in combination with read-only, for example in product names.

priority:N

字串的優先順序。高優先順序的字串首先出現被翻譯。預設的優先順序是 100,字串的優先順序越高,就會越早安排翻譯。

max-length:N

將字串的最大長度限制為 N 個字元,請參閱 翻譯最大長度

xml-text

將文字看作 XML 檔案,影響 XML 語法 and XML 標記

font-family:NAME

定義 font-family 來提供檢查,請參閱 管理字型

font-weight:WEIGHT

定義 font-weight 來提供檢查,請參閱 管理字型

font-size:SIZE

定義 font-size 來提供檢查,請參閱 管理字型

font-spacing:SPACING

定義渲染檢查的字母間隔,請參閱 管理字型

icu-flags:FLAGS

Define flags for customizing the behavior of the ICU MessageFormat quality check.

icu-tag-prefix:PREFIX

Set a required prefix for XML tags for the ICU MessageFormat quality check.

placeholders:NAME:NAME2:...

翻譯中需要的佔位字串,請參閱 佔位符號

replacements:FROM:TO:FROM2:TO2...

當檢查結果文字引數時執行替換(例如在 翻譯的最大長度翻譯最大長度 中)。這一典型應用的情況拓展了非譯元素,確保匹配那些即使使用了長值的文字,例如 replacements:%s:"John Doe"

variants:SOURCE

將此字串標記為具有匹配來源的字串的變體。見 字串變數

regex:REGEX

正規表達式用來比對翻譯,參閱:正規表達式

discard:NAME

Discards flag defined on a higher level.

forbidden

表示術語表中的禁止翻譯,請參閱 禁止的翻譯

strict-same

Make the 未變更的翻譯 avoid using the built-in words exceptions.

strict-format

Make format checks enforce using format even for plural forms with a single value, see 格式化字串.

case-insensitive

Adjust checks behavior to be case-insensitive. Currently affects only 佔位符號 quality check.

bbcode-text

Treat a text as an Bulletin Board Code (BBCode) document, affects 未變更的翻譯. Enables the BBCode 標記 quality check.

check-glossary

啟用 與詞彙表不同 品質檢查。

fluent-parts

啟用 Fluent 部分 品質檢查。

fluent-references

啟用 Fluent 引用 品質檢查。

fluent-target-inner-html

啟用 Fluent 翻譯 inner HTML 品質檢查。

fluent-target-syntax

啟用 Fluent 翻譯語法 品質檢查。

angularjs-format

啟用 AngularJS 插值字串 品質檢查。

automattic-components-format

啟用 Automattic 組件格式設定 品質檢查。

c-format

啟用 C 格式 品質檢查。

c-sharp-format

啟用 C# 格式 品質檢查。

csharp-format

啟用 C# 格式 品質檢查。

es-format

啟用 ECMAScript 範本字面值 品質檢查。

i18next-interpolation

啟用 i18next 插值 品質檢查。

icu-message-format

啟用 ICU MessageFormatICU MessageFormat 語法 品質檢查。

java-printf-format

啟用 Java 格式 品質檢查。

java-format

啟用 Java MessageFormat 品質檢查。

auto-java-messageformat

Treat a text as conditional Java MessageFormat, enabling Java MessageFormat only when the source contains Java MessageFormat placeholders. Enables the Java MessageFormat quality check.

javascript-format

啟用 JavaScript 格式 品質檢查。

laravel-format

啟用 Laravel 格式 品質檢查。

lua-format

啟用 Lua 格式 品質檢查。

object-pascal-format

啟用 Object Pascal 格式 品質檢查。

objc-format

啟用 Objective-C 格式 品質檢查。

percent-placeholders

啟用 百分比佔位符號 品質檢查。

perl-brace-format

啟用 Perl 大括號格式 品質檢查。

perl-format

啟用 Perl 格式 品質檢查。

php-format

啟用 PHP 格式 品質檢查。

python-brace-format

啟用 Python 大括號格式 品質檢查。

python-format

啟用 Python 格式 品質檢查。

qt-format

啟用 Qt 格式 品質檢查。

qt-plural-format

啟用 Qt 複數格式 品質檢查。

ruby-format

啟用 Ruby 格式 品質檢查。

scheme-format

啟用 Scheme 格式 品質檢查。

vue-format

啟用 Vue I18n 格式 品質檢查。

rst-text

Treat a text as an reStructuredText document, affects 未變更的翻譯. Enables the reStructuredText 不一致 and reStructuredText 語法錯誤 quality checks.

md-text

Treat a text as a Markdown document, and provide Markdown syntax highlighting on the translation text area. Enables the Markdown 連結, Markdown 參照 and Markdown 語法 quality checks.

max-length

啟用 翻譯最大長度 品質檢查。

max-size

啟用 翻譯的最大長度 品質檢查。

placeholders

啟用 佔位符號 品質檢查。

regex

啟用 正規表達式 品質檢查。

safe-html

啟用 不安全的 HTML 品質檢查。

auto-safe-html

Treat a text as conditional HTML, enabling 不安全的 HTML only for plain text or source strings that contain standard HTML markup or valid custom elements. This is useful for extended Markdown variants such as MDX, where angle-bracket syntax may not be HTML. Enables the 不安全的 HTML quality check.

url

The string should consist of only a URL. Enables the URL quality check.

fluent-source-inner-html

啟用 Fluent 來源內部 HTML 品質檢查。

fluent-source-syntax

啟用 Fluent 來源語法 品質檢查。

ignore-all-checks

忽略所有品質檢查。

ignore-bbcode

略過 BBCode 標記 品質檢查。

ignore-xml-chars-around-tags

略過 XML 標籤周圍的字元 品質檢查。

ignore-duplicate

略過 連續重複單字 品質檢查。

ignore-check-glossary

略過 與詞彙表不同 品質檢查。

ignore-double-space

略過 兩個空白 品質檢查。

ignore-fluent-parts

略過 Fluent 部分 品質檢查。

ignore-fluent-references

略過 Fluent 引用 品質檢查。

ignore-fluent-target-inner-html

略過 Fluent 翻譯 inner HTML 品質檢查。

ignore-fluent-target-syntax

略過 Fluent 翻譯語法 品質檢查。

ignore-angularjs-format

略過 AngularJS 插值字串 品質檢查。

ignore-automattic-components-format

略過 Automattic 組件格式設定 品質檢查。

ignore-c-format

略過 C 格式 品質檢查。

ignore-c-sharp-format

略過 C# 格式 品質檢查。

ignore-es-format

略過 ECMAScript 範本字面值 品質檢查。

ignore-i18next-interpolation

略過 i18next 插值 品質檢查。

ignore-icu-message-format

略過 ICU MessageFormat 品質檢查。

ignore-java-printf-format

略過 Java 格式 品質檢查。

ignore-java-format

略過 Java MessageFormat 品質檢查。

ignore-javascript-format

略過 JavaScript 格式 品質檢查。

ignore-laravel-format

略過 Laravel 格式 品質檢查。

ignore-lua-format

略過 Lua 格式 品質檢查。

ignore-object-pascal-format

略過 Object Pascal 格式 品質檢查。

ignore-objc-format

略過 Objective-C 格式 品質檢查。

ignore-percent-placeholders

略過 百分比佔位符號 品質檢查。

ignore-perl-brace-format

略過 Perl 大括號格式 品質檢查。

ignore-perl-format

略過 Perl 格式 品質檢查。

ignore-php-format

略過 PHP 格式 品質檢查。

ignore-python-brace-format

略過 Python 大括號格式 品質檢查。

ignore-python-format

略過 Python 格式 品質檢查。

ignore-qt-format

略過 Qt 格式 品質檢查。

ignore-qt-plural-format

略過 Qt 複數格式 品質檢查。

ignore-ruby-format

略過 Ruby 格式 品質檢查。

ignore-scheme-format

略過 Scheme 格式 品質檢查。

ignore-vue-format

略過 Vue I18n 格式 品質檢查。

ignore-translated

略過 已經翻譯過 品質檢查。

ignore-inconsistent

略過 不一致 品質檢查。

ignore-rst-references

略過 reStructuredText 不一致 品質檢查。

ignore-kashida

略過 使用 Kashida letter 品質檢查。

ignore-md-link

略過 Markdown 連結 品質檢查。

ignore-md-reflink

略過 Markdown 參照 品質檢查。

ignore-md-syntax

略過 Markdown 語法 品質檢查。

ignore-max-length

略過 翻譯最大長度 品質檢查。

ignore-max-size

略過 翻譯的最大長度 品質檢查。

ignore-escaped-newline

略過 不相配的 \n 品質檢查。

ignore-end-colon

略過 冒號不相符 品質檢查。

ignore-end-ellipsis

略過 刪節號不相符 品質檢查。

ignore-end-exclamation

略過 驚嘆號不相符 品質檢查。

ignore-end-stop

略過 句號不相符 品質檢查。

ignore-end-interrobang

略過 不相配的驚歎問號 品質檢查。

ignore-end-question

略過 問號不相符 品質檢查。

ignore-end-semicolon

略過 分號不相符 品質檢查。

ignore-newline-count

略過 斷列符不相配 品質檢查。

ignore-plurals

略過 缺少複數 品質檢查。

ignore-multiple-capital

略過 多個大寫字母 品質檢查。

ignore-kabyle-characters

略過 非標準的柏柏爾語字元 品質檢查。

ignore-placeholders

略過 佔位符號 品質檢查。

ignore-prohibited-initial-character

略過 禁止的初始字元 品質檢查。

ignore-punctuation-spacing

略過 標點符號間距 品質檢查。

ignore-regex

略過 正規表達式 品質檢查。

ignore-rst-syntax

略過 reStructuredText 語法錯誤 品質檢查。

ignore-reused

略過 重複使用的翻譯 品質檢查。

ignore-same-plurals

略過 相同複數 品質檢查。

ignore-begin-newline

略過 開頭換行 品質檢查。

ignore-begin-space

略過 開頭空格 品質檢查。

ignore-end-newline

略過 後隨換行 品質檢查。

ignore-end-space

略過 後隨空格 品質檢查。

ignore-same

略過 未變更的翻譯 品質檢查。

ignore-safe-html

略過 不安全的 HTML 品質檢查。

ignore-url

略過 URL 品質檢查。

ignore-xml-tags

略過 XML 標記 品質檢查。

ignore-xml-invalid

略過 XML 語法 品質檢查。

ignore-zero-width-space

略過 零寬度空格 品質檢查。

ignore-ellipsis

略過 刪節號 品質檢查。

ignore-fluent-source-inner-html

略過 Fluent 來源內部 HTML 品質檢查。

ignore-fluent-source-syntax

略過 Fluent 來源語法 品質檢查。

ignore-icu-message-format

略過 ICU MessageFormat 語法 品質檢查。

ignore-long-untranslated

略過 長期未翻譯 品質檢查。

ignore-multiple-failures

略過 多項未通過檢查 品質檢查。

ignore-unnamed-format

略過 多個未命名變數 品質檢查。

ignore-optional-plural

略過 未複數化 品質檢查。

備註

通常規則是,任何檢查都使用識別文字來命名 ignore-*,所以能夠將規則應用在自訂檢查中。

每個來源字串的設定,在 組件配置 設定中,並且在翻譯檔案自身中(例如在 GNU gettex 中),能夠理解這些標記。

基於位置的旗標

Some flags are added to strings by default, based on their locations. This means that certain checks will be automatically enabled depending on where the string is used.

  • rst-text: This flag is automatically added to strings in reStructuredText files, if location extension is .rst.

  • md-text: This flag is automatically added to strings in Markdown and MDX files, if location extension is .md, .markdown, or .mdx.

強制檢查

The enforced checks cannot be dismissed and mark string as Needs editing (see 翻譯狀態). This prevents translators from hiding such checks.

提示

Turning on check enforcing doesn't enable it automatically. Some checks have to be turned on by adding the corresponding flag to the string or component flags.

This is best used with checks that can cause serious issues when used like checks for 格式化字串. Using for style checks like 未變更的翻譯 is not recommended because dismissal is sometimes a reasonable approach in these.

The 翻譯品質過濾器 can then be used to exclude strings needing editing from being committed to the version control.

管理字型

提示

上傳到Weblate的字型純粹用於 翻譯的最大長度”檢查,它們在WebLate使用者介面中沒有效果。

用於計算呈現文本需要的尺寸的 翻譯的最大長度 檢查需要字型被載入進 Weblate 並被一個翻譯標識選中 (見 使用標誌自訂行為)。

Weblate font management tool in Fonts under the Operations menu of your translation project provides interface to upload and manage fonts. TrueType or OpenType fonts can be uploaded, set up font-groups and use those in the check.

字型群組允許為不同語言確定不同字型,這是非拉丁語言中典型需要的:

../_images/font-group-edit.webp

字型群組透過名稱識別,名稱不能包含空白字元或特殊字元,這使它能夠容易地用在檢查定義中:

../_images/font-group-list.webp

Font-family and style are automatically recognized after uploading them:

../_images/font-edit.webp

可以將幾種字型載入到 Weblate 中:

../_images/font-list.webp

為了使用字型來檢查字串長度,將適當的標記傳遞給它 (請參閱 使用標誌自訂行為)。可能會需要後面這些:

max-size:500 / max-size:300:5

Defines maximal width in pixels and, optionally, the maximum number of lines (word wrapping is applied).

font-family:ubuntu

確定字型群組,透過指定其識別文字來使用。

font-size:22

定義字型大小(pixels)。

撰寫自訂檢查

Weblate 內建了很大範圍的品質檢查,(請參閱 品質檢查 ),儘管可能沒有覆寫想要檢查的所有內容。可以使用 CHECK_LIST 來調整執行檢查的清單,也可以新增客製的檢查。

  1. weblate.checks.Check 子類別

  2. 設定一些特性。

  3. 應用 check (如果想要處理程式碼中的複數的話)或 check_single 方法(它將為您完成)。

一些範例:

為了安裝客製的檢查,在 CHECK_LIST 中為 Python 類提供完全合格的路徑,請參閱 自訂的品質檢查、附加元件與自動修復

檢查翻譯文字不含有 “ foo ”

這是非常簡單的檢查,只檢查翻譯中是否遺失了字串“ foo ”。

"""Simple quality check example."""

from django.utils.translation import gettext_lazy

from weblate.checks.base import TargetCheck


class FooCheck(TargetCheck):
    # Used as identifier for check, should be unique
    # Has to be shorter than 50 characters
    check_id = "foo"

    # Short name used to display failing check
    # Might be localized using gettext_lazy
    name = "Foo check"

    # Description for failing check
    description = gettext_lazy("Your translation is foo")

    # Real check code
    def check_single(self, source, target, unit):
        return "foo" in target

檢查捷克語翻譯文字的複數差異

使用語言資訊來驗證捷克語中的兩種複數形式並不相同。

"""Quality check example for Czech plurals."""

from django.utils.translation import gettext_lazy

from weblate.checks.base import TargetCheck


class PluralCzechCheck(TargetCheck):
    # Used as identifier for check, should be unique
    # Has to be shorter than 50 characters
    check_id = "foo"

    # Short name used to display failing check
    # Might be localized using gettext_lazy
    name = "Foo check"

    # Description for failing check
    description = gettext_lazy("Your translation is foo")

    # Real check code
    def check_target_unit(self, sources, targets, unit):
        if unit.translation.language.is_base({"cs"}):
            return targets[1] == targets[2]
        return False

    def check_single(self, source, target, unit) -> bool:
        """We don't check target strings here."""
        return False