这个带有超链接的 WordPress 主题国际化做得对吗?我花了一整天的时间来解决这个问题,但翻译没有出现在页面上。只有英文原版。
MO 文件使用 Loco Translate 生成到 my-theme/languages 文件夹中。
在 footer.php 中:
$anchor = esc_html_x( 'Link', 'link text for Link', 'my-theme' );
$domain = esc_url( __( 'https://myaddress.com', 'my-theme' ) );
$link = sprintf( '<a href="%s" rel="noopener" target="_blank">%s</a>', $domain, $anchor );
echo sprintf( esc_html__( ' Check this %1$s', 'my-theme' ), $link );
在 my-theme.pot 文件中:
#: footer.php:56
msgid "Link"
msgstr ""
#: footer.php:59
msgid " Check this %1$s"
msgstr ""
在functions.php文件中:
function my_theme_textdomain() {
load_theme_textdomain( 'my-theme', get_template_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'my_theme_textdomain' );
文本域也包含在 style.css 注释中。