最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin qtranslate - When there are two identical entries in a .po file, how does the translation mechanism determine which one t

programmeradmin3浏览0评论

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
  • __ 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
Add a comment  | 

4 Answers 4

Reset to default 0

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/

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论