小编fri*_*gel的帖子

获取特定区域设置的本地化字符串

如何使用 WordPress 中的本地化机制来访问现有但不是当前的语言字符串?

\n\n

背景:我有一个自定义主题,我使用语言环境 \'en_US\' 作为默认语言环境,并通过 PO 文件转换为语言环境 \'es_ES\' (西班牙语)。

\n\n

假设我使用该结构

\n\n
__(\'Introduction\', \'my_domain\');\n
Run Code Online (Sandbox Code Playgroud)\n\n

在我的代码中,并且我已将 PO 文件中的“Introduction”翻译为西班牙语“Introducci\xc3\xb3n\xc2\xb4”。这一切都运行良好。

\n\n

现在问题是:我想在数据库中插入 n 条记录,其中包含字符串“Introduction”的所有现有翻译 - 每种语言一个;因此,在我的示例中,n = 2。

\n\n

理想情况下,我会写这样的东西:

\n\n
$site_id = 123;\n// Get an array of all defined locales: [\'en_US\', \'es_ES\']\n$locales = util::get_all_locales();\n// Add one new record in table details for each locale with the translated target string\nforeach ($locales as $locale) {\n    db::insert_details($site_id, \'intro\',\n        __(\'Introduction\', \'my_domain\', $locale), $locale);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

只是,上面 __() 中的第三个参数对我来说纯粹是幻想。你只能有效地写

\n\n

__(\'简介\', \'my_domain\');

\n\n

根据当前区域设置获取“Introduction”或“Introducci\xc3\xb3n\”。 …

wordpress localization

5
推荐指数
1
解决办法
2449
查看次数

标签 统计

localization ×1

wordpress ×1