google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Ajout de JavaScript pour WordPress efficacement avec JavaScript Localisation fonction Ce JavaScript tutorial nous fournit un guide tr?s d?taill? sur la localisation de la plate-forme WordPress, ? travers les instructions contenues dans cet article JavaScript tutorial, nous pouvons facilement g?rer transf?r? les fichiers JavaScript codes source/efficacit


Étiquette: JavaScript dans WordPress, WordPress ajouter du JavaScript, localisation JavaScript, localisation WordPress

Gratuit iPage hébergement Web pour la première année MOMENT



Si vous êtes toujours à la recherche d'un fournisseur d'hébergement Web fiable avec des tarifs abordables, pourquoi vous ne prenez pas un peu de temps pour essayer iPage, seulement avec $1.89/month, inclus $500+ Crédits supplémentaires gratuites pour le paiement de 24 mois ($45)?

Plus de 1.000.000 de clients + existisng peuvent pas avoir tort, vraiment vous n'êtes pas aussi! Plus important encore, lorsque vous enregistrez l'hébergement web à iPage grâce à notre lien, nous allons être heureux de renvoyer un plein remboursement. C'est génial! Vous devriez essayer iPage hébergement web GRATUITEMENT maintenant! Et contactez-nous pour tout ce que vous devez savoir sur iPage.
Essayez iPage GRATUIT première année MOMENT

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.
iPhoneKer.com
Save up to 630$ when buy new iPhone 15

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

https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language

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

AIVideo-App.com
Render creative video automatically with AI

JavaScript par jour


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web