google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank
Guest, register






Adding JavaScript to WordPress Effectively with JavaScript Localization feature This JavaScript article tutorial provides us a very detailed guide about localization in WordPress platform; throughout the instructions within this JavaScript article tutorial, we can easily manage uploaded JavaScript files/source codes effectively.


Label: JavaScript in WordPress, WordPress add JavaScript, JavaScript localization, WordPress localization

Free iPage Web Hosting for First Year NOW



If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?

Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
Try iPage for FREE First Year NOW

Other Localization Techniques

While the wp_localize_script function does great work, it has one inherent flaw: each localized string is on a new line. For plugins that require a lot of localized strings, the size of the page source can easily balloon to unacceptable levels.

To remedy this, we can use two additional localization techniques: one uses JSON, and the other is a custom function.

The JSON Technique

The JSON Technique uses WordPress' built-in JSON class in order to parse our localized variables.

We would use the same localize_vars function, but would modify the way we queue our scripts.

First, let's create a helper function that will instantiate the JSON class and spit out our localized variables to screen.


<?php
function js_localize($name, $vars) {

    ?>
    <script type='text/javascript'>
    /* <![CDATA[ */
    var <?php echo $name; ?> = 

    <?php 
    require_once(ABSPATH . '/wp-includes/class-json.php');
        $wp_json = new Services_JSON();

        echo stripslashes($wp_json->encodeUnsafe($vars)); 
    ?>;

    /* ]]> */
    </script>
<?php
}
?>

The js_localize function takes in a $name (our object name) and an array of our localized variables ($vars).

The function then instantiates the JSON class and encodes the variables for output.

Here's how the code would look when queueing up your scripts:


<?php 
js_localize('my_unique_name', localize_vars());

wp_enqueue_script('my_script', plugins_url('your-plugin-name') . '/my_script.js', array('jquery'), '1.0.0');

?>

Please note that the js_localize function is run before the script is queued.

While this technique does eliminate the newlines and creates cleaner source code, it does have one major flaw. It doesn't work for all languages.

For example, the Turkish language causes the above technique to crash and burn.

However, if you don't plan on having additional languages and want localization purely for the ability to access the JavaScript variables, then I would recommend this technique.

A Custom Function

For those wanting to eliminate the newlines caused by wp_localize_scripts, and still have the ability to handle complex languages, then a custom function will have to suffice.

We'll use the same exact code to queue our scripts, but the js_localize function will change a bit.

My technique is to iterate through the localized variables, save them to an array, and output the array to screen.


<?php
function js_localize($name, $vars) {

    $data = "var $name = {";
    $arr = array();

    foreach ($vars as $key => $value) {
        $arr[count($arr)] = $key . " : '" . esc_js($value) . "'";

    }
    $data .= implode(",",$arr);
    $data .= "};";

    echo "<script type='text/javascript'>\n";
    echo "/* <![CDATA[ */\n";
    echo $data;

    echo "\n/* ]]> */\n";
    echo "</script>\n";

}
?>

It might not be the most poetic thing you've ever seen, but it works pretty well, even for those complex languages.

Localization Conclusion

Within this article you learned the how and the why of JavaScript localization.

The benefits of localizing your JavaScript are:

  • No need to embed JavaScript and PHP.
  • Can capture PHP variables without having to load the WordPress environment.
  • Can enable others to translate your JavaScript strings.

You also learned three different techniques to achieve localization.

  • Using wp_localize_script - Recommended for general use.
  • Using JSON - Recommended for non-complex localization and performance.
  • Using a Custom Function - Recommended for complex localization and performance.

JavaScript by day


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web