Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: provide custom locale to fmt::format #597

Closed
kivadiu opened this issue Nov 4, 2017 · 5 comments
Closed

Enhancement: provide custom locale to fmt::format #597

kivadiu opened this issue Nov 4, 2017 · 5 comments

Comments

@kivadiu
Copy link

kivadiu commented Nov 4, 2017

The library is extremely useful so that I would like to use it everywhere but it uses snprintf to print floats which uses the global locale.

One problem is when the global locale is set to a language specific locale and you still want to write some numbers with the C locale. Or you may just want to write with different locales in the same program.

Changing the global locale when you call fmt::format is not thread safe, so:

  1. is there a solution today to force the C locale in fmt::format if the global locale is different?
  2. if not, would it be possible to have something like fmt::format(locale_name, "format", arg1, arg2, ...);
    but I guess this would mean to rewrite snprintf?
@vitaut
Copy link
Contributor

vitaut commented Nov 4, 2017

The std branch of the library allows passing a locale via a buffer. Unfortunately it doesn't solve the issue with formatting of floating-point numbers because they are formatted with snprintf which uses the global locale. To solve this we need a custom implementation of floating-point formatting (#147). Note that formatting of integers is already locale-independent.

@foonathan
Copy link
Contributor

A couple of years ago I wrote such an implementation, I can look tomorrow whether I can find it

@kivadiu
Copy link
Author

kivadiu commented Nov 4, 2017

Could we copy the implementation of snprintf from the GNU libc?

@beojan
Copy link

beojan commented Nov 5, 2017

No, fmt is BSD licensed. I suppose you could copy it from BSD libc, if you really wanted.

@vitaut
Copy link
Contributor

vitaut commented Nov 12, 2017

Closing in favor of #147, since the problem is in floating-point formatting not locale itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants