如何使用 WordPress 中的本地化机制来访问现有但不是当前的语言字符串?
\n\n背景:我有一个自定义主题,我使用语言环境 \'en_US\' 作为默认语言环境,并通过 PO 文件转换为语言环境 \'es_ES\' (西班牙语)。
\n\n假设我使用该结构
\n\n__(\'Introduction\', \'my_domain\');\nRun 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}\nRun Code Online (Sandbox Code Playgroud)\n\n只是,上面 __() 中的第三个参数对我来说纯粹是幻想。你只能有效地写
\n\n__(\'简介\', \'my_domain\');
\n\n根据当前区域设置获取“Introduction”或“Introducci\xc3\xb3n\”。 …