开发者

Why does gettext translate an empty string to the .po header text?

开发者 https://www.devze.com 2023-04-09 07:29 出处:网络
I\'ve got gettext setup within PHP5. After some struggling with the charactersets and the like, I\'ve managed to translate some basic strings. Now I\'ve ran into a problem. When translating an empty s

I've got gettext setup within PHP5. After some struggling with the charactersets and the like, I've managed to translate some basic strings. Now I've ran into a problem. When translating an empty string I expect to get the untranslated (empty) string back (correct me if I'm wrong ;)).

However instead of returning the input string, I get the header of the .PO file as the translation:

Project-Id-Version: PACKAGE VERSION Report-Msgid-Bugs-To: POT-Creation-Date: 2011-09-22 15:03+0200 PO-Revision-Date: 2011-09-22 15:37+0200 Last-Translator: timo Language-Team: English Language: en MIME-Version: 1.0 Content-Type: text/plain; charset=ASCII Content-Transfer-Encoding: 8bit Plural-Forms: nplurals=2开发者_JS百科; plural=(n != 1); 

When I look in the .mo file with a text-editor I see that the header seems to placed at a random place within the file. I would like to show you, but I can't paste the file in. It looks like there is a list of my msgid's, then the header and then a list of my msgstr's

I've used the following command to generate the .mo file:

msgfmt -c -v -o en/LC_MESSAGES/messages.mo messages_en.po

After this command I've restarted my webserver (lighttpd).

What did I do wrong and how can I fix it?


After searching around with some different queries I stumbled upon this site: http://framework.zend.com/issues/browse/ZF-2914

Here I found that the translation of the empty string to meta information is actually in the specifications of gettext:

This also has another advantage, as the empty string in a PO file GNU gettext is usually translated into some system information attached to that particular MO file, and the empty string necessarily becomes the first in both the original and translated tables, making the system information very easy to find.


You can try creating another function to call gettext only when the message it is different from blank or empty string (""). Something like this in php:

function _tl($msg)
{
    $ret = "";
    if($msg != "")
    {
        $ret = _($msg);
    }    

    return $ret;

}

Then on poedit, on your catalog preferences, on the keywords tab set the using of the new function _tl. This is the manner which poedit will recognize it on your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号