【问题标题】:Is there an easy way to programmatically list all the message IDs in a .MO file?有没有一种简单的方法可以以编程方式列出 .MO 文件中的所有消息 ID?
【发布时间】:2009-07-01 18:31:52
【问题描述】:

我有一个很大的 .po 文件,其中包含我的 PHP 应用程序所需的所有面向用户的字符串。我想知道是否有一个 PHP 函数可以用来获取存储在 .po 或 .mo 中的所有 msgid 字符串的列表。

我没有看到发布的 PHP 函数可以执行此操作。任何人都知道类似的东西,还是我必须自己手动解析我的 .po 文件?

这是我最希望看到的:

$msgids = magic_gettext_keys_function('mydomain');
foreach ($msgids as $msgid) {
    do_something_awesome($msgid);
}

【问题讨论】:

    标签: php gettext


    【解决方案1】:

    还没有收到任何答案,我假设没有 API 调用可以为我做这件事。但是,从 .po 文件中提取 msgid 字符串几乎是微不足道的:

    $path = $GLOBALS['LOCALE_DIR'] . '/en/LC_MESSAGES/mydomain.po';
    $poSrc = file_get_contents($path);
    preg_match_all('/msgid\s+\"([^\"]*)\"/', $poSrc, $matches);
    $msgids = $matches[1];
    
    foreach ($msgids as $msgid) {
        do_something_awesome($msgid);
    }
    

    【讨论】:

    • 不幸的是,这不是微不足道的,msgid可以分成多行,每行都用“”包围,你必须转换回转义字符
    猜你喜欢
    • 2023-03-06
    • 2011-11-03
    • 2011-05-21
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多