Difference between revisions of "Internationalization/posix locale"

(Checking for default locale: another method)
Line 56: Line 56:
 
and so on for LC_CTYPE.  
 
and so on for LC_CTYPE.  
  
For more information please read [http://www.opengroup.org/onlinepubs/007908799/xbd/locale.html local-Specification of Unix Open]
+
For more information please read [http://www.opengroup.org/onlinepubs/007908799/xbd/locale.html local-Specification of Unix Open].
 +
Another paper auf dem Linux Kongress 1995 in Berlin from Jochen Hein is also useful, see [http://www.jochen.org/vortraege/nls-1995.pdf National Language Support]
 +
 
 +
==Retrieving local information: LC_NUMERIC ==
 +
 
 +
Comparing LC_NUMERIC formats in posix,en_US, de_DE,zh_CN
 +
1.C-codes and their outputs:
 +
===posix===
 +
#include <stdio.h>
 +
#include <langinfo.h>
 +
 
 +
int main (int arg,char** argv){
 +
        printf("%d\n",12345678);
 +
        printf("%'d\n",12345678);
 +
        printf("%f\n",123456.78);
 +
        printf("%'f\n",123456.78);
 +
        printf("%1$d:%2$.*3$d\n",12,34,2);
 +
}
 +
Outputs are:12345678 12345678 123456.780000 123456.780000 12:34
 +
===en_US===
 +
#include <stdio.h>
 +
#include <langinfo.h>
 +
#include <locale.h>
 +
 
 +
int main (int arg,char** argv){
 +
        setlocale(LC_NUMERIC,"en_US");
 +
        printf("%d\n",12345678);
 +
        printf("%'d\n",12345678);
 +
        printf("%f\n",123456.78);
 +
        printf("%'f\n",123456.78);
 +
        printf("%1$d:%2$.*3$d\n",12,4,2);
 +
}
 +
Outputs are:12345678 12,345,678 123456.780000 123,456.780000 12:04
 +
===de_DE===
 +
int main (int arg,char** argv){
 +
        setlocale(LC_NUMERIC,"de_DE");
 +
        printf("%d\n",12345678);
 +
        printf("%'d\n",12345678);
 +
        printf("%f\n",123456.78);
 +
        printf("%'f\n",123456.78);
 +
        printf("%1$d:%2$.*3$d\n",12,34,2);
 +
       
 +
       
 +
 
 +
}
 +
Outputs are:12345678 12.345.678 123456,780000 123.456,780000 12:34
 +
===zh_CN===
 +
setlocale(LC_NUMERIC,"zh_CN"); only change to above code
 +
Outputs are:12345678 12,345,678 123456.780000 123,456.780000 12:34
 +
===Analysing results===
 +
Look at all the locale source files, the results are resonable to us.
 +
 
 +
Feld         
 +
Posix
 +
en_US
 +
de_DE
 +
zh_CN
 +
decimal_point
 +
"<U002E>"
 +
"<U002E>"
 +
"<U002C>"
 +
"<U002E>"
 +
thousands_sep
 +
""
 +
"<U002C>"
 +
"<U002E>"
 +
"<U002C>"
 +
grouping
 +
-1
 +
3;3
 +
3;3
 +
3
 +
 
 +
Where "<U002E>" means "dot"; "<U002C>" means "comma".

Revision as of 01:51, 7 September 2006


Summary

My Summary is based on article by Michael Becker (see here).This summary is more or less concerning SuSE-Linux.

There are differences between User-locale and C-locale. C-locale, is the local, which is only effective in C-environment. If there are no locales set in user environment, then C-locale is effective, which is so called "POSIX"-locale.

With C-command setlocale (LC_ALL,"") one set C-locale with User-locale. So one could retrieve details about User-locale information with C-functions.

Listing available locales

There are two directories that one could get information about locales. They are: /usr/share/locale/ /usr/share/i18n/

In the first directory, the locales are binary files. So we could not need them for our purpose since i do not know how to read them for the moment.

In the second directory there are at least 2 subdirectories, with names charmaps and locales.

Shell command ls /usr/share/i18n/locales/ list all the available locales.

Checking for default locale

With shell command "locale" one can get infomation about default locale. In C, to get details of localeone can use

setlocale (LC_ALL,"")
localeconv()

see manpage locale(7) to get more information.

Another way to obtain the current locale is to use the environnement variable LANGUAGE, this is done with the following code:

environement : EXECUTION_ENVIRONMENT
...
environement.get ("LANGUAGE")
    -- returns the (rank)list (as string) of languages set by the user

Retrieving locale information

C-command nl_langinfo()(defined in langinfo.h) one could get information about all categories. For example, with nl_langinfo(D_T_FMT), one get a pointer at a string, which in form of %a %d %b %Y %T %Z.

Other information

Using a parser one could also retrieve locale information, about which I do not have a lot of idea.

Still there are C-functions to use categories. For example,

strftime()
strptime()
wcsftime()

for LC_TIME For another example,

isalnum
isalpha
isblank

and so on for LC_CTYPE.

For more information please read local-Specification of Unix Open. Another paper auf dem Linux Kongress 1995 in Berlin from Jochen Hein is also useful, see National Language Support

Retrieving local information: LC_NUMERIC

Comparing LC_NUMERIC formats in posix,en_US, de_DE,zh_CN 1.C-codes and their outputs:

posix

  1. include <stdio.h>
  2. include <langinfo.h>

int main (int arg,char** argv){

       printf("%d\n",12345678);
       printf("%'d\n",12345678);
       printf("%f\n",123456.78);
       printf("%'f\n",123456.78);
       printf("%1$d:%2$.*3$d\n",12,34,2);

} Outputs are:12345678 12345678 123456.780000 123456.780000 12:34

en_US

  1. include <stdio.h>
  2. include <langinfo.h>
  3. include <locale.h>

int main (int arg,char** argv){

       setlocale(LC_NUMERIC,"en_US");
       printf("%d\n",12345678);
       printf("%'d\n",12345678);
       printf("%f\n",123456.78);
       printf("%'f\n",123456.78);
       printf("%1$d:%2$.*3$d\n",12,4,2);

} Outputs are:12345678 12,345,678 123456.780000 123,456.780000 12:04

de_DE

int main (int arg,char** argv){

       setlocale(LC_NUMERIC,"de_DE");
       printf("%d\n",12345678);
       printf("%'d\n",12345678);
       printf("%f\n",123456.78);
       printf("%'f\n",123456.78);
       printf("%1$d:%2$.*3$d\n",12,34,2);
       
       

} Outputs are:12345678 12.345.678 123456,780000 123.456,780000 12:34

zh_CN

setlocale(LC_NUMERIC,"zh_CN"); only change to above code Outputs are:12345678 12,345,678 123456.780000 123,456.780000 12:34

Analysing results

Look at all the locale source files, the results are resonable to us.

Feld Posix en_US de_DE zh_CN decimal_point "<U002E>" "<U002E>" "<U002C>" "<U002E>" thousands_sep "" "<U002C>" "<U002E>" "<U002C>" grouping -1 3;3 3;3 3

Where "<U002E>" means "dot"; "<U002C>" means "comma".