wordpress - 是否有检查 WordPress 中是否存在翻译的功能?
问题描述
我开发了一个包含 400 多个单词和短语的插件。有一些短语对插件的功能非常重要,但很难完成所有翻译的 90% [在 wordpress.org] 以确保设置了必要的 20 个。
我需要检查当前是否存在插件的翻译,否则会为有限数量的语言提供后备。
例子:
<?php
if (wp_current_translation_exists())
{
$local_phrase = __('Some word', 'text-domain');
}
else
{
$language_code = substr(WP_LANG, 0, 2);
switch ($language_code)
{
case 'fr':
$local_phrase = 'Un mot';
break;
case 'de':
$local_phrase = 'Ein Wort';
break;
default:
$local_phrase = __('Some word', 'text-domain');
break;
}
}
?>
缺少的函数显示为:wp_current_translation_exists()
。
解决方案
看起来我会回答我自己的问题...由于 WordPress 默认为英语,我可以假设此翻译存在,并使用因语言而异的单词查看其他人。
<?php
$language_code = strtolower(substr(WP_LANG, 0, 2));
function wp_current_translation_exists($test_word = NULL, $text_domain = NULL)
{
global $language_code;
if ($test_word == NULL)
{
$test_word = 'Welcome';
}
return ($language_code == 'en' || __($test_word, $text_domain) != $test_word);
}
if (wp_current_translation_exists('Welcome', 'text-domain'))
{
$local_phrase = __('Some word', 'text-domain');
}
else
{
switch ($language_code)
{
case 'fr':
$local_phrase = 'Un mot';
break;
case 'de':
$local_phrase = 'Ein Wort';
break;
default:
$local_phrase = __('Some word', 'text-domain');
break;
}
}
?>
这不是一个很好的答案,但它是目前最好的答案。
推荐阅读
- snowflake-cloud-data-platform - 数据推送到 Snowflake 阶段时触发 Snowflake 任务
- python - Python-VLC,选择特定显示
- python - 使用回调响应从烧瓶调用 api 服务
- c - 为什么 NewLib 中的系统调用存根仅在 Linux 上需要,而在 Windows 上不需要?
- typescript - 从 Typescript 中的类型推断文字值
- class - (OOP) 在类中实现预编程的功能
- c# - 有没有办法使用 NetTopologySuite 和 MS SQL Server 在笛卡尔系统中保存坐标?
- angular - 批量更新作为集合操作
- python-3.x - 理解 Python3 中的列表
- database - 我们可以在oracle过程中接受参数的同时使用函数吗