首页 > 解决方案 > 是否有检查 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()

标签: wordpresswordpress-plugin-creation

解决方案


看起来我会回答我自己的问题...由于 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;
    }
}

?>

这不是一个很好的答案,但它是目前最好的答案。


推荐阅读