Polylang:如何翻译自定义字符串?

Polylang: How to translate custom strings?

我的问题:我正在使用 Polylang 翻译我的网站,但我很难进行自定义字符串翻译。字符串不会显示在 WP 仪表板的 "Strings translation" 菜单中。

重要提示:我对PHP了解不多所以pll_register_string函数很混乱为了我。

引用自 Polylang 文档:

https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/

pll_register_string

Allows plugins to add their own strings in the “strings translation” panel. The function must be called on admin side (the functions.php file is OK for themes). It is possible to register empty strings (for example when they come from options) but they won’t appear in the list table.

Usage:

pll_register_string($name, $string, $group, $multiline); ‘$name’ => (required) name provided for sorting convenience (ex: ‘myplugin’) ‘$string’ => (required) the string to translate ‘$group’ => (optional) the group in which the string is registered, defaults to ‘polylang’ ‘$multiline’ => (optional) if set to true, the translation text field will be multiline, defaults to false

pll__

translates a string previously registered with pll_register_string Usage:

pll__($string); The unique parameter is required:

‘$string’ => the string to translate returns the translated string.

pll_e

Echoes a translated string previously registered with pll_register_string Usage:

pll_e($string); The unique parameter is required:

‘$string’ => the string to transla

此致

您必须先注册所有这些字符串才能进行翻译。

例如,您在某些模板文件中回显 "Hello world",如下所示:

<?php pll_e('Hello world'); ?>

要在 "Strings translation" 中显示字符串,请添加 functions.php:

add_action('init', function() {
  pll_register_string('mytheme-hello', 'Hello world');
});

将要翻译的所有自定义字符串添加到此函数。

正如 Polylang docs 所说,最好先检查 polylang 函数是否存在 - 这样网站就不会在 Polylang 插件更新时中断 - 因为它会先删除旧文件。

所以我提出这个方法: 在 functions.php 对于插件文件中的主题,您可以为所需的 Polylang 函数创建包装器,如果 polylang 被删除或更新,则 WP 不会因未定义的函数错误而崩溃。

/**
 * Outputs localized string if polylang exists or  output's not translated one as a fallback
 *
 * @param $string
 *
 * @return  void
 */
function pl_e( $string = '' ) {
    if ( function_exists( 'pll_e' ) ) {
        pll_e( $string );
    } else {
        echo $string;
    }
}

/**
 * Returns translated string if polylang exists or  output's not translated one as a fallback
 *
 * @param $string
 *
 * @return string
 */
function pl__( $string = '' ) {
    if ( function_exists( 'pll__' ) ) {
        return pll__( $string );
    }

    return $string;
}

// these function prefixes can be either you are comfortable with.

注意我们在 pl__pl_e 中创建了带有单个 l 的函数,原始的 Polylang 函数是 pll__pll_e.

这些将在您的主题中用于输出或return 翻译的字符串。

如前所述,我们必须注册这些字符串,以便 Polylang 知道应该翻译这些字符串。

如果您使用主题,可能最好在 after_setup_theme 挂钩中初始化它们,如下所示:

function your_prefix_after_setup_theme() {

   // register our translatable strings - again first check if function exists.

    if ( function_exists( 'pll_register_string' ) ) {

        pll_register_string( 'ToggleNavigation', 'Toggle navigation', 'YourThemeName', false );

        pll_register_string( 'ToggleSearch', 'Toggle Search', 'YourThemeName', false );

        pll_register_string('404Message', 'It looks like nothing was found. Try getting back to the <a href="%s">home page</a>.', 'YourThemeName', true);

    }
}
 add_action( 'after_setup_theme', 'your_prefix_after_setup_theme' );

谢谢你! 我添加了这个设置,然后我在其他地方发现了另一个技巧,可以在 functions.php 文件中添加我的可翻译文本:

 __(pll__('string to translate'), 'text-domain')

您好,有什么方法可以使用名称进行翻译吗?

add_action('init', function() {
    pll_register_string('footer-Newsletter-form', 'Subscribe to Newsletter');
});

$translated_string =  pll_translate_string('footer-Newsletter-form', $lang);