Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator bcworkz

    (@bcworkz)

    Because a text widget can contain random text that’s not known ahead of time, gettext cannot work. Instead of a text widget, you could add PHP code on your sidebar template after the dynamic_sidebar() call. The PHP code could use gettext to output static content since the text is static and known ahead of time.

    Thread Starter sacconi

    (@sacconi)

    I dont have a sidebar.php. There is no way to create a div with inside an echo? there are just few words

    Moderator bcworkz

    (@bcworkz)

    But you have a widget area that places widgets in that sidebar some way, even if not via sidebar.php? You must then have a call to dynamic_sidebar() somewhere on the template that is being used. Right after that call you could add something like:
    echo __('INFORMAZIONI PRENOTAZIONI', 'sacconicase'), ':<br>02.320.555.555';
    (I altered the phone number on purpose)

    Thread Starter sacconi

    (@sacconi)

    I decided to create a specific widget for the text under the widget with the image: , my problem is that I have to put an active email link after the telephon number, more precisely (for english):

     <a href="mailto:[email protected]?subject=request of%20availibility/booking/info&amp;body=pls insert:%20DESIRED%20RESORT%20or%20PRODUCT%20CODE/S%20%28up to%20max%2010%20codes%29,%20PERIOD%20,%20NUMBER OF PEOPLE%20%28adults,%20children,%20age of the children),PETS ,%20FAMILY NAME%20e%20TEL. NUMBER,OTHER REQUESTS-%20Thank you" style="background-color: rgb(255, 255, 255);"><br style="background-color: rgb(255, 255, 255);">
    	<span style="color:#0099cc;">[email protected]</span></a> 

    I should gettext also the attrubutes of the email, so the “subject” and the “body”, is it possible?

    I havent been able up to now to write the email after

    echo __('INFORMAZIONI PRENOTAZIONI', 'sacconicase'), ':<br>02.320.555.555';
    Moderator bcworkz

    (@bcworkz)

    “body” and “subject” are directives passed to the mail client. I don’t think they will work if you translated them. Regardless of them always being in English, what the user will see as labels in their mail client will be in whatever language they have set.

    The echo line I suggested is PHP code, it needs to be within <?php ?> delimiters. The mailto: link you want is HTML, it needs to be outside of <?php ?> delimiters.

    Thread Starter sacconi

    (@sacconi)

    So, is there the system to replace conditionally the whole widget according to the language? So I could do 2 widgets and show one according to the locale

    Moderator bcworkz

    (@bcworkz)

    Template code can call the_widget() to output the content of a specific widget outside of the usual widget areas which use dynamic_sidebar() to output all widgets added to a particular widget area. You could conditionally pass one particular widget’s name or another based on locale.

    Or you could add another widget area to your theme. It’s called a “sidebar” in code regardless of where the widget area actually appears. They are often in footers as well as in actual sidebars. You could have a different widget area for each language and conditionally specify which one to use based on locale.

    Thread Starter sacconi

    (@sacconi)

    It is possible to do something like that:

    add_filter('the_widget', function( $widget, $post_id ) {
       $locale = get_locale();
       if ('de_DE' == $locale ) {
       return $widget == IDx;
         } else {
            return $widget IDy;
         }     
    Moderator bcworkz

    (@bcworkz)

    “the_widget” is an action hook, not a filter. All it does is indicate the_widget() was called, allowing us to do something in addition. We cannot influence the original call from an action hook like this.

    On a template you could do something like:

    $locale = get_locale();
    if ('de_DE' == $locale ) {
       $widget == IDx;
    } else {
       $widget == IDy;
    }
    the_widget( $widget );
    Thread Starter sacconi

    (@sacconi)

    This unluckily is not working (also without quotes/double quotes)

    $locale = get_locale();
    if ('de_DE' == $locale ) {
       $widget == "text-16";
    } else {
       $widget == "text-14";
    }
    the_widget( $widget );
    

    I see both the widgets, both in italian and german locale

    Moderator bcworkz

    (@bcworkz)

    I don’t think it’s possible for a single the_widget() call to invoke two different widgets. Did you remove the related widgets from the active widget area? Remove them from the active widget area but do not delete them. Drag them to inactive widgets. They should no longer be in the active area because the_widget() call replaces that need.

    The $widget arg passed to the_widget() is the widget object’s PHP class name, it’s not the HTML element’s class name. A typical PHP class name for a built-in WP widget is “WP_Widget_Tag_Cloud”. If you declared your own custom widget, you’d have a line similar to:
    class My_Widget_Class_Name extends WP_Widget {

    If the widget accepts input like a text widget would, you also need to pass an $instance arg to the_widget() which specifies which input to use in outputting the widget. The composition of $instance varies by widget, you would need to examine the source code to learn the correct composition. The passed $instance does not have to be related to widgets you’ve added to widget areas, it can be arbitrary content:

    the_widget('WP_Widget_Text', array(
    	'text'   => 'Hello world! from an undefined text widget',
    	'filter' => true,
    	'legacy' => true,
    ));

    AFAIK all block based widgets are wrapped in an instance of WP_Widget_Block. The $instance arg might look like:

    array(
      'content' => '<!-- wp:paragraph -->
         <p>Paragraph block widget content</p>
       <!-- /wp:paragraph -->',
    )

    If need be you can get instance data of widgets you’ve already added to a widget area from the options table. Which option depends on which widget. Block widget instances are all in an array under the option “widget_block”.

    IMO this is all needlessly complex. I think it’d be simpler to have a series of custom options and have your template code output any option values that are appropriate for any given situation.

    Thread Starter sacconi

    (@sacconi)

    For the moment I have opted for a simpler solution. In the widget there is a photo and that doesn’t need translation, so I’ll put the photo in one widget and the text in another widget, for the text I’m using a shortcode + gettext. If I wanted to have an email with a different subject for Italian and German, I simply put the email in a separate shortcode, with a conditional. Something like this is it possible:

    function email() {
    
    $locale=get locale;
    
    if $locale == [de_DE]
    
    echo '<a href="mailto:[email protected]?subject=ID Apartment:">[email protected]</a>';
    
    else {echo '<a href="mailto:[email protected]?subject=ID appartamento:">[email protected]</a>';
    
    
    $locale="";
    }
    
    // register shortcode
    
    add_shortcode('contatto_email', 'email');
Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Gettext for a text widget’ is closed to new replies.