【发布时间】:2013-09-21 00:38:43
【问题描述】:
如何使用 WordPress 中的本地化机制来访问现有但不是当前的语言字符串?
背景:我有一个自定义主题,我使用区域设置“en_US”作为默认区域设置,并通过 PO 文件转换为区域设置“es_ES”(西班牙语)。
假设我使用了结构
__('Introduction', 'my_domain');
在我的代码中,并且我已在我的 PO 文件中将“Introduction”翻译成西班牙语“Introducción”。这一切都很好。
现在的问题是:我想在我的数据库中插入 n 条记录,其中包含字符串“简介”的所有现有翻译——每种语言一个;因此,在我的示例中,n = 2。
理想情况下,我会这样写:
$site_id = 123;
// Get an array of all defined locales: ['en_US', 'es_ES']
$locales = util::get_all_locales();
// Add one new record in table details for each locale with the translated target string
foreach ($locales as $locale) {
db::insert_details($site_id, 'intro',
__('Introduction', 'my_domain', $locale), $locale);
}
只是,上面 __() 中的第三个参数是我纯粹的幻想。你只能有效地写
__('Introduction', 'my_domain');
根据当前语言环境获取“介绍”或“介绍”。
理想情况下,上面代码的结果是我的表中有两条记录:
SITE_ID CAT TEXT LOCALE
123 intro Introduction en_US
123 intro Introducción es_ES
我知道我想要一些需要加载所有 MO 文件的东西,通常只需要当前语言的 MO 文件。也许使用 WordPress 函数 load_textdomain 是必要的——我只是希望已经存在一个解决方案。
通过包含插件 PolyLang 扩展问题:是否可以使用 Custom Strings 来实现上述功能?例如。概念上:
pll_('Introduction', $locale)
【问题讨论】: