WordPress 挂钩gettext
,并且ngettext
有一段时间没有变化,并且可以处理可翻译的字符串,独立于 WooCommerce 版本......</p>
使其与钩子一起工作的正确方法gettext
是ngettext
(简化一点并添加一些缺少的函数参数):
add_filter( 'gettext', 'change_some_woocommerce_strings', 10, 3 );
add_filter( 'ngettext', 'change_some_woocommerce_strings', 10, 3 );
function change_some_woocommerce_strings( $translate_text, $original_text, $domain ) {
if ( stripos( $original_text, 'Product') !== false || stripos( $original_text, 'Categories') !== false ) {
$translate_text = str_ireplace(
array('Product categories', 'Products', 'Product'),
array('Prints', 'Prints', 'Print'),
$original_text );
}
return $translate_text;
}
如果某些字符串未翻译,可能是因为它们添加了一些上下文。在这种情况下,钩子gettext_with_context
是必需的,例如:
add_filter( 'gettext_with_context', 'change_some_woocommerce_strings_with_context', 10, 4 );
function change_some_woocommerce_strings_with_context( $translate_text, $original_text, $context, $domain ) {
if ( stripos( $original_text, 'Product') !== false || stripos( $original_text, 'Categories') !== false ) {
$translate_text = str_ireplace(
array('Product categories', 'Products', 'Product'),
array('Prints', 'Prints', 'Print'),
$original_text );
}
return $translate_text;
}
代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。