通用API文档

国际化函数

💡 云策文档标注

概述

本文档介绍了 WordPress 国际化(i18n)的核心函数,包括基本翻译函数、转义函数、日期和数字函数,以及在 JavaScript 中可用的函数。开发者应参考国际化指南以了解每个函数的用途、用法和最佳实践。

关键要点

  • 基本函数包括 __()、_e()、_x()、_ex()、_n()、_nx()、_n_noop()、_nx_noop() 和 translate_nooped_plural(),用于字符串翻译和复数处理。
  • 转义函数如 esc_html__()、esc_html_e()、esc_html_x()、esc_attr__()、esc_attr_e() 和 esc_attr_x(),确保在 HTML 属性中使用的翻译字符串被安全转义。
  • 日期和数字函数包括 number_format_i18n() 和 date_i18n(),用于本地化格式。
  • JavaScript 中可用的函数有 __()、_x()、_n()、_nx() 和 sprintf(),但需设置插件或主题的 JavaScript 本地化。

注意事项

  • 使用转义函数时,确保在 HTML 属性中的翻译字符串被正确转义,以防止安全漏洞。
  • 在 JavaScript 中使用国际化函数前,必须配置插件或主题的 JavaScript 本地化设置。

📄 原文内容

Check the Internationalization Guidelines and learn what each i18n function is for, how to use them, and the best practices when writing your strings.

Basic functions

Translate & Escape functions

Strings that require translation and is used in attributes of html tags must be escaped.

Date and number functions

Functions also available in javascript

Note: To be able to use these functions available in your javascript, you have to set up your plugin/theme javascript localization.