Search code examples
javascriptwordpresswpml

How can i translate html text that in javascript using WPML


The issues are: i want to translate words that are in javascript .i am using WPML plugin in wordpress. text within HTML tags are not showing in WPML strings

let html = `<div attrib class="applyOrCallLinkWrap byjs my-5">
            <div class="applyBtnWrap d-inline-block">
                <a href="/apply-now/" class="btn applyBtn orangeGrdBg thickRounded">
                    <span>**Apply Now**</span>
                    <i class="fas fa-arrow-right"></i>
                </a>
            </div>
            <div class="spratr d-inline-block">
                <span class="">**or**</>
            </div>
            <div class="callNowTxtWrap d-inline-block">
                **Call** <a href="${phoneLink}" class="header-phone-number-link header-phone-number">${phoneTxt}</a> **for instant approval** <sup>1</sup>
            </div>
        </div>`;

Solution

  • As you mentioned in your comment, you need to add all strings to the array and pass the array to your JS using wp_localize_script():

    $translation_array = array(
        'apply_now' => __( 'Apply Now', 'mytheme' ),
    );
    
    wp_localize_script( 'maxcashtheme-script', 'object_name', $translation_array );
    

    Then in your JavaScript use the object and the key:

    <span>${object_name.apply_now}</span>
    

    Finally, you need to scan your theme in wp-admin WPML > Theme and plugins localization > Strings in the themes. After that the string should show up for translation in WPML > String Translation.