期望使用UTF8

好的国际化应用程序不会假设字符中的字节数。这意味着你不应该使用指针算数遍历字符串中的字符,也意味着你不应该使用std::string以及像strlen()这样的与字符串有关的标准C函数,因为他们都对字符的字节数做了假设。

你可能已经通过使用std::string避免了使用原始的char*char[]进行指针算数。那么你只需要换成使用Glib::ustring即可。更多详情请参阅Glib::ustring基础章节。

25.3.1. Glib::ustring和std::iostreams

不幸的是,与标准iostreams的集成并非绝对安全的。如果你将Glib::ustring使用operator<<输出到ostream中,则gtkmm会将Glib::ustring转换为使用特定语言编码(通常不会是UTF-8编码)。而使用operator>>istream输入到Glib::ustring会发生相反的转换。但是如果你通过std::string进行则该方案会失效,例如,从文本输入流转换成std::string再隐式转换为Glib::ustring。如果该字符串中包含非ASCII字符且当前的语言环境不使用UTF-8编码,则得到的结果是一个损坏的Glib::ustring。你可以通过手动转换编码解决此问题。例如,若要从ostringstream中接受std::string

std::locale::global(std::locale("")); // Set the global locale to the user's preferred locale.
                                      // Usually unnecessary here, because Glib::init()
                                      // or Gtk::Application::create() does it for you.
std::ostringstream output;
output << percentage << " % done";
label->set_text(Glib::locale_to_utf8(output.str()));