When there are two identical entries in a .po file, how does the translation mechanism determine which one to use?

I was trying to translate some plugin when I see in their .po file there are two Sign ins. 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?

Topic plugin-qtranslate Wordpress

Category Web


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.


WordPress' 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/


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.


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.