商品页翻译做完,最容易漏的是变体下拉里那行“红 / Red”,和结账页上那句“Company name (optional)”。前者来自 term 表,后者来自 .mo 文件,中间还夹着后台手填的运费方式名。它们不在同一层,所以不会一起被翻译。
把 WooCommerce 商品页上的文字按来源拆开,每一类的处理方式都不一样。
商品标题、短描述、长描述存在 wp_posts 里。多语言有两种做法:每种语言复制一份商品,或者一套商品加一张翻译表。SKU 少、变体少的站,翻译表更省事。
给商品加 postmeta _i18n_title、_i18n_short_description、_i18n_description,值存成 array( 'zh' => '...', 'ja' => '...' ) 这样的数组,读取时按当前语言取。
function aum_current_lang(): string {
static $lang = null;
if ( null !== $lang ) {
return $lang;
}
$supported = [ 'en', 'zh', 'ja' ];
$candidate = '';
if ( ! empty( $_COOKIE['aum_lang'] ) ) {
$candidate = sanitize_key( wp_unslash( $_COOKIE['aum_lang'] ) );
} elseif ( preg_match( '#^/([a-z]{2})(/|$)#', $_SERVER['REQUEST_URI'] ?? '', $m ) ) {
$candidate = $m[1];
}
$lang = in_array( $candidate, $supported, true ) ? $candidate : 'en';
return $lang;
}
function aum_product_i18n( int $post_id, string $field ): string {
$lang = aum_current_lang();
if ( 'en' === $lang ) {
return '';
}
$map = get_post_meta( $post_id, '_i18n_' . $field, true );
return is_array( $map ) && isset( $map[ $lang ] ) ? (string) $map[ $lang ] : '';
}原文是英文,英文直接返回空串,让 WooCommerce 用原始字段。覆盖输出:
add_filter( 'the_title', function ( $title, $post_id ) {
if ( is_admin() || 'product' !== get_post_type( $post_id ) ) {
return $title;
}
$t = aum_product_i18n( (int) $post_id, 'title' );
return '' !== $t ? $t : $title;
}, 10, 2 );
add_filter( 'woocommerce_product_get_short_description', function ( $desc, $product ) {
$t = aum_product_i18n( $product->get_id(), 'short_description' );
return '' !== $t ? $t : $desc;
}, 10, 2 );
add_filter( 'woocommerce_product_get_description', function ( $desc, $product ) {
$t = aum_product_i18n( $product->get_id(), 'description' );
return '' !== $t ? $t : $desc;
}, 10, 2 );the_title 会影响面包屑、相关商品和结构化数据,这正是想要的。商品卡片如果直接读 $product->get_name(),走的是另一个 filter,再补一条:
add_filter( 'woocommerce_product_get_name', function ( $name, $product ) {
$t = aum_product_i18n( $product->get_id(), 'title' );
return '' !== $t ? $t : $name;
}, 10, 2 );is_admin() 判断要留着,否则后台商品列表里看到的也是译文,编辑时容易改错字段。
属性有两类。全局属性是 taxonomy,比如 pa_color,值存在 term 表里;本地属性存在商品的 _product_attributes postmeta 里,label 和值都是一串文本。
属性 label(页面上“颜色”那两个字)走 woocommerce_attribute_label:
add_filter( 'woocommerce_attribute_label', function ( $label, $name, $product ) {
if ( ! $product instanceof WC_Product ) {
return $label;
}
$lang = aum_current_lang();
if ( 'en' === $lang ) {
return $label;
}
$map = get_post_meta( $product->get_id(), '_i18n_attr_label', true );
return is_array( $map ) && ! empty( $map[ $name ][ $lang ] ) ? $map[ $name ][ $lang ] : $label;
}, 10, 3 );全局属性的值不要直接改 term 表,一个 term 只能有一个 name。给 term 加 termmeta _i18n_name,在输出时替换:
add_filter( 'get_term', function ( $term, $taxonomy ) {
if ( is_admin() || 0 !== strpos( $taxonomy, 'pa_' ) ) {
return $term;
}
$lang = aum_current_lang();
if ( 'en' === $lang ) {
return $term;
}
$map = get_term_meta( $term->term_id, '_i18n_name', true );
if ( is_array( $map ) && ! empty( $map[ $lang ] ) ) {
$term->name = $map[ $lang ];
}
return $term;
}, 10, 2 );商品页的“附加信息”表格是另一条路径,值由 WC_Product::get_attribute() 拼出来,没有针对单个值的 filter,要改整个数组:
add_filter( 'woocommerce_display_product_attributes', function ( $attributes, $product ) {
$lang = aum_current_lang();
if ( 'en' === $lang ) {
return $attributes;
}
$map = get_post_meta( $product->get_id(), '_i18n_attr_value', true );
if ( ! is_array( $map ) ) {
return $attributes;
}
foreach ( $attributes as $key => $data ) {
if ( ! empty( $map[ $key ][ $lang ] ) ) {
$attributes[ $key ]['value'] = wp_kses_post( $map[ $key ][ $lang ] );
}
}
return $attributes;
}, 10, 2 );本地属性的值没有 term 可挂,只能靠这张 postmeta 表。
每个变体是一个 product_variation post,属性组合存在 attribute_pa_color、attribute_size 这样的 postmeta 里。变体在下拉里显示的名字由属性值拼出来,属性值翻译了,变体名就跟着变,不用再处理一遍。
需要单独处理的是变体自己的 _variation_description 和图片 alt。变体的价格、SKU、库存、重量不翻译,但价格的显示格式要跟 locale 走:小数位、千分位、货币符号位置都由 wc_price() 按 locale 处理,切语言时要把 locale 一起切,否则德语站会显示成 1,234.00 这种格式。
变体下拉里每一项还有一条 filter,值不来自 term 的时候用得上:
add_filter( 'woocommerce_variation_option_name', function ( $name, $term, $attribute, $product ) {
if ( ! $product instanceof WC_Product ) {
return $name;
}
$lang = aum_current_lang();
if ( 'en' === $lang ) {
return $name;
}
$map = get_post_meta( $product->get_id(), '_i18n_variation_option', true );
return is_array( $map ) && ! empty( $map[ $attribute ][ $name ][ $lang ] )
? $map[ $attribute ][ $name ][ $lang ]
: $name;
}, 10, 4 );“Billing details”“Place order”、字段校验的错误提示,这些是 gettext 字符串,来自 WooCommerce 的翻译包和主题。正确做法是放一份 wp-content/languages/woocommerce/woocommerce-zh_CN.mo,用 gettext 工具链从 .po 编译出来。gettext 过滤器也能改,但每个字符串都要跑一遍回调,几十条以上就该换成 .mo。
小范围覆盖可以这样:
add_filter( 'gettext', function ( $translated, $original, $domain ) {
if ( 'woocommerce' !== $domain ) {
return $translated;
}
if ( 'zh' !== aum_current_lang() ) {
return $translated;
}
$overrides = [
'Billing details' => '账单信息',
'Place order' => '提交订单',
];
return $overrides[ $original ] ?? $translated;
}, 10, 3 );locale 要跟着语言切,否则 WooCommerce 加载的还是默认语言的 .mo:
add_filter( 'determine_locale', function ( $locale ) {
if ( is_admin() && ! wp_doing_ajax() ) {
return $locale;
}
$map = [ 'en' => 'en_US', 'zh' => 'zh_CN', 'ja' => 'ja' ];
$lang = aum_current_lang();
return $map[ $lang ] ?? $locale;
} );determine_locale 在翻译加载之前触发,比在模板里 switch_to_locale() 稳。switch_to_locale() 也能用,但要记得 restore_previous_locale(),不然同一请求里后面的输出会串语言。
运费方式的名称、支付方式的标题和描述、订单邮件的标题和正文、条款页的说明,这些是后台设置里填的,存在 wp_options 里,翻译包不会碰。三种处理办法:
运费方式那条最常被忘:
add_filter( 'woocommerce_package_rates', function ( $rates ) {
$lang = aum_current_lang();
if ( 'en' === $lang ) {
return $rates;
}
$map = [
'zh' => [ 'Flat rate' => '统一运费', 'Free shipping' => '免运费' ],
];
foreach ( $rates as $rate ) {
if ( ! empty( $map[ $lang ][ $rate->label ] ) ) {
$rate->label = $map[ $lang ][ $rate->label ];
}
}
return $rates;
} );改完之后要清一次购物车里的运费缓存,否则客户看到的还是旧 label。
邮件标题有专门的 filter,比如 woocommerce_email_heading_customer_processing_order;邮件正文是 option 里的 HTML,只能在发送前按语言取一份。
商品 schema 里的 name、description 如果还是原文,搜索引擎和 AI 抓到的是没翻译的版本。woocommerce_structured_data_product 可以在输出前改:
add_filter( 'woocommerce_structured_data_product', function ( $markup, $product ) {
$t = aum_product_i18n( $product->get_id(), 'title' );
if ( '' !== $t ) {
$markup['name'] = $t;
}
$d = aum_product_i18n( $product->get_id(), 'description' );
if ( '' !== $d ) {
$markup['description'] = wp_strip_all_tags( $d );
}
return $markup;
}, 10, 2 );hreflang 输出在 <head> 里,每种语言一个 <link rel="alternate" hreflang="zh" href="...">,x-default 指向默认语言。别只给首页输出,商品页也要,否则翻译过的页面在搜索结果里不会互相归组。
自己维护这套翻译表要写不少代码。我们做的免费插件 AumLang 走的是另一条路:AI 翻译、hreflang,商品页的标题、描述、属性都覆盖,插件页面在这里。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。