Locale Support: Difference between revisions
mNo edit summary |
|||
(3 intermediate revisions by 2 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. | |||
==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. | |||
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 | 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 | * 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 | * 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 | |||
<pre> | <pre> | ||
01: #include <stdio.h> | 01: #include <stdio.h> | ||
Line 78: | 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. | ||
02: #include <unidef.h> | |||
02: #include <unidef.h> | 03: #include <uconv.h> | ||
03: #include <uconv.h> | |||
Declare variables for the API's | Declare variables for the API's | ||
08: LocaleObject locale_object = NULL; | |||
08: LocaleObject locale_object = NULL; | 09: UconvObject uconv_object = NULL; | ||
09: UconvObject uconv_object = NULL; | 10: UniChar *pinfo_item; | ||
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: | 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); | |||
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. | 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, | |||
23: rc = UniCreateLocaleObject(UNI_UCS_STRING_POINTER, | 24: (UniChar *)L"", &locale_object); | ||
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. | 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, | |||
31: rc = UniQueryLocaleItem(locale_object, | 32: LOCI_sLanguageID, | ||
32: LOCI_sLanguageID, | 33: &pinfo_item); | ||
33: &pinfo_item); | |||
Free up the memory reserved: | Free up the memory reserved: | ||
38: rc = UniFreeMem(pinfo_item); | |||
38: rc = UniFreeMem(pinfo_item); | |||
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. | ||
44: iResultSize = UniStrlen(pinfo_item) + 1; | |||
44: iResultSize = UniStrlen(pinfo_item) + 1; | 45: pszResult = malloc(iResultSize); | ||
45: pszResult = malloc(iResultSize); | 46: | ||
46: | 47: rc = UniStrFromUcs(uconv_object, pszResult , pinfo_item, iResultSize); | ||
47: rc = UniStrFromUcs(uconv_object, pszResult , pinfo_item, iResultSize); | |||
Print out the returned name: | Print out the returned name: | ||
49: printf("sLanguageID = %s",pszResult); | |||
49: printf("sLanguageID = %s",pszResult); | |||
Free up memory: | Free up memory: | ||
51: rc = UniFreeUconvObject(uconv_object); | |||
51: rc = UniFreeUconvObject(uconv_object); | |||
== 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: | with GCC 3.5.5 RC1: | ||
gcc localetest.c | |||
[[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