英文:
Woocommerce - How to change the text next to star rating
问题
您想要修改以下HTML中的文本“customer reviews”:
<a href="#reviews" class="woocommerce-review-link" rel="nofollow"><span class="count">1</span> customer reviews</a>
您尝试了以下代码,但它对您不起作用:
add_filter( 'gettext', function( $translated_text ) {
if ( 'customer reviews' === $translated_text ) {
$translated_text = 'reviews';
}
return $translated_text;
} );
这段代码的目的是将“customer reviews”翻译为“reviews”。
英文:
Thank for reading my issue.
Please help that which code snippets can let me change the text "customer reviews" in html below:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<a href="#reviews" class="woocommerce-review-link" rel="nofollow"><span class="count">1</span> customer reviews</a>
<!-- end snippet -->
I had try this but it not work for me.
add_filter( 'gettext', function( $translated_text ) {
if ( '**customer reviews**' === $translated_text ) {
$translated_text = '**reviews**';
}
return $translated_text;
} );
Thank you!
答案1
得分: 0
这是一个有点复杂的情况,我将尽量使用简单的英语。如果您通过WooCommerce插件搜索所需的字符串,您将找到以下内容:
_n( '%s customer review', '%s customer reviews', $review_count, 'woocommerce' )
这意味着您不能使用gettext过滤器,而是需要使用ngettext,因为这是一个更复杂的国际化函数:
add_filter( 'ngettext', 'bbloomer_modify_n_customer_reviews', 9999, 5 );
function bbloomer_modify_n_customer_reviews( $translation, $single, $plural, $number, $domain ) {
$translation = preg_replace( '%s customer reviews', '%s reviews', $translation );
return $translation;
}
英文:
This is a little complex so I'll try to use plain English where possible. If you do a search through the WooCommerce plugin for the string you need, you'll find this:
_n( '%s customer review', '%s customer reviews', $review_count, 'woocommerce' )
...which means you can't use the gettext filter, you need the ngettext one instead as this is a more complex internationalization function:
add_filter( 'ngettext', 'bbloomer_modify_n_customer_reviews', 9999, 5 );
function bbloomer_modify_n_customer_reviews( $translation, $single, $plural, $number, $domain ) {
$translation = preg_replace( '%s customer reviews', '%s reviews', $translation );
return $translation;
}
答案2
得分: 0
在WordPress中,要更改前端或后台显示的任何文本,您可以使用以下代码来进行更改:
add_filter('gettext', '_translateTextCustom', 100, 3 );
function _translateTextCustom( $translated_text, $text, $domain ) {
global $pagenow, $post_type;
if ( !is_admin() && "customer reviews" === $text && 'woocommerce' === $domain )
{
$translated_text = __('reviews', $domain );
}
return $translated_text;
}
global $pagenow
- 使用这个全局变量,您可以访问所有的php页面。
> in_array( $pagenow, ['post.php', 'post-new.php']
global $post_type
- 使用这个全局变量,您可以有条件地访问所有的文章类型。
> 'product' === $post_type || 'page' === $post_type || 'post' === $post_type
英文:
In WordPress for changing any text showing on front end or on admin you can change with add_filter('gettext', '_translateTextCustom', 100, 3 );
Try below code snippet
add_filter('gettext', '_translateTextCustom', 999, 3 );
function _translateTextCustom( $translated_text, $text, $domain ) {
global $pagenow, $post_type;
if ( !is_admin() && "customer reviews" === $text && 'woocommerce' === $domain )
{
$translated_text = __( 'reviews', $domain );
}
return $translated_text;
}
global $pagenow
- You will access all php pages with this global variable.
> in_array( $pagenow, ['post.php', 'post-new.php']
global $post_type
- You will access all post types which you want conditionally.
> 'product' === $post_type || 'page' === $post_type || 'post' === $post_type
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论