Oh, I guess your version of the filter only translates given the specific locale, i.e. FI, while in my case the translation will take place no matter the locale. Correct?
Thanks,
G.
Oh, I guess your version of the filter only translates given the specific locale, i.e. FI, while in my case the translation will take place no matter the locale. Correct?
Thanks,
G.
Hi Gabriel,
The flickering shouldn’t happen since it’s a server cache and the translation is done through server. Perhaps you have some translation done through javascript or jQuery?
And yes, the code will only work for locale FI and it’s done that way since it’s not using any WPML. Plus, locale can be overriden so it’s only given and tested to his current setup and may vary.
Thanks!
I don’t have any JS or jQuery translation that I’m aware of, strange …
Would you say there is anything wrong with the code for singular/plural translations above?
G.
Hi Gabriel,
It’s may not work because the translation is already called before the hook is invoked. So checking the existence of %d is not valid anymore. The code responsible for that is this
362 function _n( $single, $plural, $number, $domain = 'default' ) {
363 $translations = get_translations_for_domain( $domain );
364 $translation = $translations->translate_plural( $single, $plural, $number );
365
366 /**
367 * Filters the singular or plural form of a string.
368 *
369 * @since 2.2.0
370 *
371 * @param string $translation Translated text.
372 * @param string $single The text to be used if the number is singular.
373 * @param string $plural The text to be used if the number is plural.
374 * @param string $number The number to compare against to use either the singular or plural form.
375 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
376 */
377 return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
378 }
Hence, please change your code to this
add_filter( 'ngettext', 'x_translate_text_plurals', 999, 3);
function x_translate_text_plurals ( $translated_text, $single, $plural ) {
if ( preg_match("/^\d Item[s]?$/", $translated_text ) ) {
$translated_text = preg_replace( '/ Item[s]?$/', '', $translated_text );
}
return $translated_text;
}
Thanks!
Thanks for the update. It’s weird, it seems to be working after all. If not I’ll give your alternative a try.
Wonderful response anyhow!
G.
You are most welcome. 
Hi!
Sorry my answer has taken so long. I managed to do the translation with your code. Thank you so much.
I have the same issues here, could you please send me the same code for that:
https://koulu.wpengine.com/category/kotitiedote/
Hey Anni,
Basically, you just need to copy the text in the front-end and add it in the code provided by Rad but making sure you follow the syntax like this:

If it does not work, you will need to check what’s in the .pot file because sometimes, it might include a space.


As you can see there, I used the HTML entity of the non-breaking space because inputting a space after the translated text doesn’t work so it’s also something to take into account.

Hope that helps.
Hi, sorry…too difficult for me. Where I will add it in the function.php file? In the end? What else I need than just the translation?
Last time I added this:
add_filter( 'gettext', 'x_translate_these_strings', 20, 3 );
function x_translate_these_strings( $translated_text, $text, $domain ) {
$locale = get_locale();
$string_table = array (
'fi' => array (
'Post Archive by Month' => 'your translation here',
'Read More' => 'your translation here',
'Below you\'ll find a list of all posts from ' => 'your translation here',
)
);
$translation = $string_table[ $locale ][ $translated_text ];
return !empty( $translation ) ? $translation : $translated_text;
}
I don´t know, do I need to copy all this above again, or just add the new translation in somewhere in the middle. I am sorry, I am not familiar with the code at all — with copy-pasting - yes 
Hi Anni,
Please add that code at the end of your child theme’s functions.php (not the main theme’s functions.php). And please follow Christians recommendation about the space.
Then you need to change the your translation here with your preferred translation.
These are what you just need to change once added to your child theme’s functions.php
'Post Archive by Month' => 'your translation here',
'Read More' => 'your translation here',
'Below you\'ll find a list of all posts from ' => 'your translation here',
Thanks!
Actually, now when I downloaded the function php, the translation place where there ready. I didn’t know that. I it´s done. Thank you!
Still missing some translation.
I tried to add this to function.php:
‘TYPE AND PRESS “ENTER” TO SEARCH’ => ‘KIRJOITA HAKUSANA JA PAINA “ENTER”’,
‘Search Results’ => ‘Hakutulos’,
‘Below you’ll see everything we could locate for your search of “yhteystiedot”’ => 'Alta löydät sisältöä, joka löytyi hakusanalla ',
After what I have added. The whole page did not work anymore. So, what I did wrong?
Hello Anni,
Please make sure that you have place the correct quotes and commas.
You should be using this:
add_filter( 'gettext', 'x_translate_these_strings', 20, 3 );
function x_translate_these_strings( $translated_text, $text, $domain ) {
$locale = get_locale();
$string_table = array (
'fi' => array (
'TYPE AND PRESS "ENTER" TO SEARCH' => 'KIRJOITA HAKUSANA JA PAINA "ENTER"',
'Search Results' => 'Hakutulos',
"Below you'll see everything we could locate for your search of" => "Alta löydät sisältöä, joka löytyi hakusanalla",
)
);
$translation = $string_table[ $locale ][ $translated_text ];
return !empty( $translation ) ? $translation : $translated_text;
}
Hope this helps. Please let us know how it goes.
Hi!
If I just add this one in the end of the function php, the page not show anymore and gives an error: HTTP ERROR 500
I added just this one:
add_filter( ‘gettext’, ‘x_translate_these_strings’, 20, 3 );
function x_translate_these_strings( $translated_text, $text, $domain ) {
$locale = get_locale();
$string_table = array (
'fi' => array (
'TYPE AND PRESS "ENTER" TO SEARCH' => 'KIRJOITA HAKUSANA JA PAINA "ENTER"',
'Search Results' => 'Hakutulos',
'Below you'll see everything we could locate for your search of' => 'Alta löydät sisältöä, joka löytyi hakusanalla',
)
);
$translation = $string_table[ $locale ][ $translated_text ];
return !empty( $translation ) ? $translation : $translated_text;
}
Please help me where I need to add this and what I need to check. I don’t understand the message before. I have copied all and I changed a few marks to be ’ not ".
Hi Anni,
There was a typo error, please change this
'Below you'll see everything we could locate for your search of'
to this
'Below you\'ll see everything we could locate for your search of'
The \ is important , but could be any of these
"Below you\"ll see everything we could locate for your search of"
"Below you'll see everything we could locate for your search of"
It depends on what you use to wrap your string like ' or ". Use \ if you’ll add the same apostrophe.
Hope this helps.
Stil not working, it brokes my page:
add_filter( ‘gettext’, ‘x_translate_these_strings’, 20, 3 );
function x_translate_these_strings( $translated_text, $text, $domain ) {
$locale = get_locale();
$string_table = array (
'fi' => array (
'TYPE AND PRESS "ENTER" TO SEARCH' => 'KIRJOITA HAKUSANA JA PAINA "ENTER"',
'Search Results' => 'Hakutulos',
'Below you\'ll see everything we could locate for your search of ' => 'Alta löydät sisältöä, joka löytyi hakusanalla',
)
);
$translation = $string_table[ $locale ][ $translated_text ];
return !empty( $translation ) ? $translation : $translated_text;
}
Hi There,
Please update your code to this:
add_filter( 'gettext', 'x_translate_these_strings', 20, 3 );
function x_translate_these_strings( $translated_text, $text, $domain ) {
$locale = get_locale();
$string_table = array (
'fi' => array (
'Post Archive by Month' => 'Tiedotearkisto',
'Read More' => 'Lue lisää',
"Below you\'ll find a list of all posts from " => 'Tiedotteet kuukaudelta ',
'Category Archive' => 'Kategoriat',
"Below you\'ll find a list of all posts that have been categorized as " => 'Tiedotteet, jonka kategoria on ',
)
);
$translation = isset($string_table[ $locale ][ $translated_text ]) ? $string_table[ $locale ][ $translated_text ] : '';
return !empty( $translation ) ? $translation : $translated_text;
}
Let us know how it goes!
Thank you! Finally it´s done 
Bad news is that I still found something to be translated. Can I just add them after the last one? They are in the Search, so not in the same place on the page.
I hope there would be easier way to translate the text. Do you know is the po. translation now possible, since the whole thing started that there were something wrong?
Hi Anni,
They should be translatable using PO files but we’re not sure why it’s failing on your site. And if you’ll add new translation through a code, you simply need to edit that existing one instead of adding another block of code. Example, let say this part
'Post Archive by Month' => 'Tiedotearkisto',
'Read More' => 'Lue lisää',
"Below you\'ll find a list of all posts from " => 'Tiedotteet kuukaudelta ',
'Category Archive' => 'Kategoriat',
"Below you\'ll find a list of all posts that have been categorized as " => 'Tiedotteet, jonka kategoria on ',
You’ll just need to add a new line separate by comma
'word' => 'word',
'word' => 'word',
'word' => 'word',
Example,
'Post Archive by Month' => 'Tiedotearkisto',
'Read More' => 'Lue lisää',
"Below you\'ll find a list of all posts from " => 'Tiedotteet kuukaudelta ',
'Category Archive' => 'Kategoriat',
"Below you\'ll find a list of all posts that have been categorized as " => 'Tiedotteet, jonka kategoria on ',
'Search' => 'Hae'
And I just added 'Search' => 'Hae'.
I’ll continue checking about PO translation issue, but I can’t guarantee a solution as translation is very broad.
Hope this helps.
This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.