【发布时间】:2023-03-12 07:38:02
【问题描述】:
我对 Drupal 模块开发相当陌生,并且面临使用 update.php 过程自动更新 .po 文件翻译的问题。
目前有一个用于翻译的自定义模块。
自定义的模块info.yml实现如下(翻译设置):
'interface translation project': custom_module
'interface translation server pattern': modules/custom/custom_module/translations/%language.po
.po文件位于指定目录。
我必须在下面的登录表单中添加一些新的 t()
class EntryGlobalLogin extends FormEntryAbstract {
...
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
...
// legend explaining that fields containing * are required
$form['legend'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this->t('* This field is required.'),
'#attributes' => ['class' => ['form-legend']],
];
...
return $form;
}
}
当我通过手动导入新的 .po 文件时
[weburl]/de/admin/config/regional/translate/import
上述值已正确翻译,因此 .po 文件没有错误。
但是,我找不到使用 update.php 过程自动更新已更新 .po 文件的翻译的方法。
正如在其他地方找到的那样,我将以下内容添加到 .module 文件的 HOOK_update_N 挂钩中
function custom_module_update_8049() {
...
// add new translations via de.po update file
Drupal::moduleHandler()->loadInclude('locale', 'compare.inc');
locale_translation_check_projects_local(['custom_module']);
}
update.php 过程运行时没有错误,并且引用的更新在挂起的更新中被提及,最终执行时没有更新所需的翻译。
因此我的问题是:如何以编程方式更新已更改 .po 文件的翻译(在 HOOK_update_N 挂钩内)?
这可能吗,或者这种更改只能手动运行一些 drush 命令或在管理站点中导入 .po 文件?
提前致谢。
Drupal 版本 8.9.17
最好的问候, 帕特里克
【问题讨论】:
标签: php drupal-8 drupal-modules po