I was trying to translate some plugin when I see in their .po file there are two "Sign in"s. I believe Wordpress uses __
to parse text that needs to be translated.
So when codes like
__('Sign in', 'buddyboss-theme')
is executed, how does it know which "Sign in" entry in the .po file is the one to look for?
I was trying to translate some plugin when I see in their .po file there are two "Sign in"s. I believe Wordpress uses __
to parse text that needs to be translated.
So when codes like
__('Sign in', 'buddyboss-theme')
is executed, how does it know which "Sign in" entry in the .po file is the one to look for?
Share Improve this question asked Jan 28, 2022 at 2:28 shenkwenshenkwen 1311 silver badge9 bronze badges 1 |4 Answers
Reset to default 0WordPress' translation mechanism looks not only for the string of the translation functions like __( 'string', 'key' )
. It looks also for the key, in your case the 'buddyboss-theme'
and search only in a translation file with this key buddyboss-theme-en_US.po
.
Reference https://make.wordpress.org/polyglots/handbook/plugin-theme-authors-guide/
If there is any specific motive to have two translations to the same sentence, you should only have one, delete the second one and translate the first one the way you want. Or better, use one with capital and other not, like so "Sign in" and "sign in", Here the capitals letter matter. It's possible to get more information on the wpml documentation
This sounds like a bug in the generation of the .po
file. You should not have two different translations to the same text, unless the translation uses a context (_x
function IIRC) in which case it should be indicated in the .po
file.
Whatever is the cause, the probable answer to you question is that you can't assume which translation will be used as it will depend on the code which parses the .mo
file generate from the .po
file. One will most likely override the other, but it will be wrong to assume which.
When there are two identical entries in a .po file, how does the translation mechanism determine which one to use?
It doesn't, WordPress is unaware that there were 2 entries as __
etc doesn't read the file directly.
When a po/mo file is loaded, it's processed top to bottom, each entry has a key based on a combination of its context and its singular value.
This gets loaded into memory, and as we can see in the PO/MO loader class, this is how the data is ingested:
$this->entries[ $entry->key() ] = &$entry;
So the second entry would overwrite the first when it is processed, there is never a situation where WP would have to choose between the two, it only ever sees one entry.
__
doesn't do the loading of the file, it just looks up the results of loading which happened in a different function at an earlier time – Tom J Nowell ♦ Commented Feb 11, 2022 at 20:39