- This topic has 7 replies, 3 voices, and was last updated 4 years, 3 months ago by Tom.
-
AuthorPosts
-
January 8, 2020 at 9:53 am #1125959sirlouen
How can I translate dates in posts?
For example:
January 8, 2020 at 2:27 pm #1126157LeoStaffCustomer SupportHi 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 π
January 8, 2020 at 2:33 pm #1126163sirlouenHello @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”
January 8, 2020 at 6:32 pm #1126271TomLead DeveloperLead DeveloperI’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.
January 10, 2020 at 5:19 am #1127721sirlouenI’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)
January 10, 2020 at 8:54 am #1128027TomLead DeveloperLead DeveloperThat sounds like a good solution as well! π
January 10, 2020 at 8:56 am #1128028sirlouenThe 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.
January 10, 2020 at 4:04 pm #1128339TomLead DeveloperLead DeveloperAh right, that’s awesome! Thanks for sharing what worked for you, we appreciate it π
-
AuthorPosts
- You must be logged in to reply to this topic.