- This topic has 7 replies, 2 voices, and was last updated 4 years, 9 months ago by
Elvin.
-
AuthorPosts
-
December 2, 2020 at 11:29 pm #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.December 2, 2020 at 11:50 pm #1565076Elvin
StaffCustomer SupportHi,
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.
December 3, 2020 at 12:19 am #1565102Enrico
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/faq/getting-string-translation-to-work/
Thanks again.
December 3, 2020 at 2:36 am #1565313Elvin
StaffCustomer SupportAh 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; } );
December 3, 2020 at 3:07 am #1565353Enrico
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 DolomitiThanks for your patience 🙂
EnricoDecember 3, 2020 at 5:54 am #1565588Elvin
StaffCustomer SupportAh 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 );
December 3, 2020 at 6:41 am #1565668Enrico
Thank you very very much!
It works perfectly.
Grazie.
EnricoDecember 3, 2020 at 2:56 pm #1566503Elvin
StaffCustomer SupportGlad it works for you. No problem. 😀
-
AuthorPosts
- You must be logged in to reply to this topic.