Search code examples
phpwordpresswoocommercecountrywpml

WooCommerce, Code snippet and WPML: Allow translation from new country


I have added a new country to WooCommerce using Code snippet plugin with the following:

add_filter( 'woocommerce_countries',  'add_my_country' );
function add_my_country( $countries ) {
     $new_countries = array( 'ESTI'  => __( 'Estonia (islands)', 'woocommerce' ), );
     return array_merge( $countries, $new_countries );
}

add_filter( 'woocommerce_continents', 'add_my_country_to_continents' );
function add_my_country_to_continents( $continents ) {
    $continents['EU']['countries'][] = 'ESTI';
    return $continents;
} 

The code works fine.

Now I am using the WPML plugin for translation, but WPML does not see this new country string. Where am I missing something? How to translate "Estonia (islands)" to other languages?


Solution

  • Fist If you are saving the code in your child theme function.php file, you should change first the text domain name of your translatable strings to your child theme text domain, so change 'woocommerce' to your child theme text domain. Then in WPML rescan your child theme for new translatable strings. Then In "string translation" section you will see those strings.

    Now if it doesn't work, as you are using WPML plugin for translations and Code Snippet plugin for code customizations, you can use the WPML ICL_LANGUAGE_CODE constant to target a specific country code.

    Then the gettext hook should work for translations by country code like:

    add_filter( 'gettext', 'translate_some_custom_strings', 10000, 3 );
    function translate_some_custom_strings( $translate_text, $original_text, $domain ) {
        if ( defined( 'ICL_LANGUAGE_CODE' ) && 'Estonia (islands)' === $original_text ) {
            // For Estonia
            if ( 'ESTI' === ICL_LANGUAGE_CODE ) {
                $translate_text = 'Eesti (saared)';
            } 
            // For France
            elseif ( 'FR' === ICL_LANGUAGE_CODE ) {
                $translate_text = 'Estonie (îles)';
            }
        }
    
        return $translate_text;
    }
    

    Note: You may need to check that "ESTI" is a registered country code in WPML.

    Related WPML thread: [Resolved] I can’t see translation for Code Snippet plugin