WordPress – Polylang Language Plugin : Registering Specific Words

What happened

Recently, while switching between various types of themes, I found some themes with great designs. However, I noticed that certain pages have hardcoded text.

Since the website is bilingual, it would be better to make some adjustments. Therefore, I’m looking to find out how to do this.

Solution and Example

For instance, in the current theme I’m using, Femina, the search results display in English.

I’m currently using the Polylang language translation plugin, which essentially allows you to register groups of words. This enables you to perform text translations at specific times.

But I haven’t found the group for Femina on its page.

Syntax Explanation

So, I searched online and found that Polylang provides a built-in way to register strings using pll_register_string. However, embarrassingly, I couldn’t find a way to unregister them, and the delete option in the UI seemed ineffective.

After some more searching, I discovered another approach – icl_register_string. This syntax is from WPML, a plugin that predates Polylang. Polylang supports most of its syntax (link provided in the reference material).

Here’s how to use it: I wrote this in the functions.php file of Femina theme.

// Registering a string with WPML's icl_register_string function
if (function_exists('icl_register_string')) {
    icl_register_string('femina', 'Search Results', 'Search Results for:');
}

// Unregistering the string using icl_unregister_string function
if (function_exists('icl_unregister_string')) {
    icl_unregister_string('femina', 'Search Results');
}

This code registers a string with the group name ‘femina’, parameter name ‘Search Results’, and default value ‘Search Results for:’. Then, it unregisters the same string using the icl_unregister_string function.

After doing this, you’ll see a new group in the plugin interface.

How to reference

Although the Polylang plugin registers the words, you still need to manually reference them. So, using the example above, I need to first find the corresponding PHP page.

<?php if ( have_posts() ) : ?>
		<header class="page-header">
			<h2 class="page-title"><?php printf( __( 'Search Results for: %s', 'femina' ), '<span>' . esc_html( get_search_query() ) . '</span>' ); ?></h2>
		</header><!-- .page-header -->

This snippet of code reveals that the original text was hardcoded, but now we need to make it dynamic.

<?php if ( have_posts() ) : ?>
		<header class="page-header">
			<!--<h2 class="page-title"><?php printf( __( 'Search Results for: %s', 'femina' ), '<span>' . esc_html( get_search_query() ) . '</span>' ); ?></h2>-->
			<h2 class="page-title"><?php printf( __( '%s', 'femina' ), icl_t( 'femina', 'Search Results' ) . '<span>' . esc_html( get_search_query() ) . '</span>' ); ?></h2>
		</header><!-- .page-header -->

⬇The modified result is as follows, a cause for celebration!

We can now see custom Chinese characters in the search results!


Conclusion

The main focus was on explaining how to register specific words. In the future, we might delve into how to use the Polylang plugin, which proves quite practical for managing multilingual websites.

Reference

💔 The entire article was written and tested independently. Feel free to quote or reference, but please avoid copying the entire content verbatim.

🧡You can support our team by sharing the post or clicking some ads, Thanks a lot

If you have any problem about this post, please feel free to ask~

🍽️We’re also actively seeking collaboration opportunities, whether it’s through friendly links on partner sites or advertising partnerships

Some Random notes

Leave a Reply

Your email address will not be published. Required fields are marked *