google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






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

Generate your business videos by AI with voice or just text



Your first FREE AI Video App! Automate Your First AI Video. Create Your Professional Video In 5 Minutes By AI No Equipment Or Video Editing Skill Requred. Effortless Video Production For Content Marketers.
Free UNLMITED AI Video App in Your Hand

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.
AIVideo-App.com
Generate your business videos by AI with voice or just text

chatGPTaz.com
Talk to ChatGPT by your mother language

AppAIVideo
Your first FREE AI Video App

Deepfake Video
Deepfake AI Video Maker

Deepfake
Deepfake AI Video Maker

AI Deep Fake
Deepfake AI Video Maker

AIvidio
AI Video Mobile Solutions

AIvideos
AI Video Platform & Solutions

AIvedio
AI Video App Maker

Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)

Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Powerful AI Presentation PPT Maker for FREE
Build an impressive presentation with our free online AI presentation app

Your next top AI Assistant
Claude AI, developed by Anthropic

Your next top AI Assistant
Claude AI, developed by Anthropic

Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program

Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing

Dall-E-OpenAI.com
Generate creative images automatically with AI

chatGPT4.win
Talk to ChatGPT by your mother language

First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language

Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

iPhoneKer.com
Save up to 630$ when buy new iPhone 16

Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k

JavaScript by day


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web