Boost.Locale: Frequently Asked Questions

| Boost.Locale

Frequently Asked Questions

- Some Boost.Locale functions throw `std::bad_cast` exception?
  Answer: You probably try to use an incorrect `std::locale` object. All Boost.Locale tools rely on `std::locale` object's facets. The locale object should be generated with the generator class and then passed to the function or alternatively global locale should be set using the `std::locale::global()` function such that global locale (and default created one) would have the required facets.
- I have installed global locale, but when I try to write something to a stream I still get the wrong output? For example:
  #include <boost/locale.hpp>
  #include <iostream>
  int main()
  {
  boost::locale::generator gen;
  std::locale::global(gen(""));
  std::cout << boost::locale::as::date << std::time(0) << std::endl;
  }
  boost::locale::generator
  the major class used for locale generation
  Definition: generator.hpp:101
  boost::locale::as::date
  std::ios_base & date(std::ios_base &ios)
  Format a date, number is treated as POSIX time.
  Definition: formatting.hpp:188
  Prints a number instead of a date.
  Answer: You forget to imbue the locale to the stream. Changing the global locale does not affect the locale in existing `iostream` objects. Thus, because `std::out` and other global streams were created before changing the global locale, Boost.Locale manipulators have no effect. You need to write:
  #include <boost/locale.hpp>
  #include <iostream>
  int main()
  {
  boost::locale::generator gen;
  std::locale l = gen("");
  std::locale::global(l);
  std::cout.imbue(l);
  std::cout << boost::locale::as::date << std::time(0) << std::endl;
  }

- © Copyright 2009-2012 Artyom Beilis, Distributed under the Boost Software License, Version 1.0.