使文本字符串在 Wordpress 或 WooCommerce 中可翻译

Make text strings translatable in Wordpress or WooCommerce

我已经接管了一个双语 Woocommerce 网站,State/Counties 已经修改了自定义国家,但尚未翻译。

我该怎么做

这是来自 functions.php 的代码,它看起来像是从 BBloomers 网站改编的代码。

add_filter( 'woocommerce_states', 'custom_woocommerce_states' ); 
function bbloomer_custom_woocommerce_states() {
     global $states;

     $states['IE'] = array( 
        'AN' => 'Antrim',
        'AR' => 'Armagh',
        'CE' => 'Clare',
        'CN' => 'Cavan',
        'CO' => 'Cork',
        'CW' => 'Carlow',
        'D]' => 'Dublin',
        'DL' => 'Donegal',
        'DW' => 'Down',
        'FE' => 'Fermanagh',
        'G]' => 'Galway',
        'KE' => 'Kildare',
        'KK' => 'Kilkenny',
        'KY' => 'Kerry',
        'LD' => 'Longford',
        'LH' => 'Louth',
        'LK' => 'Limerick',
        'LM' => 'Leitrim',
        'LO' => 'Derry',
        'LS' => 'Laois',
        'MH' => 'Meath',
        'MN' => 'Monaghan',
        'MO' => 'Mayo',
        'OY' => 'Offaly',
        'RN' => 'Roscommon',
        'SO' => 'Sligo',
        'TY' => 'Tipperary',
        'TE' => 'Tyrone',
        'WD' => 'Waterford',
        'WH' => 'Westmeath',
        'WW' => 'Wicklow',
        'WX' => 'Wexford',
    );
    return $states;
} 

是否可以用字符串替换它们以便我可以使用 WPML 进行翻译,或者我是否需要在函数文件中再次进行硬编码?

要使任何文本字符串可翻译,您需要为每个文本字符串(状态)使用特定的 __() WordPress function

同样在您的代码中,$states 变量在使用 woocommerce_states 时已作为函数的参数提供,因此实际上并不需要不正确的 global $states;(请参阅此 official code snippet).

因此您的代码将是 (在函数内设置活动主题的文本域):

add_filter( 'woocommerce_states', 'custom_ireland_country_states' ); 
function custom_ireland_country_states( $states ) {
     $domain = 'your-theme-domain-name'; // Here set your active theme's domain name

     $states['IE'] = array( 
        'AN' => __("Antrim", $domain ),
        'AR' => __("Armagh", $domain ),
        'CE' => __("Clare", $domain ),
        'CN' => __("Cavan", $domain ),
        'CO' => __("Cork", $domain ),
        'CW' => __("Carlow", $domain ),
        'D]' => __("Dublin", $domain ),
        'DL' => __("Donegal", $domain ),
        'DW' => __("Down", $domain ),
        'FE' => __("Fermanagh", $domain ),
        'G]' => __("Galway", $domain ),
        'KE' => __("Kildare", $domain ),
        'KK' => __("Kilkenny", $domain ),
        'KY' => __("Kerry", $domain ),
        'LD' => __("Longford", $domain ),
        'LH' => __("Louth", $domain ),
        'LK' => __("Limerick", $domain ),
        'LM' => __("Leitrim", $domain ),
        'LO' => __("Derry", $domain ),
        'LS' => __("Laois", $domain ),
        'MH' => __("Meath", $domain ),
        'MN' => __("Monaghan", $domain ),
        'MO' => __("Mayo", $domain ),
        'OY' => __("Offaly", $domain ),
        'RN' => __("Roscommon", $domain ),
        'SO' => __("Sligo", $domain ),
        'TY' => __("Tipperary", $domain ),
        'TE' => __("Tyrone", $domain ),
        'WD' => __("Waterford", $domain ),
        'WH' => __("Westmeath", $domain ),
        'WW' => __("Wicklow", $domain ),
        'WX' => __("Wexford", $domain ),
    );
    return $states;
} 

On WPML: Rescan your active theme's files. Then in WPML "string translations" section, each state will appear in the translatable strings list.


To be read absolutely for translations in Wordpress related: I18n for WordPress Developers
Everything is explained with all different available functions and code examples.

相关胎面:

信息: Bloomers 代码取自官方 WooCommerce Add/Modify States in WooCommerce


我还建议通读 http://ottopress.com/2012/internationalization-youre-probably-doing-it-wrong/

这里转载的很简单:

  • 不要在任何翻译函数中使用变量,包括域。
  • 翻译短语而不是单词。
  • 必要时消除歧义。
  • 翻译最少 HTML。

这是一篇较旧的文章,但有助于解释翻译的原因和目的。