Custom widgets for localized text in wordpress posts page

Problem:

On a wordpress project, the posts page needed static localized text.
The site is an already deployed wordpress installation with a custom template and the multilingual plugin Polylang ( https://wordpress.org/plugins/polylang/ )
The standard wordpress package isn’t very friendly towards multilanguage sites, and relies on external plugins and tweaks to support different localized content.

Solution: Register and place dedicated widget areas in the index page, and create a custom text widget.

This solution can be implemented in three quick easy steps.

Step 1:

Create the code to register two new widget areas, one that goes in the higher part of the page and one for the middle

In the file functions.php of the custom theme, add:

function cascina_widgets_init() {

register_sidebar( array(
             'name'          => 'Higher - Descrizione Eventi e Eventi Futuri',
             'id'            => 'eventi_descrizione_01',
             ) );

register_sidebar( array(
             'name'          => 'Middle - Titolo Eventi Passati',
             'id'            => 'eventi_descrizione_02',
             ) );
}

add_action( 'widgets_init', 'cascina_widgets_init' );

Step 2:

Display the widget area in the post page, by editing index.php and adding in the desired spots the following code:

<?php dynamic_sidebar('eventi_descrizione_01'); ?>

and

<?php dynamic_sidebar('eventi_descrizione_02'); ?>

Here are in the admin panel the new two widget areas on top of the others.

Step 3:

Now the template has two more widget areas, and I need a very simple text widget.
The default one in wordpress has the “textwidget” class that alters the CSS and the desired outcome.

To avoid messing with the textwidget class, I chose to define a new text widget from scratch, adding the following code in the custom theme functions.php

class Cascina_Text_Widget extends WP_Widget {

    public function __construct() {
          $widget_ops = array('classname' => 'Cascina_Widget', 'description' => 'Widget Testo Eventi' );
          $this->WP_Widget('Cascina_Text_Widget', 'Widget Testo Eventi', $widget_ops);
    }

    public function form( $instance ) {
          $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
          $title = $instance['title'];
          $text = $instance['text'];
    ?>

    <p><label for="<?php echo $this->get_field_id('text'); ?>">Text/HTML Code:
<textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"/> <?php echo attribute_escape($text); ?></textarea>
    </label></p>

   <?php
   }
    function widget( $args, $instance ) {
          extract($args);
          $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' :           $instance['text'], $instance );
          echo $text;
          }
 }

add_action( 'widgets_init', function(){
          register_widget( 'Cascina_Text_Widget' );
          });

In conclusion:

I can now add multiple copies of this custom widget and choose which one to show to the visitor according to the language the visitor is using.
This custom text widget that will be placed in the newly registered widget areas is a quick solution that works well with the Polylang plugin, and serves the purpose.