- 状态
- 已解决
- 报告针对
- WPML String Translation 3.2.15
- 解决于
- 3.2.16
- 主题标签
- Bug
问题概述
当存储在 .mo 文件中的自定义翻译无法在 WordPress 6.7.x 及更高版本上正确加载时,就会出现此问题。该问题与 WordPress 处理即时 (JIT) 翻译加载方式的更改有关,导致 $l10n 全局变量中的翻译条目被取消设置。
临时解决方法
在继续操作之前,请确保已创建完整的网站备份。
- 打开 …/wp-content/plugins/wpml-string-translation/classes/MO/Hooks/LoadTextDomain.php 文件。
- 找到第 62 行。
- 将:
public function overrideLoadTextDomain( $override, $domain, $mofile ) { if ( ! $mofile ) { return $override; } if ( ! $this->isCustomMOLoaded( $domain ) ) { remove_filter( 'override_load_textdomain', [ $this, 'overrideLoadTextDomain' ], 10 ); $locale = $this->file_locale->get( $mofile, $domain ); $this->loadCustomMOFile( $domain, $mofile, $locale ); add_filter( 'override_load_textdomain', [ $this, 'overrideLoadTextDomain' ], 10, 3 ); } $this->loaded_mo_dictionary->addFile( $domain, $mofile ); return $override; } - 替换为:
public function overrideLoadTextDomain( $override, $domain, $mofile ) { if ( ! $mofile ) { return $override; } if ( ! $this->isCustomMOLoaded( $domain ) ) { $locale = $this->file_locale->get( $mofile, $domain ); $wpml_mofile = $this->file_manager->get( $domain, $locale ); if ( ! file_exists( $mofile ) && $wpml_mofile ) { add_filter( 'load_translation_file', function($file) use ( $mofile, $wpml_mofile ) { if ( $file === $mofile ) { return $wpml_mofile; } return $file; // unset this own hook, just to be used once. }); return $override; } remove_filter( 'override_load_textdomain', [ $this, 'overrideLoadTextDomain' ], 10 ); $this->loadCustomMOFile( $domain, $mofile, $locale ); add_filter( 'override_load_textdomain', [ $this, 'overrideLoadTextDomain' ], 10, 3 ); } $this->loaded_mo_dictionary->addFile( $domain, $mofile ); return $override; }