When using Polylang Pro, Polylang adds a language selection dropdown to each widget. This allows you to specify the language for each widget. However, with the free version of Polylang, this “Languages” option is not available. Only “Legacy Widgets” can be translated. These are widgets that were available before WordPress 5.8 (found under the Appearance tab -> Widgets sub-tab). They still work in WordPress 5.8 and beyond and can be accessed in the legacy widget block. You can choose to display these widgets for all languages (default setting) or for a specific language.
For more detailed information, refer to the Polylang documentation on widgets.
How to Translate Widgets Using the Free Version of Polylang
- Add a Class Name to Your Block: First, add a class name to your widget block that includes the language in which it is written, such as
widget-en
for English orwidget-fr
for French.
- Add a Global Class to the
<body>
Tag: In your theme’sheader.php
file, modify the<body>
tag to include the current language as a class:
<body <?php body_class(pll_current_language()); ?>> - Edit Your
styles.css
File: Add the following CSS to yourstyles.css
file:
body.en .widget-fr {
display: none;
}
body.fr .widget-en {
display: none;
}
This CSS ensures that the .widget-fr elements are hidden when the current language is English (en), and the .widget-en elements are hidden when the current language is French (fr).
By following these steps, you can effectively manage and display widgets based on the current language using the free version of Polylang.
This is very smart, thanks
Perfect solution! Thanks!