Locale Support: Difference between revisions
mNo edit summary |
|||
Line 141: | Line 141: | ||
<pre>gcc localetest.c</pre> | <pre>gcc localetest.c</pre> | ||
[[Category:Localization Articles]] |
Revision as of 22:11, 28 October 2011
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 enivronment:
- 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.
- Using the LIBULS allow you to acces 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
localetest.c
Now lets gets hands on with a sample, later I'll descibe 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