[Resolved] Translating dates

Home Forums Support Translating dates

  • This topic has 7 replies, 3 voices, and was last updated 1 year ago by Tom.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1125959
    sirlouen

    How can I translate dates in posts?
    For example:

    #1126157
    Leo
    Staff
    Customer Support

    Hi there,

    I feel like it should be translated by default.

    Can you confirm that your site language is not set to English?
    https://wordpress.org/support/article/settings-general-screen/

    Let me know πŸ™‚

    #1126163
    sirlouen

    Hello @Leo, the language is fine, check it in the bottom (Everything in spanish, comments and all that), just the date is not being translated, even the “By %s” is being translated as you may see

    https://neocinismo.com/poder-como-fuego/

    (I have updated it to the DD/MM/YYYY format so it technically solves it, but still concerned of why it’s not being translated to something like “8 Enero 2020” instead of “8th January 2020”

    #1126271
    Tom
    Lead Developer
    Lead Developer

    I’m surprised WordPress doesn’t do this by default.

    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_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
            $time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">%4$s</time>' . $time_string;
        }
    
        $time_string = sprintf( $time_string,
            date_i18n( 'c', strtotime( get_the_date() ) ),
            date_i18n( '', strtotime( get_the_date() ) ),
            date_i18n( 'c', strtotime( get_the_modified_date() ) ),
            date_i18n( '', strtotime( get_the_modified_date() ) )
        );
    
        return sprintf( // WPCS: XSS ok, sanitization ok.
            '<span class="posted-on">%1$s<a href="%2$s" title="%3$s" rel="bookmark">%4$s</a></span> ',
            apply_filters( 'generate_inside_post_meta_item_output', '', 'date' ),
            esc_url( get_permalink() ),
            esc_attr( get_the_time() ),
            $time_string
        );
    }, 10, 2 );

    I’m not 100% sure it will work, but it’s worth a shot.

    #1127721
    sirlouen

    I’ve found the trick, with a combination of the filter option_time_format to adapt the format to j F Y instead of the default english version, and wpml_register_single_string and translating all the month’s strings manually.

    Maybe it’s not the best and by using date_i18n it’s straight forward. But I’ve tested that filter and the code shows right but not the strings within the code (the ones that theoretically shall be shown)

    #1128027
    Tom
    Lead Developer
    Lead Developer

    That sounds like a good solution as well! πŸ™‚

    #1128028
    sirlouen

    The good part, is that combining with the Amp plugin, since the default behaviour of WP is not based on the i18n function, it works site-wide, not only in the parts that have been templated with GP.

    #1128339
    Tom
    Lead Developer
    Lead Developer

    Ah right, that’s awesome! Thanks for sharing what worked for you, we appreciate it πŸ™‚

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