Translate any string with Polylang

 

Polylang allows you to translate almost any WordPress string, from your template and some plugins. After installing Polylang, in Languages / Translations you will find everything you can translate from your administration panel.

I say that almost any string can be translated because, in practice, some templates and plugins are either not fully compatible with Polylang or fail to register all of them for reasons that in some cases are beyond my understanding.

Today we are going to see how to translate and add those strings that resist registering them as new with the help of Ad Inserter Pro in the GeneratePress template in its Premium version.

The only difference in these steps if you don't use Ad Inserter, is that you will have to add by hand the call to these new translation strings in the corresponding WordPress files. You can refer to the Polylang documentation, but if you are not sure how to interpret this information for practical purposes I will try to explain it as simply as possible.

Add a new chain

Once we have checked that the strings we want to translate do not appear in the Polylang translation panel, it is time to register them.

The example I will use is a very common one that is often resisted, it is the text"Leave a comment" which we will translate as "Anything to say?"

This is the text that is displayed above the comment box and in some templates, such as GeneratePress, it does not appear in the comments.php file, which is where it usually is.

The first step to register a new string is to add it in the functions.php file (it is always advisable to do this in the functions.php of a child theme).

pll_register_string('¿Algo que decir?', 'Leave a comment');;

If you are going to register several strings you can comment on them to remember how they were added. The last ones I added have these notes:

Translate any string with Polylang 1

Now, when you go back to the translation administration, you will find that the text is now available for translation under a new group called "Polylang".

We add our translations and save, this only registers the translations in the Polylang administration.

The last step is to add the call in the right place in our template. In a perfect world we would open the comments.php file of our template and add the following replacing the line where "Leave a comment" appears (the size and thickness is up to your taste and needs).

<h5 class="post-title"> <strong><?php echo pll__('Leave a comment'); ?></strong></h5>

If everything went well, congratulations. This is the end of the tutorial for you. Now the message will be displayed in each language you have added.

It did not work

If it didn't work or you can't find that text in the comments.php file of your template and you don't know where it is called from, all is not lost. You can still cheat to get it added. This is where Ad Inserter comes into play, and although it is a plugin for placing ads, it can be used for both the broken and the broken.

The first thing we will do is hide the "Leave a Comment" message with this CSS (valid for GeneratePress, for other templates you should consult their documentation).

#reply-title {
    display: none;
}

We will then create a new AdInserter block like this by adding the code:

1- Check that it is shown in the entries.

2- In Insert, choose"before, after or inside" the HTML element, depending on what you want.

3- Click on the box indicated and a screen will open where you can select the place where you want it to be displayed. Open any post and mark the container, the colour of the selection will change from blue to red when you click on it and finally press"Use" (remember to remove the ID of the post if it appears at the beginning of the code or it will only be shown in that post).

For this example I marked "after" this message to be displayed just above the comment box, but you can also mark"before" and select the comment box in case the first option fails.

Save the Ad Inserter block and check that everything went well.

A couple of plugins to register strings

If you don't feel you can handle any of the methods described above and prefer to use a paid solution, there are at least two plugins that promise to make the task of registering and adding new strings easier.

One of them costs only 5 euros and is called Polylang register all strings and the other is Strings Registration for Polylang, which is much more expensive and sells for £25.

I haven't tried either of these two plugins, so I don't know if they do what they promise, so don't take the references as a recommendation. Before paying for them check with the developer to try to make sure they work with all your plugins and your template.

Post programado

Programmed post

This is an automatic scheduled summer post. Comments may remain in moderation.

Suscripción por email

Related articles

Este blog se aloja en LucusHost

LucusHost, el mejor hosting