在没有扩展名的情况下,从 "backend" 翻译 MediaWiki 的可能方法是什么?

What are the possible ways to translate in MediaWiki from "backend", without an extension?

我有一个只有一个扩展名的 MediaWiki 1.33.0 网站 → ContactPage,有了它我可以有一个简单的联系表格。

使用 HTMLForms template engine (in which the default form-template for ContactPage 编写),我扩展了默认表单以包含 选择菜单

我的问题

此选择菜单的选择列表数组键和值在 LocalSettings.php 中用英文书写,但我的网站主要不是 LTR 英语,而是 RTL 希伯来语,我希望它们以我网站的母语显示给最终用户。

我自己的代码模式

wfLoadExtension( 'ContactPage' );
$wgContactConfig['default'] = array(
    'RecipientUser' => 'Admin', // Must be the name of a valid account which also has a verified e-mail-address added to it.
    'SenderName' => 'Contact Form on ' . $wgSitename, // "Contact Form on" needs to be translated
    'SenderEmail' => null, // Defaults to $wgPasswordSender, may be changed as required
    'RequireDetails' => true, // Either "true" or "false" as required
    'IncludeIP' => false, // Either "true" or "false" as required
    'MustBeLoggedIn' => false, // Check if the user is logged in before rendering the form
    'AdditionalFields' => array(
        'omgaselectbox' => [
            'class' => 'HTMLSelectField',
            'label' => 'Select an option',
            'options' => [
                'X' => 'X',
                'Y' => 'Y',
                'Z' => 'Z',
            ],
        ],
    ),
        // Added in MW 1.26
    'DisplayFormat' => 'table',  // See HTMLForm documentation for available values.
    'RLModules' => array(),  // Resource loader modules to add to the form display page.
    'RLStyleModules' => array(),  // Resource loader CSS modules to add to the form display page.
);

可能的解决方案

1) 用希伯来语编写选择列表数组键和值(由于 LTR-RTL 冲突,这可能有点混乱):

'options' => [
    'ס' => 'ס',
    'ט' => 'ט',
    'ז' => 'ז',
],

2) 在客户端翻译英语选择列表数组键和值 JavaScript 通过一些类似的代码:

document.getElementById('select').selectedIndex = 0;
document.getElementById('select').value = 'Default';

我的愿望

我想要一种有序的后端方式来做到这一点,如果有的话,比没有扩展更好

In this discussion, a MediaWiki community member recommended using system message transclution 但是关于它的章节我不太清楚;我不明白这是怎么回事,这对我的情况有什么帮助。

我的问题

在没有扩展名的情况下,从 "backend" 翻译 MediaWiki 有哪些可能的方法?

本地化系统在 MediaWiki 的后端 (php) 和前端 (JavaScript) 部分工作得很好 → 留在后端最好,因为它更小.

假设您采用仅后端方法:

使用预定义字符串进行翻译

如果您想要的翻译已经存在于 MediaWiki 中(例如在另一页表格中),您可以"simply" 重新使用密钥。因此,假设您当前的附加 select 字段定义如下所示:

'Select' => [
    'type' => 'select',
    'options' => [
        'The english message' => 'value'
    ]
],

然后,你可以把它改成这样:

'Select' => [
    'type' => 'select',
    'options-messages' => [
        'the-message-key' => 'test'
    ]
],

请考虑将 options 更改为 options-messages 密钥。
另外:将密钥 the-message-key 更改为您要重复使用的消息密钥。

如果您知道使用 message/string 的页面,您可以使用 GET 选项 uselang 和值 qqx 打开该页面,以便查看消息钥匙。示例:如果在登录页面上使用该字符串,只需使用 https://example.com/wiki/Special:Userlogin?uselang=qqx 打开登录页面即可显示页面上使用的所有消息键。

但是,在这样做时一个警告:大多数情况下不鼓励重复使用现有的消息键,尤其是当它们在其他页面上使用时。考虑到特定上下文,密钥被翻译成数百种语言。这也可能意味着,当在联系页面上使用 string/message 时,特定语言的翻译不适合。所以我建议使用下面的第二个选项。

没有预定义字符串的翻译

通常它会通过扩展来完成,它可以提供一个特定的目录,其中保存了带有消息密钥翻译的 JSON 文件。但是,当您 "just" 自定义扩展程序时,您需要一种方法来为您的密钥添加翻译。

所以,首先,让我们接管上面的更改。将您的 select 字段定义更改为:

'Select' => [
    'type' => 'select',
    'options-messages' => [
        'my-fancy-key' => 'test'
    ]
],

现在,有两种方法可以翻译密钥:

On-Wiki

通过在 wiki 上保存消息,只需编辑 wiki 中的相应页面即可轻松更改消息。在我们的示例中,让我们将密钥翻译成英语和希伯来语:

英语:在您的 wiki 中编辑页面 MediaWiki:My-fancy-key 并添加所需的文本。

希伯来语:在您的 wiki 中编辑页面 MediaWiki:My-fancy-key/he 并添加所需的文本。

作为部署代码的一部分

我们需要为这些消息的翻译注册一个包含 JSON 个文件的目录。我们使用与扩展程序相同的配置变量,$wgMessagesDirs,即使我们没有创建扩展程序。将以下行添加到您的 LocalSettings.php:

$wgMessagesDirs['ContactPageCustomization'] = __DIR__ . '/customContactPage';

现在,在您的 MediaWiki 安装的根文件夹中创建一个目录 customContactPage 并放入以下内容的文件:

en.json

{
    "my-fancy-key": "Default"
}

如果您想翻译成另一种语言,请使用要翻译成的语言代码创建一个新文件。在希伯来语中应该是他,所以让我们创建一个新的语言文件:

he.json

{
    "my-fancy-key": "ברירת מחדל"
}

如果您随后打开联系页面,消息键 my-fancy-key 应该被翻译成英语 Default 并且同样(至少基于 Google 翻译)希伯来语。这是添加自定义翻译的更稳定的方式,但是,您现在还需要注意将密钥翻译成您自己想要支持的语言。如果密钥未翻译成用户的 selected 语言,则使用默认语言英语。