Skip to Content
使用 WordPress 构建网络应用程序,第 2 版
book

使用 WordPress 构建网络应用程序,第 2 版

by Brian Messenlehner, Jason Coleman
May 2025
Intermediate to advanced
546 pages
6h 39m
Chinese
O'Reilly Media, Inc.
Content preview from 使用 WordPress 构建网络应用程序,第 2 版

第 13 章 本地化 WordPress 应用程序 本地化 WordPress 应用程序

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

本地化(或国际化)是将应用程序翻译成不同地区和语言使用的过程。 在本章中,我们将讨论用于本地化应用程序、主题和插件的工具和方法。

备注

您有时会看到本地化缩写为l10n,国际化有时缩写为i18n。

您需要本地化您的应用程序吗?

网络应用程序市场日益全球化。 提供其他语言版本的应用程序可以成为一个强大的优势,帮助您在本地/语言市场的竞争中赢得市场份额,同时也有助于抵御其他地区/语言的竞争。

如果您计划在开源许可证下发布您的任何代码,首先将其本地化是增加参与您项目的开发人员数量的好方法。如果您的插件或主题进行了本地化,那么使用其他语言的开发者将更有可能直接为您的项目做出贡献,而不是通过分叉来让它在他们的语言中运行。

如果您计划发布商业插件或主题,将代码本地化可以增加潜在客户的数量。

如果您的目标市场仅限于美国,而且您没有任何拓展到其他地区或语言的近期计划,那么您可能不想花时间准备本地化代码。此外,请记住,每种语言或地区版本的应用程序都可能需要自己的托管、支持、客户服务和维护。对于许多企业来说,在应用程序的早期阶段承担这样的成本太高。另一方面,您会发现为本地化准备代码的基础工作(在_() 、_e() 或_x() 函数中封装字符串输出)非常简单,而且通常在本地化之外还有其他用途。

最后,需要注意的是,有时本地化不仅仅意味着翻译代码。如果您的代码与其他服务有接口,您需要确保这些服务能在不同地区使用,或者准备开发替代服务。例如,Paid Memberships Pro 插件的一个重要组件是与支付网关的集成。在本地化 Paid Memberships Pro 之前,Jason 确保该插件与国际支付网关集成良好。否则,人们虽然可以用自己的语言使用该插件,但却无法与他们所在地区的支付网关兼容。

WordPress 如何实现本地化

WordPress 使用为 GNU 翻译项目开发的gettext 翻译系统。WordPress 中的 gettext 系统包括以下组件:

  • 定义地域/语言的方法

  • 定义文本域的方法

  • 翻译代码中字符串的方法

  • .pot文件包含所有要翻译的单词和短语

  • 每种语言的.po文件包含翻译内容

  • 每种语言的.mo文件包含.po翻译的编译版本

您的翻译工作必须具备上述每个组成部分。下文将详细解释每个步骤。最后,您将拥有创建本地化插件和翻译本地文件所需的所有工具。

在 WordPress 中定义您的地域

WPLANG 要在 WordPress 中定义您的地域,只需在wp-config.php文件中设置 常量即可:

<?php
// use the Spanish/Spain locale and language files.
define('WPLANG', 'es_ES');
?>
备注

使用 "locale "一词代替 "language",是因为同一种语言可以有多种翻译。例如,英式英语不同于美式英语。墨西哥西班牙语也不同于西班牙的西班牙语。

文本域

gettext 规范使用文本域来组织翻译表。在 WordPress 中,这意味着每个插件和主题都应该有自己独特的文本域。

从技术上讲,您可以使用任何文本域,只要它是唯一的、一致的,并使用正确的语法(全部小写,使用破折号但不使用下划线)。在实践中,插件或主题的文本域应该与标题相匹配,因为大多数其他插件和工具都希望如此。例如,WordPress.org ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

WordPress 2: Visual QuickStart Guide

WordPress 2: Visual QuickStart Guide

Maria Langer, Miraz Jordan
Web Marketing all-in-one for Dummies®

Web Marketing all-in-one for Dummies®

Ian Lurie, Elizabeth Marsten, Michael Becker, John Arnold, Marty Dickinson
How to Develop a Great Digital Strategy

How to Develop a Great Digital Strategy

Jeanne W. Ross, Cynthia M. Beath, Ina M. Sebastian

Publisher Resources

ISBN: 9798341659339