首页 > 解决方案 > 在 php 中准备好可变文本字段翻译

问题描述

我在后端有一个文本字段。里面的文字值会根据我选择的产品而有所不同。所以,它是一种动态场。

在此处输入图像描述

如何定位该字段以使其可翻译?所以每次我创建一个offer WPML 可以检测到字符串吗?

if ( $total_to_add - $total_added > 0 ) {
    if ( ! in_array( $rule['key'], $special_offers, true ) ) {
        $rule_text = isset( $rule['text_in_modal_special_offer'] ) ? $rule['text_in_modal_special_offer'] : __( 'Get a special discount if you add {{total_to_add}} product(s) to your order.', 'discountText' );
        $special_offer = array(
            'text'     => $rule_text,
            'items_in_cart' => $total_added,
            'discount' => array(
                'type' => $discount['discount_amount']['type'],
                'amount' => $discount['discount_amount']['amount'],
             ),
         );
         $special_offers[ $rule['key'] ] = $special_offer;
     }
}

标签: phpwordpresswpml

解决方案


您需要使用 WPML(文档)的“自动注册字符串进行翻译”功能:

WPML 使用静态代码分析来定位主题和插件中需要翻译的字符串。在某些情况下,静态代码扫描无法可靠地找到所有字符串。当使用代码动态生成字符串时,通常会发生这种情况。

然后,您需要转到站点的前端并确保执行 IF 语句的两个变体。另外,我也会尝试__()动态值:

$rule_text = isset( $rule['text_in_modal_special_offer'] ) ? __($rule['text_in_modal_special_offer'], 'discountText') : __( 'Get a special discount if you add {{total_to_add}} product(s) to your order.', 'discountText' );

之后,转到WPML > String Translation,搜索两个字符串并翻译它们。


推荐阅读