php Polylang:如何翻译自定义字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46557981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Polylang: How to translate custom strings?
提问by MIkeMo
My problem: I'm translating my website using Polylang but I'm having a hard time with custom strings translation. The strings won't show up in the "Strings translation" menu in the WP dashboard.
我的问题:我正在使用 Polylang 翻译我的网站,但我在自定义字符串翻译方面遇到了困难。字符串不会显示在 WP 仪表板的“字符串翻译”菜单中。
Important:I don't know much about PHP so the pll_register_string functionis very confusing for me.
重要提示:我对 PHP 了解不多,所以pll_register_string 函数对我来说非常混乱。
Quoted from the Polylang doc:
引自 Polylang 文档:
https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/
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
pll_register_string
允许插件在“字符串翻译”面板中添加自己的字符串。该函数必须在管理端调用(functions.php 文件可以用于主题)。可以注册空字符串(例如当它们来自选项时),但它们不会出现在列表中。
用法:
pll_register_string($name, $string, $group, $multiline); '$name' =>(必需)为方便排序而提供的名称(例如:'myplugin')'$string' =>(必需)要翻译的字符串 '$group' =>(可选)字符串所在的组已注册,默认为 'polylang' '$multiline' => (可选)如果设置为 true,则翻译文本字段将为多行,默认为 false
pll__
翻译先前使用 pll_register_string 注册的字符串 用法:
pll__($string); 唯一参数是必需的:
'$string' => 要翻译的字符串返回翻译后的字符串。
pll_e
回显先前使用 pll_register_string 注册的已翻译字符串用法:
pll_e($string); 唯一参数是必需的:
'$string' => 要翻译的字符串
Best regards
此致
回答by drazewski
You must first register all these strings for translation.
您必须首先注册所有这些字符串以进行翻译。
For example you echo "Hello world" in some template file like this:
例如,您在一些模板文件中回显“Hello world”,如下所示:
<?php pll_e('Hello world'); ?>
To show string in the "Strings translation" add in your functions.php
:
要在“字符串翻译”中显示字符串,请添加functions.php
:
add_action('init', function() {
pll_register_string('mytheme-hello', 'Hello world');
});
Add all custom strings you want to translate to this function.
将您要翻译的所有自定义字符串添加到此函数中。
回答by Mikhail.root
As Polylang docssays it's good to check polylang functions for existance first - so site will not break upon Polylang plugin update - because it removes old files first.
正如 Polylang文档所说,最好先检查 polylang 函数是否存在 - 因此站点不会在 Polylang 插件更新时中断 - 因为它首先删除旧文件。
So i propose this approach:
in functions.php
for theme of in your plugin's file, you can create wrappers for needed Polylang functions with fallbacks if polylang was removed, or updated so WP will not crash with undefined function error.
所以我提出了这种方法:在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.
NOTE we've created functions with single l
in pl__
and pl_e
and original Polylang functions are pll__
and pll_e
.
注意我们已经创建了具有单l
输入pl__
和pl_e
和原始 Polylang 函数是pll__
和的函数pll_e
。
These will be used in your theme to output or return translated strings.
这些将在您的主题中用于输出或返回翻译后的字符串。
And as mentioned before we must register these strings so Polylang will know that these should be translated.
如前所述,我们必须注册这些字符串,以便 Polylang 知道应该翻译这些字符串。
If you work with theme probably it's good to initialize them in after_setup_theme
hook like this:
如果您使用主题,可能最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' );
回答by user12547341
Thank you for this! I add this setup and then i found another trick somewhere else to add my translatable text in the functions.php file:
这次真是万分感谢!我添加了这个设置,然后我在其他地方找到了另一个技巧来在functions.php文件中添加我的可翻译文本:
__(pll__('string to translate'), 'text-domain')