未加星标

Internationalizing WordPress Projects: Updates With WordPress4.6

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Throughout this series, we've covered exactly what you need to do to internationalize your WordPress projects. If you've not read any of the previous posts, I recommend checking them out.

Though there have been some changes to how internationalization and localization work in WordPress 4.6, that doesn't mean the previous tutorials are irrelevant. It just means that the way you opt to distribute your plugins and their localizations will change.

And that's what we're going to be covering in this tutorial.

Before We Start

As mentioned, this tutorial is assuming that you're caught up on everything we've discussed so far in this series. This includes:

Understanding internationalization and why it's important. How to set up a development environment to work with the sample code. Understanding localization (and the difference between internationalization). How to use tools to begin translating your internationalized strings.

If none of the above makes any sense, then please review the series. If you feel confident that you can explain or use each of bullet points above, then you're ready to proceed.

Understanding the Changes

The nice thing about this tutorial is that it's more informative than theoretical (or practical, for that matter). That is, there won't be any code to demonstrate. It's just about disseminating information and making sure you know what to use and when.

Before we get too deep into the topic, let's back up a little bit and talk about the whole internationalization process of WordPress and how it works with loading localized files.

How Internationalization and Localization Work

The concepts of internationalization and localization are relatively easy to understand, but they're also easily confused (when I first started my work in WordPress, even I misused the terms).

Internationalization is the process of developing your plugin so it can easily be translated into other languages.

Thus, this is how we prepare all of the strings and human-readable text in our plugin that need to be translated for other locales.

Localization, then, is the act of actually translating the strings and bundling them into language packs that will then be loaded by WordPress depending on the locale that is set in the installation.

For example, let's say I build a plugin and I'm using American English, or in the en_US locale as you may see it written. Then you're going to see all of the text I've written in my language.

But what happens when someone who wants to use the plugin wants to translate it into Spanish? First, the person responsible will use a tool such as POEdit to provide the translations for each of the strings.

Then she or he will store these in the languages directory (or whatever directory is used). The file should be named based on the locale for which it's related. In this case, es_ES .


Internationalizing WordPress Projects: Updates With WordPress4.6

When the plugin is loaded in an installation of WordPress that's been installed on a machine that has the es_ES locale set as its default locale, the localization file will be loaded and will replace all of the translated strings with their Spanish equivalents.

And for a long time, this is how the internationalization process has worked. Furthermore, if you're opting to distribute plugins outside of the WordPress Plugin Repository, this is something that you will still need to do.

But what about for those plugins being distributed in the repository?

Just-in-Time Translations

In computer science, there's a concept of JIT (or just-in-time), and we often hear it referred to as "just-in-time" compilation .

In computing, just-in-time (JIT) compilation, also known as dynamic translation, is compilation done during execution of a program at run time rather than prior to execution.

In the latest release of WordPress, that isWordPress 4.6, internationalized plugins following a specific protocol are able to leverage the advantage of just-in-time localization. From the Make WordPress blog , these changes can be summarized as follows:

Since translations files are usually inside wp-content/languages, WordPress now scans that directory for available translations and automatically loads them if it encounters a text domain for the first time.

What does this mean for us developers? In short, it means that if we're distributing our work via the WordPress Plugin Repository, WordPress will first scan its library of translations to see if one exists for the current plugin and its locale. If it detects one, then it will use that one.

If it does not detect a translation, there one of two things can happen:

The plugin simply won't be localized. The plugin will use the localization file that's included with the plugin.

There are some caveats to this new approach, though:

We no longer have to call load_plugin_textdomain() in our WordPress 4.6-based plugins. If you're in the habit of using unload_textdomain() , you'll need to manually load the translations after the call if you want to use them again.

Though they are simple, I highly recommend reading the blog post in its entirety to understand how it works, its functionality, and how it applies to the work you're doing day to day.

Generally speaking, I find this feature incredibly nice. It gives us a chance to distribute plugins that may already have translations available and that can be loaded via WordPress.

That said, I don't think it's an excuse not to bundle the internationalized files. After all, if a language pack isn't found, then it will still need to be loaded from the languages directory.

And if the ultimate goal is to have the most resilient, robust plugins available, then we shouldn't rely on something that may not exist. Instead, let's hope for the best, but plan for the worst.

Conclusion

And with that, we've covered everything we can about how WordPress handles internationalization and localization for plugins that will be available in both the WordPress Plugin Repository and those you'll be distributing through your own channels.

If you're interested in learning more about WordPress from a development perspective, note that I exclusively work with WordPress and often write about it. You can catch all of my courses and tutorials onmy profile page, and you can follow me on my blog and/or Twitter at @tommcfarlin where I talk about software development in the context of WordPress.

As always, if you're looking for other utilities to help you build out your growing set of tools for WordPress or for examplecode to study and become more well-versed in WordPress, don't forget to see what we have available in Envato Market .

Don't hesitate to leave any questions or comments in the feed below, and I'll aim to respond to each of them.

Related Resources There are a number of resources listed below. Note that these come from the previous tutorials as well as what's been menti

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: WordJITTwitter
分页:12
转载请注明
本文标题:Internationalizing WordPress Projects: Updates With WordPress4.6
本站链接:http://www.codesec.net/view/484868.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(16)