Get and set a stream's locale via getloc() and imbue() on a stream. - C++ Internationalization

C++ examples for Internationalization:Locale

Description

Get and set a stream's locale via getloc() and imbue() on a stream.

Demo Code

#include <iostream>
#include <fstream>
#include <locale>
using namespace std;
int main()//  w  w  w  .  j  av a2  s.  c om
{
   ofstream fout("test.dat");
   if(!fout) {
      cout << "Cannot open file.\n";
      return 1;
   }
   // Display the name of the current locale.
   cout << "The original locale is " << fout.getloc().name();
   cout << "\n\n";
   cout << "Setting the locale to German_Germany.\n";

   // Create a locale object for Germany.
   locale loc("German_Germany");
   // Set fout's locale to loc.

   fout.imbue(loc);
   // Display the name of the new locale.

   cout << "The current locale is now " << fout.getloc().name();
   cout << endl;
   // First, confirm that moneypunct facet is available.

   if(has_facet<moneypunct<char, true> >(fout.getloc())) {
      // Obtain the moneypunct facet.
      const moneypunct<char, true> &mp =
      use_facet<moneypunct<char, true> >(fout.getloc());
      // Display the currency symbol and thousands separator.
      cout << "Money symbol: " << mp.curr_symbol() << endl;
      cout << "Thousands separator: " << mp.thousands_sep() << endl;
   }
   fout.close();
   if(!fout.good()) {
      cout << "Error closing file.\n";
      return 1;
   }
   return 0;
}

Result


Related Tutorials