2

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>`;
Abdul Rehman
  • 119
  • 1
  • 1
  • 7
  • 2
    I recommend you have a look at [wp_localize_script](https://developer.wordpress.org/reference/functions/wp_localize_script/) this allows you to create a localization object in WordPress – Jasper B Mar 02 '21 at 12:13
  • I have done that , But strings aren't show in WPML--> String Translation . – Abdul Rehman Mar 02 '21 at 14:12
  • ```$translation_array = array( 'apply_now' => __( 'Apply Now', 'mytheme' ), ); wp_localize_script( 'maxcashtheme-script', 'object_name', $translation_array );``` – Abdul Rehman Mar 02 '21 at 14:14

1 Answers1

2

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.

montrealist
  • 5,593
  • 12
  • 46
  • 68