-
jrmora
Hello friends, I am happy with version 2.0 ๐
This is more of a query than an incident in case it is possible now, or in the future, to be able to do so.
I will try to explain:
The Polylang plug-in allows you to register strings to add their corresponding translations.
I use as an example these words that I have in a GP element with this GB Text block:
Publicado: {{post_date}} | Actualizado: {{post_date type:modified}} |Once the two strings are registered in the functions.php you can add each language from Polylang:
pll_register_string('Publicado:', 'Published:');pll_register_string('Actualizado:', 'Updated:');To display these translations, calls must be added like this:
<?php echo pll__('Published:'); ?><?php echo pll__('Updated:'); ?>At this point, the question is:
Is it possible to add these PHP calls to the GB Text block somehow?
These are the only strings I have yet to translate from the blog and I think it would be nice to be able to do it and it would be very useful for other scenarios where Polylang is used.
Tnx!
-
Hi there,
I don’t think GB text block is compatible with this.
However, you can try to turn the PHP code to shortcode, so you can add them to the block editor.
-
Good afternoon, Ying
Thanks, I hadn’t thought of that, I’ll try calling an Ad Inserter block with a shortcode and see if I have any luck.
I’ll let you know if it worked. -
Well, I’ve already tried with a shortcode and it didn’t work.
It doesn’t read any content inside the GB Text block, neither PHP, nor HTML, nor plain text, so I understand that the GB Text block doesn’t support shortcodes.
Anyway, I’ll keep investigating alternatives in case can add support for shortcodes in the future.
Regards.
-
I’m also wondering if
render_block()filter would work, to replace the text Publicado and Actualizado with the PHP function.Can you try this?
1. add a CSS class to the text block, eg.
my-post-dates.2. add this PHP code:
add_filter( 'render_block', function( $block_content, $block ) { if ( ! empty( $block['attrs']['className'] ) && 'my-post-dates' === $block['attrs']['className'] ) { $published = pll__('Published:'); $updated = pll__('Updated:'); // Ensure these values are translated, fallback to default if empty $translated_published = !empty($published) ? $published : 'Published:'; $translated_updated = !empty($updated) ? $updated : 'Updated:'; $block_content = str_replace('Publicado', $published, $block_content); $block_content = str_replace('Actualizado', $updated, $block_content); } return $block_content; }, 10, 2 );Adding PHP: https://docs.generatepress.com/article/adding-php/
Let me know if this helps!
-
jrmora
Hi, Ying Thank you very much for the filter, but it doesn’t seem to have worked.
It doesn’t read the new strings, but I think he has come very close to achieving it.
Reading the Polylang documentation, I think that to print strings you have to use pll_e.
To try out, I have tried to change it in the filter to pll_e but it does not print them as a replacement.
They appear as follows:
-
Hi there,
Could you let me know where you added the snippet? I looked at the
functions.phpfile, but it doesn’t appear to be there. -
jrmora
Good morning, Alvind
Last night I was testing other options and made a backup before I left.
I’ve already put it back in the
functions.phpif you want to take a look at it. -
Can you also link us to the element in question? I’m lost in 17 pages of elements…
And just to confirm that you want Polylang’s function to translate the Publicado and Actualizado words?
-
jrmora
Yes, sorry. There are many elements for many languages and test. The element in question is called:
GB 2.0 META Fecha y hora publish-update-
Yes, I want to translate these two words for each language, Publicado: and Actualizado:
As these strings are already registered, the translations can be added in Polylang.
So if we manage to add those two calls in place of the words, it will read their translations(See in Polylang admin)
<?php echo pll__('Published:'); ?><?php echo pll__('Updated:'); ?>Thank you very much for your help, but I don’t want to be a pain with this. If you think it is complicated or too much outside of the GB help I would understand.
For me they are small challenges that I enjoy looking for other possible ways to achieve. ๐
-
Upon checking,
pll_register_string('Publicado:', 'Published:'); pll_register_string('Actualizado:', 'Updated:');are outputing Spanish, so there’s nothing to replace in your initial element.So i changed your text block to
INLINE Ying Help Code -Published: {{post_date}} | Updated: {{post_date type:modified}} |, and now the code translatespublishedandupdatedto Spanish, please check ๐ -
jrmora
Oh! So simple, I wouldn’t have guessed it in a thousand years ๐
It works perfect, now both words are translated in all languages.
I’m going to apply it to the production site right away.
I will never tire of saying it. You are the best. Thank you so much, just for these great details with the help and your patience is worth every dollar paid for GB + GP.
-
Glad to hear that ๐
It is my pleasure โค๏ธ
- You must be logged in to reply to this topic.