通用API文档

多语言国际化

💡 云策文档标注

概述

本文档介绍了 WordPress 中国际化(i18n)的基本概念和重要性,旨在帮助开发者编写可翻译的代码。核心内容包括使用 WordPress i18n 函数包装字符串、设置文本域(Text Domain),以及针对插件、主题和 JavaScript 文件的具体国际化方法。

关键要点

  • 国际化(i18n)是使代码易于翻译的过程,而本地化(Localization)是实际翻译和适配字符串的过程。
  • 使用 WordPress i18n 函数(如 _e())包装字符串,并设置文本域以避免插件、主题和核心之间的字符串冲突。
  • 插件和主题的国际化设置略有不同,需参考各自手册。
  • 从 WordPress 5.0 开始,JavaScript 文件可使用与 PHP 相同的 i18n 函数,需声明 wp-i18n 依赖并调用 wp_set_script_translations() 加载翻译。
  • WordPress 5.0 之前,可使用 wp_localize_script() 函数国际化 JavaScript。
  • 建议遵循国际化指南,了解各 i18n 函数的用途、用法和最佳实践。

代码示例

// PHP 中使用 i18n 函数
_e('Edit post');
_e('Edit movie', 'my-plugin');

// JavaScript 中注册脚本并设置翻译依赖
wp_register_script(
    'my-script',
    plugins_url( 'js/my-script.js', FILE ),
    array( 'wp-i18n' ),
    '0.0.1'
);
wp_set_script_translations('my-script', 'my-plugin');

// JavaScript 中使用 i18n 函数
const { __, _x, _n, sprintf } = wp.i18n;
__( 'Hello World', 'my-plugin' );
_x( 'My Gutenberg block name', 'block name', 'my-plugin' );

注意事项

  • 确保仅在真正使用时加载脚本和翻译,以提高性能。
  • 如果插件或主题未托管在 WordPress.org,需自行创建翻译文件。
  • 参考国际化指南以正确使用 i18n 函数并遵循最佳实践。

📄 原文内容

What is internationalization?

Internationalization is the process of developing your application in a way it can easily be translated into other languages. Internationalization is often abbreviated as i18n (because there are 18 letters between the letters i and n).

Why is internationalization important?

WordPress is used all over the world, by people who speak many different languages. The strings in WordPress need to be coded in a special way so that they can be easily translated into other languages. As a developer, you may not be able to provide localization for all your users; however, a translator can successfully localize your code without needing to modify the source code itself.

While making your code translatable is called Internationalization, the act of translating it and adapting the strings to a specific location is called Localization. Read more about Localization in WordPress.

The basics

In order to make a string translatable, you have to wrap the original strings in a call to one of the WordPress i18n functions.

For example, the PHP function _e() echoes a translatable string:

 _e('Edit post'); 

You will find code like this all over WordPress core files. However, if you are internationalizing a theme or a plugin, there is another argument that all i18n functions take called Text Domain.

Text Domains set the domain your plugin or theme translations belong. This assures there is no conflict between strings in plugins, themes, and the WordPress core.

With a text-domain, the most basic call to a i18n function that will output a string would be like:

 _e('Edit movie', 'my-plugin'); 

Setting up your plugin and theme to i18n

Setting up i18n is slightly different for Plugins and Themes, therefore this information is detailed in each respective Handbook:

Internationalizing JavaScript

Since WordPress 5.0 it’s possible to internationalize JavaScript files using the same set of i18n functions used in PHP.

In order to be able to use i18n functions in your JavaScript code, you have to declare wp-i18n as a dependency on your script when registering or enqueueing it. For example:

wp_register_script(
     'my-script',
     plugins_url( 'js/my-script.js', FILE ),
     array( 'wp-i18n' ),
     '0.0.1'
 );

Now that you have added the dependency to your script, you can use i18n functions in it, however you still have to tell WordPress to load the translations.

You do this by calling wp_set_script_translations(). This function takes three arguments: the registered/enqueued script handle, the text domain, and optionally a path to the directory containing translation files. The latter is only needed if your plugin or theme is not hosted on WordPress.org, which provides these translation files automatically.

wp_set_script_translations('my-script', 'my-plugin');
For better performance, always make sure to enqueue your scripts and load their translations only when they are really used.

In your JavaScript code you will use i18n functions pretty much the same way you do in your PHP code:

const { __, _x, _n, sprintf } = wp.i18n;

// simple string
__( 'Hello World', 'my-plugin' );

// string with context
_x( 'My Gutenberg block name', 'block name', 'my-plugin' );

The available i18n for you to use in your JS code are (See internationalization functions for more details):

Notice that the wp-i18n package also includes the sprintf function. This is very useful to internationalize strings that have variables in it.

Now refer to the Internationalization Guidelines to learn how to use all these functions and the best practices on writing your strings.

If you are not hosting your plugin or theme on WordPress.org, you will need to create your translation files yourself. Check this post out to learn how to do this.

Internationalizing JavaScript before WP 5.0

Another way to internationalize your JavaScript files is to use the wp_localize_script() function.

With this function you can register translatable strings and have them available in your JavaScript to be used.

Please refer to the wp_localize_script() reference to learn how to use it.

Internationalization Guidelines

Now that you are ready to internationalize your application, read through the Internationalization Guidelines and learn what each i18n function is for, how to use them, and the best practices when writing your strings.