Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the JavaScript Localization of WordPress

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to understand the JavaScript localization of WordPress, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

After upgrading WordPress to 3. 1, if the theme supports the wp_head () method, a file named l10n.js will be loaded on the page, which is the toollibrary for JavaScript localization. At present, it is a useless feature for most people, and some people even think that WordPress has despicably secretly buried a bullshit script on the page, so all you 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 is the intention of adding scripts? The editor will explain the significance of the existence of l10n.js scripts, the use of localized scripts and prospects for its future.

I18n and l10n

I18n [1] and l10n [2] are commonly used abbreviations in the field of IT, meaning internationalization and localization, respectively. I18n means that the application can provide localized services for users in different countries and locales, while l10n implements localization features in the user environment, while internationalization and localization generally refer to the multilingual implementation of the locale. For example, WordPress is a program that supports internationalization. As long as the corresponding country language pack is provided and the country code is established, the language environment of the whole website can be replaced.

JavaScript localization

As in the previous example, WordPress has always supported internationalization and provides _ e () and _ _ () two PHP methods 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, so it is necessary to introduce the internationalization and localization of JavaScript.

If you use a theme that supports the wp_head () method, the l10n.js file will be loaded on the page, and users can inject localized resources into the page through simple code. L10n.js is a JavaScript localized tool library that contains the convertEntities () method that restores the HTML object to its original value, so it is loaded before all injected JavaScript scripts and localized resources.

The method used

We can define localized resources for each JavaScript file to be loaded. The implementation code is as follows [3]:

/ / you must first load a Javascript file wp_enqueue_script ('some_handle',' / some_javascript.js'); / / assemble the localization data and use _ _ () to make the localization content $data = array ('some_string' = > _ _ (' Some string to translate')); / / load the localization script. You must specify a JavaScript file that needs to be loaded. Wp_localize_script ('some_handle',' object_name', $data)

After execution, the code is generated on the page as follows:

/ * /

Localized resources are loaded and defined before the script is specified. Where {ROOT DIR} is the root directory of the site.

At this point, if we can test with the following code:

Alert (object_name.some_string); / / pop-up dialog box showing 'Some string to translate''

Advantages and disadvantages and applications

The benefits mentioned above make it easy to localize the front end (with the back-end PHP required). The disadvantage is also obvious that localized resources are calculated each time the page is loaded, and a large amount of JavaScript code is loaded, increasing the system burden and prolonging the page loading time.

JavaScript localization is a troublesome part of making WordPress themes and plug-ins. Some plug-in developers will define l10n objects on their own pages. You can refer to the practice of WP-RecentComments plug-ins. The forced introduction of WordPress is likely to be widely used by theme and plug-in developers. In other words, you may not dare to uninstall it in the near future, otherwise some plug-ins will not work.

Methods of uninstalling and restoring

Although the l10n.js file is small, at least a request will be made, and if you really think it's useless, you can uninstall it by adding the following code to functions.php.

Wp_deregister_script ('l10n')

If the function name, this method is not disabled, but logged out. In other words, once you log out, it will not be restored even if the code is deleted. If you want to reply after logging out, you need to use the following methods:

Wp_register_script ('l10n'); the above is how to understand the JavaScript localization of WordPress. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report