google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Chèn JavaScript vào WordPress hiệu quả hơn với tính năng định vị hóa Bài viết này sẽ cung cấp cho chúng ta một hướng dẫn chi tiết về tính năng localization (định vị hóa) trong nền tảng WordPress, qua đó chúng ta có thể quản lí các tập tin/mã nguồn JavaScript được thêm vào hiệu quả hơn.


Nhãn: chèn JavaScript vào WordPress, định vị hóa, tính năng localization, nền tảng WordPress

Miễn phí web hosting 1 năm đầu tại iPage



Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?

Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.

Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
Thử iPage miễn phí cho năm đầu tiên NGAY

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 theo ngày


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web