Translations
The translations utility functions provide a layer of abstraction for working with Polylang, a popular translation plugin for WordPress. These functions ensure compatibility even if Polylang isn't activated.
Available Functions
pl_e($string)
Displays a localized string with Polylang or the original string if Polylang isn't available.
Parameters:
$string: The string to translate.
1// Display a translated string2pl_e('Welcome to our website');34// With HTML5pl_e('<a href="/contact">Contact us</a>');
pl__($string)
Returns a localized string with Polylang or the original string if Polylang isn't available.
Parameters:
$string: The string to translate.
Return Value:
Returns the translated string or the original string if Polylang isn't available.
1// Get a translated string2$welcome = pl__('Welcome to our website');3echo $welcome;45// With variables6$username = 'John';7echo sprintf(pl__('Hello %s, welcome to our website'), $username);
Configuration
These functions use the global $cbo variable for configuration:
$cbo['pll']['reference']: Determines if strings are referenced by content ('string') or by key ('key').$cbo['pll']['strings']: Associative array of strings to translate whenreferenceis set to'key'.$cbo['pll']['domain']: Translation domain used by Polylang.
Modes of Operation
Content Mode (Default)
In this mode, strings are identified directly by their content:
1// Configuration2$cbo['pll']['reference'] = 'string';34// Usage5pl_e('Welcome to our website'); // Displays "Welcome to our website" or its translation
Key Mode
In this mode, strings are identified by a key, allowing you to modify the source text without updating translations:
1// Configuration2$cbo['pll']['reference'] = 'key';3$cbo['pll']['strings'] = [4'welcome' => 'Welcome to our website',5'thanks' => 'Thank you for your visit'6];78// Usage9pl_e('welcome'); // Displays "Welcome to our website" or its translation
Usage Examples
Multilingual Navigation
1// Create a multilingual navigation menu2function multilingual_menu() {3$menu_items = [4['url' => '/', 'text' => 'Home'],5['url' => '/about', 'text' => 'About Us'],6['url' => '/services', 'text' => 'Services'],7['url' => '/contact', 'text' => 'Contact']8];910echo '<nav class="main-nav">';11echo '<ul>';1213foreach ($menu_items as $item) {14echo '<li>';15echo '<a href="' . $item['url'] . '">' . pl__($item['text']) . '</a>';16echo '</li>';17}1819echo '</ul>';20echo '</nav>';21}2223// Usage24multilingual_menu();
Translatable Form Labels
1// Create a contact form with translatable labels2function contact_form() {3echo '<form class="contact-form" method="post">';45echo '<div class="form-group">';6echo '<label for="name">' . pl__('Name') . '</label>';7echo '<input type="text" id="name" name="name" required>';8echo '</div>';910echo '<div class="form-group">';11echo '<label for="email">' . pl__('Email') . '</label>';12echo '<input type="email" id="email" name="email" required>';13echo '</div>';1415echo '<div class="form-group">';16echo '<label for="message">' . pl__('Message') . '</label>';17echo '<textarea id="message" name="message" required></textarea>';18echo '</div>';1920echo '<button type="submit">' . pl__('Send Message') . '</button>';21echo '</form>';22}2324// Usage25contact_form();
Best Practices
- Use key mode for better maintainability when working with a team of translators.
- Keep translations organized by using a consistent naming convention for keys.
- Use sprintf for strings with variables to ensure proper translation.
- Document your translation keys to help translators understand the context.
- Use these functions consistently throughout your theme for better maintainability.