Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polylang: How to translate custom strings?

Tags:

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

like image 921
MIkeMo Avatar asked Oct 04 '17 06:10

MIkeMo


People also ask

How do I translate a string in WordPress?

To get started, install the free TranslatePress plugin from WordPress.org. Then, go to Settings → TranslatePress to choose the language(s) into which you want to translate your site. Again, the free version lets you translate your site into one new language, while the premium version lets you use unlimited languages.

Does Polylang translate automatically?

You can use Polylang to translate content manually inside WordPress, while for automatic translation you can utilize Lingotek. It offers community, professional, and machine translation solutions to simplify the maintenance and operation of a website with content in multiple languages.

How to show string in Polylang function translation?

To show string in the "Strings translation" add in your functions.php: Add all custom strings you want to translate to this function. Show activity on this post. As Polylang docs says 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.

How to delete translations from the Polylang database?

You can permanently delete these translations from your database by checking the “Clean strings translation database” option ❷ , and then clicking on “Save Changes”. ❸ Thanks to the WPML compatibility mode of Polylang, some strings may be registered by the WPML functions, used by the themes and plugins.

How to translate WordPress strings?

You first need to fill in the relevant fields in the WordPress, plugins or theme settings the source strings. Then go in the Languages > Strings translations, translate your strings in the Translations column and then click on “Save changes”.

How to translate a string into another language?

Then go in the Languages > Strings translations, translate your strings in the Translations column and then click on “Save changes”. If some languages are missing, make sure that the languages filter displays “Show all languages”.


1 Answers

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.

like image 102
drazewski Avatar answered Sep 22 '22 03:09

drazewski