Site logo

[Resolved] Help with gettex

Home Forums Support [Resolved] Help with gettex

Home Forums Support Help with gettex

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1565053
    Enrico

    Hi everybody.
    I’m a dummy, I don’t know PHP, and I hope some of you will help me.
    Also, I don’t know if my request is related with GeneratePress support, but I try to ask…
    I would like to translate via WPML plugin a couple of words, but the text must be wrapped in gettext calls, and I have not idea how to do.
    WPML support say that I must use _e() and text-domain too, but they do not give me more help 🙁

    The words that I need to translate are “Published on” and “Last Updated on”.
    Hereunder is the code, that I added with Code Snippet plugin:

    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
        $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
    
        if ( get_the_date() !== get_the_modified_date() ) {
            $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on: %2$s</time> | <time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated on: %4$s</time>';
        }
    
        $time_string = sprintf( $time_string,
            esc_attr( get_the_date( 'c' ) ),
            esc_html( get_the_date() ),
            esc_attr( get_the_modified_date( 'c' ) ),
            esc_html( get_the_modified_date() )
        );
    
        return sprintf ( '<span class="posted-on">%s</span> ',
            $time_string
        );
    }, 10, 2 );

    That’s all. I hope anybody has time to help me.
    Thank you in advance.

    #1565076
    Elvin
    Staff
    Customer Support

    Hi,

    To clarify: Should the translation be dynamic? Meaning are the translations depending on which page you are on? (EN page, FR page, JP page, etc?)

    If not, you can simply re-write “Published on: and “Last Updated on:” within the code into your text literal translation.

    Example #1: translating “Published on:” to “Veröffentlicht auf:”(German).

    Change this:
    <time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on: %2$s</time>

    to

    <time class="entry-date published" datetime="%1$s" itemprop="datePublished">Veröffentlicht auf: %2$s</time>

    Example #2: translating “Last Updated on:” to “Huling nai-update noong:”.(Filipino)
    Change this line:
    <time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated on: %4$s</time>

    to this line:
    <time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Huling nai-update noong: %4$s</time>

    We’re basically just replacing the default English string within the code to our preferred translated text.

    #1565102
    Enrico

    Thank yuo very much Elvin.
    Yes, the translation must be dynamic.
    WPML support gave me two links, but as I said I’m a dummy 🙂
    They suggest to have a look here:

    https://wpml.org/documentation/support/enabling-text-translation-for-themes-not-compatible-with-wpml/

    https://wpml.org/faq/getting-string-translation-to-work/

    Thanks again.

    #1565313
    Elvin
    Staff
    Customer Support

    Ah right of course.

    Try this:

    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
        $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
    
        if ( get_the_date() !== get_the_modified_date() ) {
            $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%5$s %2$s</time> | <time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">%6$s %4$s</time>';
        }
    
        $time_string = sprintf( $time_string,
            esc_attr( get_the_date( 'c' ) ),
            esc_html( get_the_date() ),
            esc_attr( get_the_modified_date( 'c' ) ),
            esc_html( get_the_modified_date() ),
    		_e( 'Published on:', 'generatepress' ),
    		_e( 'Last Updated on:', 'generatepress' )
        );
    
        return sprintf ( '<span class="posted-on">%s</span> ',
            $time_string
        );
    }, 10, 2 );

    And here’s a sample PHP to test this code. We’re basically testing if the string translate with gettext().

    add_filter( 'gettext', function( $text ) {
        if ( 'Published on:' === $text ) {
            $text = 'Your published on translation';
        }
    
        if ( 'Last Updated on:' === $text ) {
            $text = 'Your last updated on translation';
        }
    
        return $text;
    } );
    #1565353
    Enrico

    Thanks a lot!
    It works almost well. The only thing is that the two sentences Published on: (Publicato il) and Last update on: (Aggiornato il:) are on the same line, consecutive.

    Please have a look here:
    Guide Dolomiti

    Thanks for your patience 🙂
    Enrico

    #1565588
    Elvin
    Staff
    Customer Support

    Ah that’s right my bad. I’ve misread a few parts.

    Try this one.

    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
        $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
    
        if ( get_the_date() !== get_the_modified_date() ) {
            $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time> | <time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">%4$s</time>';
        }
    
        $time_string = sprintf( $time_string,
            esc_attr( get_the_date( 'c' ) ),
            esc_html__( 'Published on:', 'generatepress' ).' '.esc_html( get_the_date() ),
            esc_attr( get_the_modified_date( 'c' ) ),
            esc_html__( 'Last Updated on:', 'generatepress' ).' '.esc_html( get_the_modified_date() )
        );
    
        return sprintf ( '<span class="posted-on">%s</span> ',
            $time_string
        );
    }, 10, 2 );
    #1565668
    Enrico

    Thank you very very much!
    It works perfectly.
    Grazie.
    Enrico

    #1566503
    Elvin
    Staff
    Customer Support

    Glad it works for you. No problem. 😀

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.