std::format 不支持本地化,因其设计上强制使用 C locale 以保证跨平台一致性;它忽略所有 locale 设置,不识别 L 标志,数字/时间/货币格式均硬编码为 C locale 行为。

C++20 的 std::format 不支持本地化(L10N)和国际化(I18N)——它完全忽略当前 locale,所有格式化行为都是“C locale 固定”的。 这是标准明确规定的限制,不是实现缺陷,也不是你漏配了什么 flag。
为什么 std::format 不处理 locale?
标准委员会在设计 std::format 时,将“可预测性”和“跨平台一致性”置于 locale 敏感性之上。比如 {:.2f} 在任何系统上都必须输出小数点(.),而不是逗号(,);千位分隔符、货币符号、日期缩写等 locale 特有行为被**完全排除在 format 规范之外**。
-
std::format的解析器不识别L标志(如{:Ld})、不读取std::locale设置 - 即使你传入
std::locale{"de_DE.UTF-8"},std::format也**不会使用它** - 所有数字、时间、货币的格式化逻辑都硬编码为 C locale 行为
替代方案:用 std::locale + std::stringstream 或 std::put_time
需要真正本地化的场景(如带千位分隔符的数字、本地星期名、货币符号),必须绕过 std::format,改用传统流机制:
std::locale loc{"zh_CN.UTF-8"};
std::ostringstream oss;
oss.imbue(loc);
oss << std::showbase << std::put_money(1234567LL); // 输出:¥12,345.67
std::string result = oss.str();
-
std::put_money、std::put_time、std::numpunct等 facet 才真正尊重std::locale -
std::format的{:%Y-%m-%d}会输出英文月份,而std::put_time在中文 locale 下可输出“2024年04月05日” - 注意:Linux/macOS 上 locale 名称如
zh_CN.UTF-8需系统实际安装;Windows 使用Chinese_China.936等名称,且支持度有限
第三方库:fmt 库的 fmt::format(locale, ...) 是可行路径
如果你坚持用类 std::format 的语法但需要 locale 支持,fmt 库(C++20 std::format 的上游实现)提供了扩展:
立即学习“C++免费学习笔记(深入)”;
auto loc = std::locale{"de_DE.UTF-8"};
std::string s = fmt::format(loc, "{:L}", 1234567); // 输出:1.234.567
- 需启用
FMT_LOCALE宏并链接libfmt -
{:L}是fmt特有标志,std::format中非法 - 日期、货币等仍需配合
fmt::localtime和自定义模板,非开箱即用
真正做 I18N 时,格式化只是冰山一角;字符串翻译、复数规则、双向文本、时区感知等,都得靠 gettext、ICU 或现代框架(如 Qt Linguist)支撑。std::format 的定位很清晰:高性能、无 locale 依赖的通用格式化——别指望它替你解决 L10N。










