c++ - boost 语言环境格式

标签 c++ boost format

可以the boost locale format facility按照它们遵循的顺序格式化参数?

format("Today {1} I would meet {2} at home") % time(0) % name

我想要这样的东西

format("Today {0} I would meet {0} at home") % time(0) % name

产生与上述示例相同的结果。

最佳答案

看起来不像。但是,您可以到达那里。

我以前一直以为Boost Locale是直接使用Boost Format的,但是貌似他们加了一些东西:

std::cout << boost::locale::format("writing %s,  x=%s : %d-th step \n") % "toto" % 40.23 % 50; 
std::cout << boost::format("writing %s,  x=%s : %d-th step \n") % "toto" % 40.23 % 50; 

打印:

writing %s,  x=%s : %d-th step 
writing toto,  x=40.23 : 50-th step 

所以,如果你想要这个,你应该混合使用 Boost Locale 替换和 Boost Format 格式化:

之前:

Live On Coliru 带有位置参数:

#include <boost/date_time.hpp>
#include <boost/locale.hpp>
#include <boost/format.hpp>
#include <iostream>

int main()
{ 
    boost::locale::generator gen;
    gen.add_messages_domain("messages");
    gen.add_messages_path(".");

    for(auto loc_id : { "de_DE", "en_US", "nl_NL", "nl_BE", "fr_FR", "ru_RU" })
    {
        namespace as = boost::locale::as;
        using boost::locale::translate;
        using boost::locale::format;

        auto loc = gen.generate(std::string(loc_id) + ".UTF-8");
        std::cout.imbue(loc);
        std::cout << "---------------------------\n";

        boost::locale::date_time start_date(loc);
        start_date += boost::locale::period::day(3);
        boost::locale::date_time aug(boost::locale::period::august(), loc);

        std::cout << format(translate("{1}: Task runs every {2,ordinal} week from this {3,ftime='%A'} ({3,date})\n")) % loc_id % 3 % start_date;

        std::cout << format(translate("First period ({1}) bill is {2}\n")) 
                        % (format("{1,ftime='%B'}") % aug)
                        % (format("{1,currency}") % 42);
    }
} 

之后

Live On Coliru 带有 sprintf 非重新排序参数:

#include <boost/date_time.hpp>
#include <boost/locale.hpp>
#include <boost/format.hpp>
#include <iostream>

int main()
{ 
    boost::locale::generator gen;
    gen.add_messages_domain("messages");
    gen.add_messages_path(".");

    for(auto loc_id : { "de_DE", "en_US", "nl_NL", "nl_BE", "fr_FR", "ru_RU" })
    {
        namespace as = boost::locale::as;
        using boost::locale::translate;
        using boost::locale::format;

        auto loc = gen.generate(std::string(loc_id) + ".UTF-8");
        std::cout.imbue(loc);
        std::cout << "---------------------------\n";

        boost::locale::date_time start_date(loc);
        start_date += boost::locale::period::day(3);
        boost::locale::date_time aug(boost::locale::period::august(), loc);

        std::cout << boost::format(translate("%s: Task runs every %s week from this %s (%s)\n").str(loc), loc) 
                        % loc_id
                        % (boost::locale::format("{1,ordinal}") % 3)
                        % (boost::locale::format("{1,ftime='%A'}") % start_date)
                        % (boost::locale::format("{1,date}") % start_date)
                        ;

        std::cout << boost::format(translate("First period (%s) bill is %s\n").str(loc), loc) 
                        % (format("{1,ftime='%B'}") % aug)
                        % (format("{1,currency}") % 42);
    }
} 

注意事项

请注意,像这样将 Boost 格式与 boost::locale::format 混合使用时,需要牢记一些微妙的要点(与流操作应用的确切顺序和需要将 loc 传递给对 boost::format 的每次调用)。我花了大约 10 分钟的时间才找出适合这个答案的咒语。

另请注意,此示例是您为何需要位置参数的绝佳示例。单词和句子的顺序可能取决于本地化语言。

关于c++ - boost 语言环境格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27218723/

相关文章:

c++ - 如何从 `var (*)(void*, var)` 中获取 C 指针(如 `boost::function<var (void*, var)>` )?

c++ - 将 pos_infin 作为超时传递给 timed_wait 时,年份超出有效范围

c++ - Linux/CUPS 打印示例/教程?

c++ - 使用 SQlite3 C 库进行全文搜索,字符 "-"不起作用

c++ - 如何使用 Boost.Asio 的生成(堆栈协程),使其仅依赖于 Boost.Context?

java - 如何创建带有可选时间参数的 DateFormat?

vba - 如何标准化范围内列出的文件名

javascript - 使用 Javascript 格式化 NdefFormatable NFC 标签

c++ - SAX 在 C++ 中解析用户对象的片段?

c++ - 如何加快大字符串的解析速度?