2

I'm using Polylang plugin on recent website. Posts and pages, also categories translations work perfectly. But I have problem with translations of custom strings.

I am registering string in my functions.php file with:

pll_register_string('read_more', 'Read More');

Then, in my template view I'm trying to get access to it by:

pll_e('read_more');

In response, I'm getting the "read_more"... The same if I tried to:

pll_e('Read more');

According to function reference of that plugin, it's a proper way. Maybe someone had a similiar problem in the past and can help.

Edit: I've conquer the problem by using Polylang extension for custom string. But, maybe someone can resolve this anyway.

Tomasz Jacek
  • 53
  • 1
  • 9

2 Answers2

3

I think there is a bug in the last version of Polylang or maybe it's a regression due to an upgrade of wordpress but for me pll_register_string would not work at all... I had to use icl_register_string instead which is the WPML counterpart (yet works with Polylang).

The plugin "Polylang Theme Strings" would not work either. It would find translations in the current theme but without being able to add them to the strings translations list. My guess is that it's due to the same bug but it could be a coincidence.

Migs
  • 96
  • 6
2

I am not sure if it helps you perfectly but the best approach I am using in this case is to create .po and .mo file for each language, upload it to a website and register it in functions.php or in plugin with this function (you must set path where .po and .mo files will be stored - bellow it's plugin path but you can change it to whatever you want and also you can change "my-theme" tag to your own):

// Adding language files
function theme_setup_hook () {
 load_theme_textdomain('my-theme', plugin_dir_path(__FILE__).'/languages');
}
add_action( 'after_setup_theme', 'theme_setup_hook' );

In .po file using Poedit (lightweight open source app which can be downloaded here https://poedit.net/) you just set the language of the file in Properties and select the folder on your disc where files with code with strings to translate are written. Also you must set Keywords in properties where I put these: __ and _e.

In the template's code you then write all strings which needs to be translated like this:

for simple echo:

<?php _e('string to translate','my-theme'); ?>

for using in scripts:

<?php
 $string = __('string to translate','my-theme');
 echo $string;
?>

After that just Update the po file in Poedit and since you have set proper path all strings are updated here and you can all translate them. Then save the file. Poedit automatically create also .mo file so upload both of files (po and mo) back to server and that's it. For each language you can do that with separate file which is named like en_GB.po etc.

I hope it helps.

Grows
  • 53
  • 6