Search code examples
phpwordpresswoocommercehook-woocommercewpml

Make text strings translatable in Wordpress or WooCommerce


I've taken over a dual language Woocommerce site, State/Counties have been modified with custom counties but they have not been translated.

How do I go about this

Here's the code from functions.php, it looks like it's an adaptation of code from BBloomers website.

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;
} 

Is it possible to replace these with strings so that I can use WPML to translate or do I need to hard code again in the functions file?


Solution

  • To make any text string translatable, you will need to use specific __() WordPress function, for each text string (state).

    Also in your code, the $states variable is already available as an argument from the function when using woocommerce_states so incorrect global $states; is not really needed (see this official code snippet).

    So your code will be (set your active theme's text domain inside the function):

    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.

    Related tread: Add states for shipping zones instead of postcodes in Woocommerce

    For info: Bloomers code is taken from official WooCommerce Add/Modify States in WooCommerce