Title: String translation using JavaScript doesn’t work
Last modified: August 19, 2024

---

# String translation using JavaScript doesn’t work

 *  Resolved [arnauhi](https://wordpress.org/support/users/arnauhi/)
 * (@arnauhi)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/string-translation-using-javascript-doesnt-work/)
 * Hi,
 * It seems that you have not taken into account the translations of message strings
   using JavaScript.
   In a “.js” file I use the function “__(‘Back’, ‘hitemplate’)”
   but this text does not appear in the list of translations. I have also tried 
   to put a text that I am using in the PHP part, but it does not translate it either.
   For example, “__(‘Close’, ‘hitemplate’);” shows the translated text from PHP,
   but from JS it always shows “Close”.
 * Am I making any mistakes? Is it possible to make it work?
 * Thanks in advance for your help

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [vnoben](https://wordpress.org/support/users/vnoben/)
 * (@vnoben)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/string-translation-using-javascript-doesnt-work/#post-17976910)
 * [@arnauhi](https://wordpress.org/support/users/arnauhi/) are you suggesting that
   you are using a PHP function like `__( "string", "domain")` directly in your 
   javascript file? That will obviously not work as those are 2 different coding
   languages. The `__()` and related i18n functions are core WordPress functions
   and are not added by this plugin, even though the plugin will register strings
   that are shown with those functions.
   To use translated strings in JS you must
   use `wp_localize_script`. Documentation and examples can be found here: [https://developer.wordpress.org/reference/functions/wp_localize_script/](https://developer.wordpress.org/reference/functions/wp_localize_script/)
   It basically allows you to echo an array off string translations as a javascript
   object (for example “translations”) that you then can use in as you please. For
   example `translations.close_button_string`
    -  This reply was modified 1 year, 9 months ago by [vnoben](https://wordpress.org/support/users/vnoben/).
 *  Thread Starter [arnauhi](https://wordpress.org/support/users/arnauhi/)
 * (@arnauhi)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/string-translation-using-javascript-doesnt-work/#post-17977660)
 * Hi [@vnoben](https://wordpress.org/support/users/vnoben/),
 * I’ve tried what you suggested and I’ve been able to get it working.
 * Anyway, keep in mind that, since 2018 (in version 5.0 of WordJPress) the use 
   of i18n in JS has been made compatible: [https://make.wordpress.org/core/2018/11/09/new-javascript-i18n-support-in-wordpress/](https://make.wordpress.org/core/2018/11/09/new-javascript-i18n-support-in-wordpress/)
 * Thanks for your help.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘String translation using JavaScript doesn’t work’ is closed to new replies.

 * ![](https://ps.w.org/theme-translation-for-polylang/assets/icon-256x256.png?rev
   =1431912)
 * [Theme and plugin translation for Polylang (TTfP)](https://wordpress.org/plugins/theme-translation-for-polylang/)
 * [Support Threads](https://wordpress.org/support/plugin/theme-translation-for-polylang/)
 * [Active Topics](https://wordpress.org/support/plugin/theme-translation-for-polylang/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/theme-translation-for-polylang/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/theme-translation-for-polylang/reviews/)

## Tags

 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [string translation](https://wordpress.org/support/topic-tag/string-translation/)

 * 2 replies
 * 2 participants
 * Last reply from: [arnauhi](https://wordpress.org/support/users/arnauhi/)
 * Last activity: [1 year, 9 months ago](https://wordpress.org/support/topic/string-translation-using-javascript-doesnt-work/#post-17977660)
 * Status: resolved