问题
My .po file contains:
"X-Poedit-KeywordsList: __;_e;__ngettext:1,2;_n:1,2;__ngettext_noop:1,2;"
"_n_noop:1,2;_c,_nc:4c,1,2;_x:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2;_ex:1,2c;"
"esc_attr__;esc_attr_e;esc_attr_x:1,2c;esc_html__;esc_html_e;esc_html_x:1,2c\n"
And my php/wordpress code contains:
_x( 'Add New', $this->post_type , $domain ),
I know that Poedit is scanning that file (all the other translatable strings are picked up on "update catalog", and share the same domain). And if I change _x to __ (and remove the context parameter) it picks that one as well. But as it is written, the "Add New" string simply wont show in PoEdit.
Even if I add manually the string to the po file, it will put it in the "obsolete strings" after trying to update to .po file from sources...
Would have thought that the "_x:1,2c;" bit would be enough to pick up the string, but it never does.
Is there something I'm doing wrong, or have I encountered an actual bug?
回答1:
See the documentation for _x()
at http://codex.wordpress.org/Function_Reference/_x — the second argument is context, which is a string included in the PO together with the source text to disambiguate otherwise identical strings with different meaning. The codex page above has an example; another one is e.g. "Open" that could be used in different context, so the programmer would use e.g. "File menu" as the context.
The important part is that it is handled the same way as the text to be translated. The text has to be a string literal for xgettext
to extract it and so does the context (it has to be: it's something that goes into the PO file!).
$this->post_type
is not a literal, which is why xgettext
doesn't recognize it. There's no way it could know what the post_type value could be at runtime and so it couldn't possibly know what to write into the created PO file.
To fix this, you need to use _x()
with at least the first two arguments string literals.
来源:https://stackoverflow.com/questions/26497272/poedit-doesnt-recognize-x