什么是升压::语言环境和std ::区域之间的权衡? [英] What are the tradeoffs between boost::locale and std::locale?

查看:143
本文介绍了什么是升压::语言环境和std ::区域之间的权衡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在国际化C ++中的大笔遗产codeBase的过程中,并且我面临着一个艰难的决定:我应该使用boost ::语言环境或std C ++语言环境

I am in the process of internationalizing a large legacy codebase in C++, and I am faced with a difficult decision: should I use boost::locale's or std c++ locales?

我要COMMITED使用UTF-8。我们必须做一个合理的范围广泛的文字处理,虽然这不是什么我们code呢,它是重要的核心。我们可以期望做的大部分是人们可能需要做的:时间,日期,数字和货币格式,整理,正则表达式,串隔离,以互动的boost ::文件系统,数据库访问等。

I am commited to using utf-8. We have to do a reasonably broad range of text processing, although it is not the core of what our code does, it is important. We can expect to do most of what one might need to do: time, date, number, and money formatting, collation, regexp, substring isolation, interaction with boost::filesystem, DB access, etc.

借助介绍的boost ::区域我得到


  1. 设置全局区域设置有副作用(CSV例子)。它会影响printf和boolst lexical_cast的。有些第三方库可以打破。

  2. 数字格式被打破一些区域设置的。

  3. 语言环境名称不规范。

  4. 许多厂商只提供C和POSIX,只在Linux下GCC这样的本地化支持。

我有麻烦评估点1的影响我想点2是pretty严重,如果它影响到我们,广告3和4将不会是一个大问题我们。

I have trouble evaluating the impact of point 1 I guess point 2 is pretty severe if it affects us, ad 3 and 4 won't be a big deal for us.

有没有在升压::语言环境是更好的选择社会的共识?是否有标准commity任何动议,以解决的std ::语言环境的问题?谁能帮我做出更明智的决定呢?

Is there a consensus in the community that Boost::locale is the better alternative? Is there any motion in the standard commity to address the issues with std::locale's? Can anyone help me make a more informed decision?

也许最重要的是,它是简单的,从一个到另一个迁移?如何做好两部戏彼此?难道是合法的设置和一个升压区域全局区域设置,然后使用std设施?

Perhaps most importantly, is it simple to migrate from one to the other? How well do the two play with one another? Is it legitimate to set the global locale with a boost locale, and then use std facilities?

推荐答案

在结束时,Boost文档不回答我的问题的一个很好的工作,但你必须做一些阅读,它有助于理解的std ::区域比我在发帖的时候更好。

In the end, the boost documentation does a good job of answering my question, but you have to do some reading, and it helps to understand std::locale better than I did at the time of posting.

与性病很好地播放

A 的std ::区域个集合。该标准定义了一组小面,每个区域必须提供的,但除此之外,似乎最留给实现。这包括语言环境的行为,语言环境的名称。

A std::locale is a collection of facets. The standard defines a set of facets which each locale must provide, but other than that it seems most is left to the implementation. This includes locale behavior, and the names of the locales.

什么的boost ::区域确实是提供了一堆刻面,收集到的语言环境,其行为以同样的方式与平台无关的(至少如果你使用的是默认后台ICU)。

What boost::locale does is provide a bunch of facets, collected into locales, that behave the same way regardless of platform (at least if you are using the default ICU backend).

所以的boost ::区域提供一套标准化的std ::的语言环境的可跨平台行为一致,提供了广泛的全面统一code支持文化规范,并与一致的命名。使用非增压的std ::区域(即实现提供了语言环境)和的boost ::区域之间的切换是平凡的,因为它们是相同的类型 - 无论是的std ::方面的集合,虽然实现是不同的。机会是的boost ::区域就做做你想做的工作做得更好。

So boost::locale provides a standardized set of std::locale's which can behave consistently across platforms, provides full Unicode support for a wide range of cultural norms, and with consistent naming. Switching between use of a non boost std::locale (i.e. an implementation provided locale) and a boost::locale is trivial since they are the same types -- both are collections of std::facets, although implementations are different. Chances are the boost::locales do a better job of doing what you want.

完整统一code的支持,对所有的编码,在所有平台上结果
此外,的boost ::区域通过提供的 ICU ,它可以让你获得ICU的好处,而可怜的(不是​​C ++ ISH)ICU的接口。

Complete Unicode support, for all encodings, on all platforms
Further, boost::locale provides a way of accessing complete unicode support through ICU, which allows you to gain the benefits of ICU, without the poor (not C++ish) interface of ICU.

这是有利的,因为统一code的任何标准的支持很可能来通过现场frameork,任何单code识别程序很可能将需要语言环境感知以及(对于排序规则例子)。

This is advantageous, since any standard support of Unicode is very likely to come through the locale frameork, and any unicode aware program is likely going to need to locale aware as well (for collation for example).

有关数字的理智行为
最后,的boost ::区域地址是什么可以合法在的std ::语言环境的实现通常被称为显著的缺陷 - 任何流格式的数字将语言环境的影响,不管这是否是可取的 - 请参见提振文档进行了详细的讨论。

Saner behavior regarding numbers Finally, boost::locale addresses what could legitimately be called a significant flaw in the usual implementations of the std::locales -- any stream formatted number will be affected by locale, regardless of whether this is desirable -- see the boost documentation for a detailed discussion.

因此​​,如果您正在使用的ofstream来读取或写入一个文件,你的globale设置区域设置你的平台的德语语言环境中,你将有逗号分隔您花车小数部分。如果你正在读/写一个CSV文件,这可能是一个问题。如果你使用了的boost ::区域为全局区域,如果你明确告诉它使用语言环境约定为你的数字输入/输出这才会发生。请注意,许多图书馆在后台使用的语言环境信息,包括升压:: lexical_cast的。所以做的std :: to_string,对于这个问题。因此,考虑下面的例子:

So if you are using an ofstream to read or write a file, and you have set the globale locale to your platform's german locale, you'll have commas separating the decimal part of your floats. If you're reading/writing a csv file, that might be a problem. If you used a boost::locale as your global locale, this will only happen if you explicitly tell it to use locale conventions for your numeric input/output. Note that many libraries use locale info in the background, including boost::lexical_cast. So does std::to_string, for that matter. So consider the following example:

std::locale::global(std::locale("de_DE"));

auto demo = [](const std::string& label)
{
    std::cout.imbue(std::locale()); // imbue cout with the global locale.
    float f = 1234.567890;
    std::cout << label << "\n";
    std::cout << "\t streamed:  " << f << "\n";
    std::cout << "\t to_string: " << std::to_string(f) << "\n";
};

std::locale::global(std::locale("C"));//default.
demo("c locale");

std::locale::global(std::locale("de_DE"));//default.
demo("std de locale");

boost::locale::generator gen;
std::locale::global(gen("de_DE.UTF-8"));
demo("boost de locale");

提供了以下的输出:

Gives the following output:

c locale
     streamed:  1234.57
     to_string: 1234.567871
std de locale
     streamed:  1.234,57
     to_string: 1234,567871
boost de locale
     streamed:  1234.57
     to_string: 1234,567871

在code实现(以GUI或终端输出)人类的沟通和机器间通信(CSV文件,XML等),这是不理想的可能性的行为。当使用升压区域,您明确指定,当你想格式化语言环境,ALA:

In code that implements both human communication (output to gui or terminal) and inter-machine communication (csv files, xml, etc) this is likely undesireable behavior. When using a boost locale, you explicitly specify when you want locale formatting, ala:

cout << boost::locale::as::currency << 123.45 << "\n";
cout << boost::locale::as::number << 12345.666 << "\n"

结论

这似乎是的boost ::语言环境应该是pferred通过系统提供的语言环境$ P $。

It would seem that boost::locale's should be preferred over the system provided locales.

这篇关于什么是升压::语言环境和std ::区域之间的权衡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆