Localization with JavaScript in WordPress

Source: WP Engineer.com
Story flagged by: RominaZ

Creating Plugins and Theme functions with multilanguage capability has been established nowadays.  But there are some difficulties if you are using JavaScript, the question is how to provide it multilingual or provide option values in different languages. WordPress offers some possibilities shown below. The following little example should show the realization and the easy output of strings demonstrates it. Wether strings for multilanguage or providing options values, it doesn’t matter.

1.-First we create a function with an array that contains the required content and can be accessed on the relevant content using variables.

2.- When you call the extension, Plugin or Theme, the function must be loaded. For this task WordPress provides the functionwp_localize_script() that takes care of everything:

  • wp_localize_script(
  • ‘my_foo’,
  • ‘my_var_prefix’,
  • $this->get_language_strings() // inside class
  • );

3.- You can access the variables in the scripts:


  • alert( my_var_prefix.example );
  • No integration of wp-load.php or something like this, simply access the content. The strings are now translated into all languages using the classical methods and it applies only to create the appropriate files.

    See: WP Engineer.com

    Comments about this article



    Translation news
    Stay informed on what is happening in the industry, by sharing and discussing translation industry news stories.

    All of ProZ.com
    • All of ProZ.com
    • Term search
    • Jobs
    • Forums
    • Multiple search