twitter@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

G�n�rez vos vid�os d'entreprise par l'IA avec la voix ou simplement du texte



Votre premi�re application vid�o AI GRATUITE ! Automatisez votre premi�re vid�o AI. Cr�ez votre vid�o professionnelle en 5 minutes gr�ce � l'IA. Aucun �quipement ni comp�tence en montage vid�o requis. Production vid�o sans effort pour les sp�cialistes du marketing de contenu.
Application vid�o AI ILLIMIT�E gratuite � port�e de main

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.

Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web