php - 在 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;
}
}
解决方案
您需要使用 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,搜索两个字符串并翻译它们。
推荐阅读
- php - 将自己的类代码放在单独的文件中后,管理类不可用
- google-sheets - 在 Google 表格中按字母顺序排列/排序
- python - Straithforward if 语句得到 ValueError:Series 的真值不明确
- jenkins-groovy - Jenkins Spock 测试因 java.lang.NoSuchMethodError 失败:com.google.common.collect.Sets
- python - Groupby,移位和前向填充
- java - Phaser 作为 CountdownLatch 的替代品
- jmeter - 如何在 JMeter 的 Flexible File Writer 插件的 Filename 字段中传递参数化变量值?
- java - java中减去两个浮点值的奇怪结果的问题
- python - 无法执行 ColumnTransformer 功能
- c# - C#调用matlab函数,结果总是一样