Localization of JavaScript in WordPress

Time:2020-1-26

At present, it’s a useless function for most people, even some people think it’s WordPress’s insidious and insidious script on the page, so all we can find on the Internet is how to uninstall the script. Before we uninstall, should we first understand what kind of function this is, and what’s the intention of adding the script? This article will explain the l10n.js foot The significance of this existence, the use of localized scripts and its future prospects

Copy code

The code is as follows:

<script type=’text/javascript‘ src='{ROOT DIR}/wp-includes/js/l10n.js?ver=20101110’></script>

I18N and L10N
I18N [1] and L10N [2] are commonly used abbreviations in the field of it, which means internationalization and localization respectively. I18N refers to that applications can provide localized services for user groups in different countries and language environments, while L10N refers to the realization of localization features in the user environment, and internationalization and localization generally refer to the multilingual realization of language environment. For example, WordPress is a supporting country International program, as long as the corresponding national language package and the development of national code, can replace the language environment of the entire website
JavaScript localization
As the previous example, WordPress has always supported internationalization and provided two PHP methods ﹐ e() and ﹐ () to localize the program. However, with more and more front-end JavaScript applications, the internationalization of the back-end can not meet all the needs. It is necessary to introduce the internationalization and localization of JavaScript
If the theme you use supports the wp_head() method, the file l10n.js will be loaded on the page, and users can inject localization resources into the page through simple code. L10n.js is a JavaScript localization tool library, including the convertentities() method, which can restore the HTML object to the original value, so it will be used in all injected JavaScript scripts and localization resources Previously loaded
Method used
We can define localization resources for each JavaScript file to be loaded. The implementation code is as follows [3]:

Copy code

The code is as follows:

//You must first load a JavaScript file
wp_enqueue_script(‘some_handle’, ‘/some_javascript.js’);
//Assemble localization data, and use \
$data = array( ‘some_string’ => __( ‘Some string to translate’ ) );
//Load localization script. Here you must specify a JavaScript file to load
wp_localize_script( ‘some_handle’, ‘object_name’, $data );

After execution, the code will be generated on the page as follows:

Copy code

The code is as follows:

/* <![CDATA[ */
var object_name = {
some_string: “Some string to translate”
};
/* ]]> */
<script type=’text/javascript’ src='{ROOT DIR}/some_javascript.js?ver=3.1′></script>

Localization resources are loaded and defined before the specified script. Where {root dir} is the root directory of the site
At this time, if we can test through the following code:

Copy code

The code is as follows:

Alert (object_name. Some_string); / / a dialog box will pop up, displaying ‘some string to translate’

Advantages and disadvantages and application occasions
There are many advantages mentioned above, and it is convenient to localize the front-end (with the support of PHP at the back-end). The disadvantages are also obvious. Localization resources will be calculated each time the page is loaded, and a lot of JavaScript code will be loaded, which increases the system burden and prolongs the page loading time
JavaScript localization is a troublesome part of WordPress Theme and plug-in production. Some plug-in developers will define L10N objects on the page by themselves. You can refer to the practice of WP recentcomments plug-in. This forced introduction of WordPress is likely to be widely used by theme and plug-in developers. That is to say, you may not dare to uninstall it in the near future, otherwise some plug-in developers will The plug-in will not work
How to uninstall and restore
Although the file l10n.js is very small, it will send out a request, if you really think it is useless, you can add the following code in functions.php to unload it

Copy code

The code is as follows:

wp_deregister_script(‘l10n’);

For example, for function name, this method is not disabled but unregistered. That is to say, once you unregister, there will be no code, even if you delete this code, it will not be restored. If you want to reply after unregistering, you need to use the following methods:

Copy code

The code is as follows:

wp_register_script(‘l10n’);

summary
WordPress’s idea is very good, but this way of realization is how I can Tucao, hope that “support JavaScript localization” is not a necessary condition for subject audit. As the front end, I have a little bit of page cleanliness. I have already written off the script. In addition, I saw the WordPress 3.1 Admin Bar, I smiled calmly, wish WordPress more and more bloated.

annotation
  1. Because there are 18 letters between the letters I and N before and after the word, it is abbreviated as I18N
  2. Because there are 10 letters between the letters L and N before and after the word, it is abbreviated to L10N
  3. Code fromWordPress Codex – wp_localize_script.