检查和修正

定制的自动修正

还可以应用除了自动修正以外自己的自动修正,并将它们包括到 AUTOFIX_LIST

自动修复很强大,但可能导致损坏;写脚本的时候要小心。

例如,后面的自动修复会将每次出现的字符串 foo 在翻译中替换为 bar

#
# Copyright © 2012–2022 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

from django.utils.translation import gettext_lazy as _

from weblate.trans.autofixes.base import AutoFix


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

    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 类提供完全合规的路径,请参见 定制的质量检查、附加组件和自动修复

使用标记定制行为

您可以使用标记微调 Weblate 的行为。这可以在源字符串层面进行(请参见 源字符串另外的信息),或者在 部件配置翻译标记)中进行。某些文件格式还允许直接在格式中指定标记(请参见 支持的文件格式)。

标记用逗号分隔,参数用冒号分隔。可以在字符串中使用引号来包含空格或特定字符。例如:

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

单引号和双引号都被接受,特殊字符使用反斜杠进行转义:

placeholders:"quoted \"string\"":'single \'quoted\''

以下是目前接受的标记列表:

rst-text

将文本视为 reStructuredText 文档,影响 未更改的翻译

dos-eol

使用 DOS 的行尾标记,而不使用 Unix 的(使用 \r\n 而不使用 \n)。

read-only

这个字符串是只读的,且不应该在 Weblate 中进行编辑。请参见 只读字符串

priority:N

字符串的优先级。高优先级的字符串首先出现被翻译。默认的优先级是 100,字符串的优先级越高,就会越早安排翻译。

max-length:N

将字符串的最大长度限制为 N 个字符,请参见 译文最大长度

xml-text

将文本看作 XML 文档,影响 XML 语法XML 标记

font-family:NAME

定义 font-family 来提供检查,请参见 管理字型

font-weight:WEIGHT

定义 font-weight 来提供检查,请参见 管理字型

font-size:SIZE

定义 font-size 来提供检查,请参见 管理字型

font-spacing:SPACING

定义渲染检查的字母间隔,请参见 管理字型

icu-flags:FLAGS

指定自定义 ICU MessageFormat 质量检查行为的标记。

icu-tag-prefix:PREFIX

ICU MessageFormat 质量检查设置必需的 XML 标签前缀。

placeholders:NAME:NAME2:...

译文中需要的占位符字符串,请参见 占位符

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

当检查结果文本参数时执行替换(例如在 最大翻译大小译文最大长度 中)。这一典型应用的情况拓展了非译元素,确保匹配那些即使使用了长值的文本,例如 replacements:%s:"John Doe"

variants:SOURCE

将此字符串标记为具有匹配源的字符串的变体。见 字符串变体

regex:REGEX

用于匹配翻译文件的正则表达式,详见 正则表达式

forbidden

表示术语表中的禁止翻译,参见 禁止的翻译

strict-same

使“未更改的译文”检查不使用内置单词黑名单。请参见 未更改的翻译

check-glossary

启用 不遵循术语表 质量检查。

angularjs-format

启用 AngularJS 插值字符串 质量检查。

c-format

启用 C 格式 质量检查。

c-sharp-format

启用 C# 格式 质量检查。

es-format

启用 ECMAScript 模板字面量 质量检查。

i18next-interpolation

启用 i18next 插值 质量检查。

icu-message-format

启用 ICU MessageFormat 质量检查。

java-printf-format

启用 Java 格式 质量检查。

java-format

启用 Java MessageFormat 质量检查。

javascript-format

启用 JavaScript 格式 质量检查。

lua-format

启用 Lua 格式 质量检查。

object-pascal-format

启用 Object Pascal 格式 质量检查。

percent-placeholders

启用 百分比占位符 质量检查。

perl-format

启用 Perl 格式 质量检查。

php-format

启用 PHP 格式 质量检查。

python-brace-format

启用 Python brace 格式 质量检查。

python-format

启用 Python 格式 质量检查。

qt-format

启用 Qt 格式 质量检查。

qt-plural-format

启用 Qt 复数格式 质量检查。

ruby-format

启用 Ruby 格式 质量检查。

scheme-format

启用 Scheme 格式 质量检查。

vue-format

启用 Vue I18n 格式化 质量检查。

md-text

将文本看作 Markdown 文档。启用 Markdown 链接, Markdown 引用, 和 Markdown 语法 质量检查。

case-insensitive

调整检查行为不区分大小写。目前仅影响 占位符 质量检查。

safe-html

启用 不安全的 HTML 质量检查。

url

字符串应该只由一个 URL 组成。启用 URL 质量检查。

ignore-all-checks

忽略所有质量检查。

ignore-bbcode

跳过 BBCode 标记 质量检查。

ignore-duplicate

跳过 连续重复的单词 质量检查。

ignore-check-glossary

跳过 不遵循术语表 质量检查。

ignore-double-space

跳过 双空格 质量检查。

ignore-angularjs-format

跳过 AngularJS 插值字符串 质量检查。

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-format

跳过 Java MessageFormat 质量检查。

ignore-java-printf-format

跳过 Java 格式 质量检查。

ignore-javascript-format

跳过 JavaScript 格式 质量检查。

ignore-lua-format

跳过 Lua 格式 质量检查。

ignore-object-pascal-format

跳过 Object Pascal 格式 质量检查。

ignore-percent-placeholders

跳过 百分比占位符 质量检查。

ignore-perl-format

跳过 Perl 格式 质量检查。

ignore-php-format

跳过 PHP 格式 质量检查。

ignore-python-brace-format

跳过 Python brace 格式 质量检查。

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-kashida

跳过 使用了 Kashida 字母 质量检查。

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-question

跳过 不匹配的问号 质量检查。

ignore-end-semicolon

跳过 不匹配的分号 质量检查。

ignore-newline-count

跳过 不匹配的换行符 质量检查。

ignore-plurals

跳过 缺少复数形式 质量检查。

ignore-placeholders

跳过 占位符 质量检查。

ignore-punctuation-spacing

跳过 标点间距 质量检查。

ignore-regex

跳过 正则表达式 质量检查。

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-icu-message-format-syntax

跳过 ICU MessageFormat 语法 质量检查。

ignore-long-untranslated

跳过 长期未翻译 质量检查。

ignore-multiple-failures

跳过 多项未通过的检查 质量检查。

ignore-unnamed-format

跳过 多个未命名的变量 质量检查。

ignore-optional-plural

跳过 未复数化 质量检查。

备注

通常,对于任何检查,都可以使用标识符将规则命名为 ignore-* ,所以您甚至可以将其用于自定义检查。

每个源字符串的设置,在 部件配置 设置中,并且在翻译文件自身中(例如在 GNU gettex 中),能够理解这些标记。

强制检查

在 3.11 版本加入.

您可以通过在 部件配置 中设置 强制检查 来配置不能忽略的检查列表。每个列出的检查都不能在用户界面中关闭,任何未通过此检查的字符串都标记为 需要编辑`(请参阅 :ref:`states)。

管理字型

在 3.7 版本加入.

提示

上传到 Weblate 的字体仅用于 最大翻译大小 检查,它们对 Weblate 用户界面没有影响。

用于计算所呈现文本尺寸的 最大翻译大小 检查需要将字体加载到 Weblate 中,并使用翻译标记选中(请参见 使用标记定制行为)。

在您的翻译项目 管理 菜单下 字体 中的 Weblate 字体管理工具提供了上传和管理字体的界面。可以上传 TrueType 或 OpenType 字体,设置 font-groups 并在检查中使用它们。

字型组允许为不同语言确定不同字型,这是非拉丁语言中典型需要的:

../_images/font-group-edit.png

字型组通过名称识别,名称不能包含空格或特殊字符,这使它能够容易地用在检查定义中:

../_images/font-group-list.png

字型族和样式在上传后自动识别:

../_images/font-edit.png

可以将几种字型加载到 Weblate 中:

../_images/font-list.png

为了使用字型来检查字符串长度,将适当的标记传递给它 (请参见 使用标记定制行为)。可能会需要后面这些:

max-size:500

指定以像素为单位的最大宽度。

font-family:ubuntu

通过指定其标识符来定义要使用的字型组。

font-size:22

指定以像素为单位的字体大小。

编写自己的检查

Weblate 内置了多种多样的质量检查,(请参阅 质量检查),尽管它们可能没有涵盖您想要检查的所有东西。可以使用 CHECK_LIST 来调整执行的检查列表,也可以添加自定义检查。

  1. 子类 weblate.checks.Check

  2. 设置一些属性。

  3. 应用 check (如果想要处理代码中的复数的话)或 check_single 方法(它将为你完成)。

一些示例:

为了安装定制的检查,在 CHECK_LIST 中为 Python 类提供完全合格的路径,请参见 定制的质量检查、附加组件和自动修复

检查译文文本是否不包含“foo”

这是一个非常简单的检查,只是检查译文中是否缺少了字符串“foo”。

#
# Copyright © 2012–2022 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
"""Simple quality check example."""

from django.utils.translation import gettext_lazy as _

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
    name = _("Foo check")

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

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

检查捷克语译文文本复数是否不同

使用语言信息检查,验证捷克语中的两种复数形式不同。

#
# Copyright © 2012–2022 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
"""Quality check example for Czech plurals."""

from django.utils.translation import gettext_lazy as _

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
    name = _("Foo check")

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

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

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