Php 更改或翻译商业中的特定文本
我在网上找到了一个解决方案,但似乎不起作用 它说要编辑下面的文件,这是我几天前做的,但不知怎么的,它仍然不能工作 /wp content/plugins/woocommerce/templates/single product/related.php 如果将其FTP到服务器,则文件显示如下:Php 更改或翻译商业中的特定文本,php,wordpress,woocommerce,translation,gettext,Php,Wordpress,Woocommerce,Translation,Gettext,我在网上找到了一个解决方案,但似乎不起作用 它说要编辑下面的文件,这是我几天前做的,但不知怎么的,它仍然不能工作 /wp content/plugins/woocommerce/templates/single product/related.php 如果将其FTP到服务器,则文件显示如下: if ( $products->have_posts() ) : ?> <div class="related products"> <h2><?php
if ( $products->have_posts() ) : ?>
<div class="related products">
<h2><?php _e('You may also like', 'woocommerce' ); ?></h2>
if($products->have_posts()):?>
然而,该网页仍然显示“相关产品”,而不是“您也可能喜欢”
出于某种原因,这并没有发生,也没有在某个地方被过度利用
有什么想法吗?覆盖默认模板的最佳方法是将文件复制到当前主题中名为
/woocommerce/single product
的文件夹中。对该文件进行更改
通常,要覆盖以下模板文件:
/wp content/plugins/woocommerce/templates/
将文件复制到
/wp content///woocommerce///code>我在子函数中找到了以下内容:
/**
*更改文本字符串
*
*@linkhttp://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
*/
函数my_text_字符串($translated_text,$text,$domain){
开关($translated_text){
“相关产品”案例:
$translated_text=uuuuuuu('Check out the related products','woocommerce');
打破
}
返回$translated_text;
}
添加过滤器('gettext','my_text_strings',20,3);
它在这里工作得很好:这里是user5217948
的代码,需要从Frithir
进行案例更新:
// CHANGE RELATED PRODUCTS TEXT
function my_text_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Related products' :
$translated_text = __( 'You may also like...', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );
该代码适用于WooCommerce 3.3.1和WordPress 4.9.4(均约于2018年2月)
.对于那些使用另一种语言的woocommerce/wordpress的人来说,这是一个友好的提示
您必须将“相关产品”替换为显示在您的语言中的文本。在我的例子中,相关产品被翻译成“productos relacionados”
要包含在functions.php文件中的代码
function my_text_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Productos relacionados' :
$translated_text = __( 'Completá el look', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );
一个小PHP代码片段。将PHP代码片段放在子theme functions.PHP文件的底部:wp content/themes/mythemename/functions.PHP
add_filter( 'gettext', 'mythemename_translate_woocommerce_strings', 999, 3 );
function mythemename_translate_woocommerce_strings( $translated, $text, $domain ) {
$translated = str_ireplace( 'text EN', 'text translated PT_BR', $translated );
return $translated;
}
2020年更新
现在,您可以使用一个更精确的过滤器来代替建议的搜索和替换方法
将此代码段添加到子主题中的functions.php文件中
这似乎需要WooCommerce 3.9.0
/*-------------- Change related products text -------------*/
add_filter( 'woocommerce_product_related_products_heading', 'single_related_text' );
function single_related_text(){
return __('These products might be cool', 'woocommerce');
}
我刚刚在自己的一个网站上成功地使用了以下代码。它需要放在子主题的functions.php中。将“此处的自定义文本”替换为您希望使用的单词
add_filter( 'gettext', 'wtd_related_products_text', 20, 3 );
function wtd_related_products_text( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Related products' :
$translated_text = __( 'Your Custom Text Here', 'woocommerce' );
break;
}
return $translated_text;
}
我怀疑这就是答案@user3710926您不应该直接编辑WooCommerce核心文件;该特定文件应作为woocommerce/single product/related.php
复制到主题文件夹中。您维护插件模板文件夹中的文件夹层次结构。这是正确的解决方案,一个函数覆盖。不要更改主题,因为主题文件也可能会更新。这是正确的代码。如果您的站点是多语言的,则需要将switch($translated_text)替换为switch($text)HTML实体,并且可能需要在搜索字符串中匹配大小写敏感度…>大小写“您可能也喜欢&hellip;”:目前此功能不起作用。。我试过了,但没有使用“相关产品”,因为翻译是区分大小写的。谢谢,伙计,你让我度过了一个美好的夜晚。我试着用美国原版翻译,因为在函数编码中,它通常是关于美国原版术语的。但在这里,我也使用了我的法语“相关产品”术语。也就是说,我必须将重音字符改为非重音字符。法国的传统是“显而易见的产品”。但在代码的切换部分:case'Produits expectiveés'不起作用。你知道如何在php中使用重音字符串作为比较或切换条件来管理语句吗?