xgettext - 如何提取由空值分割的字符串

问题描述

gst-plugins-base提取字符串(使用 xgettext)存在问题,其中字符串具有空分隔符 -

static const gchar genres[] =
"Blues\000Classic Rock\000Country\000Dance\000disco\000Funk\000Grunge\000"
"Hip-Hop\000Jazz\000Metal\000New Age\000Oldies\000Other\000Pop\000R&B\000"
"Rap\000Reggae\000Rock\000Techno\000Industrial\000Alternative\000Ska\000"
"Death Metal\000Pranks\000Soundtrack\000Euro-Techno\000Ambient\000Trip-Hop"
"\000Vocal\000Jazz+Funk\000Fusion\000Trance\000Classical\000Instrumental\000"
"Acid\000House\000Game\000Sound Clip\000Gospel\000Noise\000Alternative Rock"
"\000Bass\000Soul\000Punk\000Space\000Meditative\000Instrumental Pop\000"
"Instrumental Rock\000Ethnic\000Gothic\000Darkwave\000Techno-Industrial\000"
"Electronic\000Pop-Folk\000Eurodance\000Dream\000Southern Rock\000Comedy"
"\000Cult\000Gangsta\000Top 40\000Christian Rap\000Pop/Funk\000Jungle\000"
"Native American\000Cabaret\000New Wave\000Psychedelic\000Rave\000Showtunes"
"\000Trailer\000Lo-Fi\000Tribal\000Acid Punk\000Acid Jazz\000polka\000"
"Retro\000Musical\000Rock & Roll\000Hard Rock\000Folk\000Folk/Rock\000"
"National Folk\000Swing\000Bebob\000Latin\000Revival\000Celtic\000Bluegrass"
"\000Avantgarde\000Gothic Rock\000Progressive Rock\000Psychedelic Rock\000"
"Symphonic Rock\000Slow Rock\000Big Band\000Chorus\000Easy Listening\000"
"Acoustic\000Humour\000Speech\000Chanson\000Opera\000ChAmber Music\000"
"Sonata\000Symphony\000Booty Bass\000Primus\000Porn Groove\000Satire\000"
"Slow Jam\000Club\000Tango\000Samba\000Folklore\000Ballad\000Power Ballad\000"
"Rhythmic Soul\000Freestyle\000Duet\000Punk Rock\000Drum Solo\000A Capella"
"\000Euro-House\000Dance Hall\000Goa\000Drum & Bass\000Club-House\000"
"Hardcore\000Terror\000Indie\000BritPop\000Negerpunk\000Polsk Punk\000"
"Beat\000Christian Gangsta Rap\000Heavy Metal\000Black Metal\000"
"Crossover\000Contemporary Christian\000Christian Rock\000Merengue\000"
"Salsa\000Thrash Metal\000Anime\000Jpop\000Synthpop";

我使用 xgettext-0.21 来提取字符串 -

xgettext -a --no-wrap ./gst-libs/gst/tag/gstid3tag.c -o -

我只得到一个字符串 -

#: gst-libs/gst/tag/gstid3tag.c:51
msgid "Blues"
msgstr ""

虽然我也应该得到“经典摇滚”、“乡村”、“舞蹈”等......

还有其他方法可以提取这些字符串吗?也许是其他工具或通过 xgettext 命令使用特定标志?

解决方法

无法使用 xgettext 提取此字符串,这是设计使然。即使有办法,也没有可用的工具来编辑包含空字节条目的 po 文件。

解决方案是在运行时或编译时用空字节组合字符串。后者需要一个帮助脚本来生成包含流派列表的源文件。

Perl 中的示例:

#! /usr/bin/env perl

use strict;

# Stub gettext that just returns the argument.
sub gettext {
    shift;
}

my $genres = join '\\000',(
    gettext('Blues'),gettext('Classic Rock'),gettext('Country'),gettext('Dance'),);

print <<EOF;
static const gchar genres[] = "$genres";
EOF

运行脚本将生成所需的 C 代码片段。并将其作为附加源文件提供给 xgettext 会将所有流派添加到您的 po 文件中:

$ xgettext --omit-header -o - genres.pl
#: genres.pl:11
msgid "Blues"
msgstr ""

#: genres.pl:12
msgid "Classic Rock"
msgstr ""

#: genres.pl:13
msgid "Country"
msgstr ""

#: genres.pl:14
msgid "Dance"
msgstr ""

当然,您可以在 xgettext 支持的所有其他语言中做到这一点,而不仅仅是在 Perl 中。选择最容易集成到您的构建系统中的那个。

仅使用不同的分隔符(例如“Blues:Classic Rock:...”)不仅会出现转义问题,还会导致 po 文件难以翻译。