Jump to content

Locale Support: Difference between revisions

From EDM2
Ak120 (talk | contribs)
mNo edit summary
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== Obtaining localized information from the system ==
== Obtaining localized information from the system ==
If you want to know how the date is displayed on the current system or how the names of the month are spelled in the currently selected language or what the currently selected language is at all at this time there is not well documented library that comes with ACP/MCP & eCS.


If you want to know how the date is displayed on the current system or how the names of the month are spelled in the currently selected language or what the currently selected language is at all at this time there is not well documented library that comes with ACP/MCP & eCS. LIBULS
==LIBULS==
 
This article is intended to give you a short introduction on the usage of that library.
This article is intended to give you a short introduction on the usage of that library.


in LIBULS there is a whole set of call that support codepage conversion and querying locale information.
In LIBULS there is a whole set of call that support codepage conversion and querying locale information.


This article will give a short quick and dirty sample on how to use it.
This article will give a short quick and dirty sample on how to use it.


Reasons to use the LIBULS functions instead of querying enivronment:
Reasons to use the LIBULS functions instead of querying environment:
* Normaly LANG will contain something usefull like: '''en_us''' or '''de_de''' from which you could parse a language. But '''LANG=myRealyLongStupidLocale''' is allowed as well, now you tell me which language this locale represents.
* Normally [[LANG]] will contain something useful like: '''en_us''' or '''de_de''' from which you could parse a language. But '''LANG=myRealyLongStupidLocale''' is allowed as well, now you tell me which language this locale represents.
* Using the LIBULS allow you to acces not only language names, but a whole set of localized settings
* Using the LIBULS allow you to access not only language names, but a whole set of localized settings
* Using a country number will result in suplying several translations for the same language ( e.g. german is used in 041,049,043) where using the locale will return a common language for all of them : '''de'''
* Using a country number will result in supplying several translations for the same language ( e.g. German is used in 041,049,043) where using the locale will return a common language for all of them : '''de'''


== localetest.c ==
== localetest.c ==
 
Now lets gets hands on with a sample, later I'll describe what every line does here:
Now lets gets hands on with a sample, later I'll descibe what every line does here:
 
<pre>
<pre>
01: #include <stdio.h>
01: #include <stdio.h>
02: #include <unidef.h>
02: #include <unidef.h>
03: #include <uconv.h>
03: #include <uconv.h>
04:  
04: #include <stdlib.h>
05: int main(void)
05:
06: {
06: int main(void)
07:    LocaleObject      locale_object = NULL;
07: {
08:    UconvObject      uconv_object = NULL;
08:    LocaleObject      locale_object = NULL;
09:    UniChar          *pinfo_item;
09:    UconvObject      uconv_object = NULL;
10:    int              rc = ULS_SUCCESS;
10:    UniChar          *pinfo_item;
11:    char              *pszResult;
11:    int              rc = ULS_SUCCESS;
12:    int              iResultSize;
12:    char              *pszResult;
13:
13:    int              iResultSize;
14:  
14:  
15:    rc = UniCreateUconvObject((UniChar *)L"", &uconv_object);
15:
16:    if (rc != ULS_SUCCESS)
16:    rc = UniCreateUconvObject((UniChar *)L"", &uconv_object);
17:          {
17:    if (rc != ULS_SUCCESS)
18:            printf("UniCreateUconvObject error: return code = %u\n", rc);
18:          {
19:            return 1;
19:            printf("UniCreateUconvObject error: return code = %u\n", rc);
20:          }
20:            return 1;
21:
21:          }
22:    rc = UniCreateLocaleObject(UNI_UCS_STRING_POINTER,
22:
23:                                    (UniChar *)L"", &locale_object);
23:    rc = UniCreateLocaleObject(UNI_UCS_STRING_POINTER,
24:    if (rc != ULS_SUCCESS)
24:                                    (UniChar *)L"", &locale_object);
25:        {
25:    if (rc != ULS_SUCCESS)
26:            printf("UniCreateLocaleObject error: return code = %u\n", rc);
26:        {
27:            return 1;
27:            printf("UniCreateLocaleObject error: return code = %u\n", rc);
28:        }
28:            return 1;
29:          /* Query the Abriviated name of the current language */
29:        }
30:          rc = UniQueryLocaleItem(locale_object,
30:          /* Query the Abriviated name of the current language */
31:                                  LOCI_sLanguageID,
31:          rc = UniQueryLocaleItem(locale_object,
32:                                  &pinfo_item);
32:                                  LOCI_sLanguageID,
33:          if (rc != ULS_SUCCESS) {
33:                                  &pinfo_item);
34:            printf("UniQueryLocaleItem error: return code = %u\n", rc);
34:          if (rc != ULS_SUCCESS) {
35:            return 1;
35:            printf("UniQueryLocaleItem error: return code = %u\n", rc);
36:          }
36:            return 1;
37:          rc = UniFreeMem(pinfo_item);
37:          }
38:          if (rc != ULS_SUCCESS) {
38:          rc = UniFreeMem(pinfo_item);
39:            printf("UniFreeMem error: return code = %u\n", rc);
39:          if (rc != ULS_SUCCESS) {
40:            return 1;
40:            printf("UniFreeMem error: return code = %u\n", rc);
41:          }
41:            return 1;
42:  
42:          }
43:    iResultSize = UniStrlen(pinfo_item) + 1;
43:  
44:    pszResult = malloc(iResultSize);
44:    iResultSize = UniStrlen(pinfo_item) + 1;
45:
45:    pszResult = malloc(iResultSize);
46:    rc = UniStrFromUcs(uconv_object, pszResult , pinfo_item, iResultSize);
46:
47:
47:    rc = UniStrFromUcs(uconv_object, pszResult , pinfo_item, iResultSize);
48:    printf("sLanguageID = %s",pszResult);
48:
49:
49:    printf("sLanguageID = %s",pszResult);
50:    rc = UniFreeUconvObject(uconv_object);
50:
51:
51:    rc = UniFreeUconvObject(uconv_object);
52:    return(0);
52:
53: }
53:    return(0);
54: }
</pre>
</pre>


Line 77: Line 76:


Include the header which declares the functions and defines the constants for the locale Items.
Include the header which declares the functions and defines the constants for the locale Items.
<pre>
02: #include <unidef.h>
02: #include <unidef.h>
03: #include <uconv.h>
03: #include <uconv.h>
</pre>
 
 
Declare variables for the API's
Declare variables for the API's
<pre>
08:    LocaleObject      locale_object = NULL;
07:    LocaleObject      locale_object = NULL;
09:    UconvObject      uconv_object = NULL;
08:    UconvObject      uconv_object = NULL;
10:    UniChar          *pinfo_item;
09:    UniChar          *pinfo_item;
</pre>
 
Since all the strings returned are UniCode we need to convert them to current process codepage before we can print them. We need a conversion object for that:
Since all the strings returned are UniCode we need to convert them to current process codepage before we can print them. We need a conversion object for that:
<pre>
16:    rc = UniCreateUconvObject((UniChar *)L"", &uconv_object);
15:    rc = UniCreateUconvObject((UniChar *)L"", &uconv_object);
</pre>
 
Now create the locale_object, note that the second parameter, '''(UniChar *)L""''', a blank locale, will cause to use the locale specified in the '''LANG''' environment.
Now create the locale_object, note that the second parameter, '''(UniChar *)L""''', a blank locale, will cause to use the locale specified in the '''LANG''' environment.
<pre>
23:    rc = UniCreateLocaleObject(UNI_UCS_STRING_POINTER,
22:    rc = UniCreateLocaleObject(UNI_UCS_STRING_POINTER,
24:                                    (UniChar *)L"", &locale_object);
23:                                    (UniChar *)L"", &locale_object);
</pre>
 
Query the locale Item want to use. Look in the advanced page of the '''Country setting''' in your configuration folder to see all the names of supported items.
Query the locale Item want to use. Look in the advanced page of the '''Country setting''' in your configuration folder to see all the names of supported items.
<pre>
31:          rc = UniQueryLocaleItem(locale_object,
30:          rc = UniQueryLocaleItem(locale_object,
32:                                  LOCI_sLanguageID,
31:                                  LOCI_sLanguageID,
33:                                  &pinfo_item);
32:                                  &pinfo_item);
</pre>
 
Free up the memory reserved:
Free up the memory reserved:
<pre>
38:          rc = UniFreeMem(pinfo_item);
37:          rc = UniFreeMem(pinfo_item);
</pre>
 
Now that we got the result string, we have to convert it to the current codepage.
Now that we got the result string, we have to convert it to the current codepage.
<pre>
44:    iResultSize = UniStrlen(pinfo_item) + 1;
43:    iResultSize = UniStrlen(pinfo_item) + 1;
45:    pszResult = malloc(iResultSize);
44:    pszResult = malloc(iResultSize);
46:
45:
47:    rc = UniStrFromUcs(uconv_object, pszResult , pinfo_item, iResultSize);
46:    rc = UniStrFromUcs(uconv_object, pszResult , pinfo_item, iResultSize);
</pre>
 
Print out the returned name:
Print out the returned name:
<pre>
49:    printf("sLanguageID = %s",pszResult);
48:    printf("sLanguageID = %s",pszResult);
</pre>
 
Free up memory:
Free up memory:
<pre>
51:    rc = UniFreeUconvObject(uconv_object);
50:    rc = UniFreeUconvObject(uconv_object);
</pre>


== How to compile ==
== How to compile ==
with Visual Age C++ v3.08:
with Visual Age C++ v3.08:
icc localetest.c libuls.lib
with GCC 3.5.5 RC1:
gcc localetest.c


<pre>icc localetest.c libuls.lib</pre>
[[Category:Localization Articles]]

Latest revision as of 23:42, 23 September 2022

Obtaining localized information from the system

If you want to know how the date is displayed on the current system or how the names of the month are spelled in the currently selected language or what the currently selected language is at all at this time there is not well documented library that comes with ACP/MCP & eCS.

LIBULS

This article is intended to give you a short introduction on the usage of that library.

In LIBULS there is a whole set of call that support codepage conversion and querying locale information.

This article will give a short quick and dirty sample on how to use it.

Reasons to use the LIBULS functions instead of querying environment:

  • Normally LANG will contain something useful like: en_us or de_de from which you could parse a language. But LANG=myRealyLongStupidLocale is allowed as well, now you tell me which language this locale represents.
  • Using the LIBULS allow you to access not only language names, but a whole set of localized settings
  • Using a country number will result in supplying several translations for the same language ( e.g. German is used in 041,049,043) where using the locale will return a common language for all of them : de

localetest.c

Now lets gets hands on with a sample, later I'll describe what every line does here:

01: #include <stdio.h>
02: #include <unidef.h>
03: #include <uconv.h>
04: #include <stdlib.h>
05: 
06: int main(void)
07: {
08:    LocaleObject      locale_object = NULL;
09:    UconvObject       uconv_object = NULL;
10:    UniChar           *pinfo_item;
11:    int               rc = ULS_SUCCESS;
12:    char              *pszResult;
13:    int               iResultSize;
14: 
15: 
16:    rc = UniCreateUconvObject((UniChar *)L"", &uconv_object);
17:    if (rc != ULS_SUCCESS)
18:          {
19:            printf("UniCreateUconvObject error: return code = %u\n", rc);
20:            return 1;
21:          }
22:
23:    rc = UniCreateLocaleObject(UNI_UCS_STRING_POINTER,
24:                                    (UniChar *)L"", &locale_object);
25:    if (rc != ULS_SUCCESS)
26:         {
27:            printf("UniCreateLocaleObject error: return code = %u\n", rc);
28:            return 1;
29:         }
30:          /* Query the Abriviated name of the current language */
31:          rc = UniQueryLocaleItem(locale_object,
32:                                  LOCI_sLanguageID,
33:                                  &pinfo_item);
34:          if (rc != ULS_SUCCESS) {
35:            printf("UniQueryLocaleItem error: return code = %u\n", rc);
36:            return 1;
37:          }
38:          rc = UniFreeMem(pinfo_item);
39:          if (rc != ULS_SUCCESS) {
40:            printf("UniFreeMem error: return code = %u\n", rc);
41:            return 1;
42:          }
43: 
44:    iResultSize = UniStrlen(pinfo_item) + 1;
45:    pszResult = malloc(iResultSize);
46:
47:    rc = UniStrFromUcs(uconv_object, pszResult , pinfo_item, iResultSize);
48:
49:    printf("sLanguageID = %s",pszResult);
50:
51:    rc = UniFreeUconvObject(uconv_object);
52:
53:    return(0);
54: }

I'll leave out the obvious calls here, just the libuls Specific stuff:

Include the header which declares the functions and defines the constants for the locale Items.

02: #include <unidef.h>
03: #include <uconv.h>

Declare variables for the API's

08:    LocaleObject      locale_object = NULL;
09:    UconvObject       uconv_object = NULL;
10:    UniChar           *pinfo_item;

Since all the strings returned are UniCode we need to convert them to current process codepage before we can print them. We need a conversion object for that:

16:    rc = UniCreateUconvObject((UniChar *)L"", &uconv_object);

Now create the locale_object, note that the second parameter, (UniChar *)L"", a blank locale, will cause to use the locale specified in the LANG environment.

23:    rc = UniCreateLocaleObject(UNI_UCS_STRING_POINTER,
24:                                    (UniChar *)L"", &locale_object);

Query the locale Item want to use. Look in the advanced page of the Country setting in your configuration folder to see all the names of supported items.

31:          rc = UniQueryLocaleItem(locale_object,
32:                                  LOCI_sLanguageID,
33:                                  &pinfo_item);

Free up the memory reserved:

38:          rc = UniFreeMem(pinfo_item);

Now that we got the result string, we have to convert it to the current codepage.

44:    iResultSize = UniStrlen(pinfo_item) + 1;
45:    pszResult = malloc(iResultSize);
46:
47:    rc = UniStrFromUcs(uconv_object, pszResult , pinfo_item, iResultSize);

Print out the returned name:

49:    printf("sLanguageID = %s",pszResult);

Free up memory:

51:    rc = UniFreeUconvObject(uconv_object);

How to compile

with Visual Age C++ v3.08:

icc localetest.c libuls.lib

with GCC 3.5.5 RC1:

gcc localetest.c