【问题标题】:Drupal 8 updating custom module's language.po fileDrupal 8 更新自定义模块的 language.po 文件
【发布时间】: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


    【解决方案1】:

    由于我对 .po 文件的更改范围较小,因此我设法通过 hook_update_N 实现了翻译,如下所示: https://drupal.stackexchange.com/a/220608.

    因此,在将 custom_module_add_translation 添加到 .module 文件后,编码解决方案如下所示

    function custom_module_update_8049() {
    
      ...
    
      // Update new translations.
      custom_module_add_translation("* This field is required.", "* Dieses Feld muss ausgefüllt werden.", "de");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      • 2015-02-21
      • 2015-08-03
      • 2020-05-06
      • 2018-07-24
      • 1970-01-01
      • 2017-01-01
      相关资源
      最近更新 更多