我正在尝试翻译“Have a coupon?”使用此代码在 Woocommerce 结帐页面中输入文本:
function custom_strings_translation( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'HAVE A COUPON?' :
$translated_text = __( 'TIENES UN CUPÓN?', '__x__' );
break;
}
return $translated_text;
}
add_filter('gettext', 'custom_strings_translation', 20, 3);
但它不起作用。
谁能告诉我为什么?
最佳答案
It doesn't work because the text is not in capitals and you are not using the right variable.
有两种方法可以更改此文本(第二种方式似乎最好):
1) 以这种方式使用 gettext
过滤器钩子(Hook):
add_filter('gettext', 'custom_strings_translation', 20, 3);
function custom_strings_translation( $translated_text, $text, $domain ) {
if( $text == 'Have a coupon?' ){
$translated_text = __( 'Tienes un cupón?', $domain );
}
return $translated_text;
}
代码进入事件子主题(或事件主题)的 function.php 文件。经过测试并有效。
2) 使用 woocommerce_checkout_coupon_message
过滤器钩子(Hook)可以让您进行更多更改:
add_filter( 'woocommerce_checkout_coupon_message', 'custom_checkout_coupon_message', 20, 1 );
function custom_checkout_coupon_message( $notice ) {
return __( 'Tienes un cupón?', 'woocommerce' ) . ' <a href="#" class="showcoupon">' . __( 'Haga clic aquí para ingresar su código', 'woocommerce' ) . '</a>'
}
代码进入事件子主题(或事件主题)的 function.php 文件。经过测试并有效。
相关:
关于php - 如何更改 Woocommerce 结帐页面中的 "Have a coupon?"文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52508477/