25

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

MIkeMo
  • 332
  • 1
  • 3
  • 10

4 Answers4

64

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.

pravdomil
  • 2,961
  • 1
  • 24
  • 38
drazewski
  • 1,739
  • 1
  • 19
  • 21
  • 4
    Is there no way to auto detect these stings? I have used WPML before, and then did this for example: `` And then i was recognized by WPML... – Loosie94 Dec 04 '19 at 14:07
  • 1
    Yeah, Polylang does not pick up those strings. You need to register them first. https://polylang.pro/doc/function-reference/#pll_register_string – Klaudio Milankovic Mar 24 '20 at 20:38
  • 2
    For those who are interested, I used an additional plugin for string translations. See: https://wordpress.org/plugins/theme-translation-for-polylang/ – Loosie94 Feb 05 '21 at 09:42
  • @drazewski Does this work if the string is from a plugin, not a custom string added on the theme? I need to translate a Cookie notice from Patchstack plugin – Diego M. Jun 04 '21 at 20:49
11

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.

So i propose this approach: in functions.phpfor 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.

/**
 * 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.

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.

If you work with theme probably it's good to initialize them in after_setup_theme hook like this:

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' );
Mikhail.root
  • 764
  • 9
  • 18
  • 1
    Or simply (correct me if i am wrong) : `if ( !function_exists( 'pll_e' ) ) { function pll_e( $string = '' ) { echo $string; } }` – Corentin Dec 14 '18 at 23:09
  • @Corentin yes, i've just described `pl__` `pl_e` approach to simplify code in templates as it's just shorter and cleaner then type `if(!function_exists('pll_e')){}` each time you need a translated string. – Mikhail.root Jan 02 '19 at 14:52
  • @Mikhail.root You don't have to type it each time. If the Polylang is activated you can safely use `pll_e` and `pll__`. If Polylang however is NOT activated, it will call custom `pll_e` and `pll__` functions, which only output the string. I think this approach is better because some developer might not noticed that your theme uses custom method names for Polylang calls and they might use the default ones instead - then your solution wouldn't work. – Kristián Filo Jan 14 '21 at 17:56
0

Hi there any way to translate using the name ?

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

$translated_string =  pll_translate_string('footer-Newsletter-form', $lang);
Wilmar Arias
  • 186
  • 8
  • That is a good question! Any anwers on that? It is very strange to use the value instead of the name.. If you have a whole paragraph or something like that ...? – Kiriakos Grhgoriadhs Jan 19 '23 at 18:38
-1

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:

 __(pll__('string to translate'), 'text-domain')
  • 1
    This is not going to work for translating your project. This is fine for your system but cannot be used if you want others to use it as the way gettext works requires an actual string. – Jeremy Jun 20 '22 at 18:51