You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@stdcxx.apache.org by Dipak Bapatla <de...@sbcglobal.net> on 2006/02/14 22:11:05 UTC

How to setup the locales

Hi,
   
  How can I get locales to work with the library? If I use 
  locale loc1 = std::locale("");
  the code works fine.
   
  But if I use any other locale it fails because of an exception.  
   
  There is nothing built in the nls directory in my BUILDDIR to copy to the lib directory of my installation. What do I need to do to build the locales. I have tried doing gmake util but it doesn't help. It build locale and localedef but there is nothing in the nls directory.
   
  One thing that I noticed in the scripts is that they use the keyword function and /bin/sh as the interpreter.
   
  The following code fails
  #!/bin/sh
  function hello {
    echo "Hello";
};
  hello

  ./test.sh: function: not found
Hello
./test.sh: syntax error at line 5: `}' unexpected

  where as the one below works
   
  #!/bin/sh
  hello() {
    echo "Hello";
};
  hello

  If I use /bin/ksh,
  the first one works fine
   
  thanks,
  Dipak

Re: How to setup the locales

Posted by Martin Sebor <se...@roguewave.com>.
Dipak Bapatla wrote:
> Hi,
>    
>   How can I get locales to work with the library? If I use 
>   locale loc1 = std::locale("");
>   the code works fine.
>    
>   But if I use any other locale it fails because of an exception.  

There are two locale modes in stdcxx. By default, the library tries
to use the locale installed on the system (run locale -a to see a
list of such locales). If it can't find any such locale it switches
from this mode to a mode where it tries to find a locale that was
built using the included localedef utility. By default it looks in
the current working directory but the directory can be overridden
by setting the RWSTD_LOCALE_ROOT environment variable.

>    
>   There is nothing built in the nls directory in my BUILDDIR to copy to
> the lib directory of my installation. What do I need to do to build the
> locales. I have tried doing gmake util but it doesn't help. It build
> locale and localedef but there is nothing in the nls directory.

There is a set of locales and encodings that the makefile "knows"
how to build, everything else needs to be done by manually invoking
the localedef utility. The utility is documented here:
http://svn.apache.org/viewcvs.cgi/*checkout*/incubator/stdcxx/trunk/doc/stdlibref/localedefutility.html

The default set of locales and their encodings is listed in the
following table:
http://svn.apache.org/viewcvs.cgi/*checkout*/incubator/stdcxx/trunk/doc/stdlibref/locale.html#Table%A021

To build one of these preset locales, say, es_ES.ISO-8859-1, you
would cd to $BUILDDIR and enter the following command

     $ nice make -Cbin es_ES.ISO-8859-1

This creates the es_ES.ISO-8859-1 locale in $BUILDDIR/nls.

To use the locale you can either name it using its full pathname
or just use its file name and set the RWSTD_LOCALE_ROOT variable
in the environment of the process. For example, given the program
below compiled in $BUILDDIR/examples, you can invoke it like so
to get it to use the locale:

     $ RWSTD_LOCALE_ROOT=../nls/ ./t
     mar 14 feb 2006 16:43:50 MST

     $ cat t.cpp

#include <iostream>
#include <locale>
#include <ctime>

int main ()
{
     try {
         std::wcout.imbue (std::locale ("es_ES.ISO-8859-1"));
     }
     catch (...) {
         std::cerr << "Failed to set locale\n";
         return 1;
     }

     // obtain the local time
     const std::time_t t = std::time (0);
     std::tm* const tmb = std::localtime (&t);

     typedef std::time_put<wchar_t>            TimePut;
     typedef std::ostreambuf_iterator<wchar_t> Iter;

     const wchar_t fmt[] = L"%c";

     // format time
     const TimePut &tp = std::use_facet<TimePut>(std::wcout.getloc ());
     const Iter end = tp.put (Iter (std::wcout.rdbuf ()), std::wcout,
                              std::wcout.fill (), tmb, fmt, fmt + 2);

     std::wcout << '\n';
}

There is another similar example for codecvt_byname on this page:
http://svn.apache.org/viewcvs.cgi/*checkout*/incubator/stdcxx/trunk/doc/stdlibref/codecvt-byname.html

>    
>   One thing that I noticed in the scripts is that they use the keyword
> function and /bin/sh as the interpreter.
>    
>   The following code fails
>   #!/bin/sh
>   function hello {
>     echo "Hello";
> };
>   hello
> 
>   ./test.sh: function: not found
> Hello
> ./test.sh: syntax error at line 5: `}' unexpected
> 
>   where as the one below works
>    
>   #!/bin/sh
>   hello() {
>     echo "Hello";
> };
>   hello
> 
>   If I use /bin/ksh,
>   the first one works fine

Let me look into it.

Thanks
Martin