Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
971 views
in Technique[技术] by (71.8m points)

php - Polylang: How to translate custom strings?

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.

Important: I don't know much about PHP so the pll_register_string function is very confusing for me.

Quoted from the Polylang doc:

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

Best regards

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You must first register all these strings for translation.

For example you echo "Hello world" in some template file like this:

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

To show string in the "Strings translation" add in your functions.php:

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

Add all custom strings you want to translate to this function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...