#include "unicode/utypes.h"
#include "unicode/ustring.h"
#include "unicode/uloc.h"
#include "ustr_imp.h"
#include "uresimp.h"
#include "umutex.h"
#include "cstring.h"
#include "cmemory.h"
#include "ucln_cmn.h"
U_CFUNC void locale_set_default(const char *id);
U_CFUNC const char *locale_get_default(void);
static const char _kLocaleID[] = "LocaleID";
static const char _kLanguages[] = "Languages";
static const char _kCountries[] = "Countries";
static const char _kIndexLocaleName[] = "res_index";
static const char _kIndexTag[] = "InstalledLocales";
#if 0
static const char* _kLocaleString = "LocaleString";
static const char* _kShortLanguage = "ShortLanguage";
static const char* _kShortCountry = "ShortCountry";
#endif
#define TEMPBUFSIZE 8
static char** _installedLocales = NULL;
static int32_t _installedLocalesCount = 0;
static const char * const _languages[] = {
"aa", "ab", "ace", "ach", "ada", "ae", "af", "afa",
"afh", "ak", "akk", "ale", "alg", "am", "ang", "apa",
"ar", "arc", "arn", "arp", "art", "arw", "as", "ast",
"ath", "aus", "av", "awa", "ay", "az", "ba", "bad",
"bai", "bal", "bam", "ban", "bas", "bat", "be", "bej",
"bem", "ber", "bg", "bh", "bho", "bi", "bik", "bin",
"bla", "bm", "bn", "bnt", "bo", "br", "bra", "bs",
"btk", "bua", "bug", "ca", "cad", "cai", "car", "cau",
"ce", "ceb", "cel", "ch", "chb", "chg", "chk", "chm",
"chn", "cho", "chp", "chr", "chy", "cmc", "co", "cop",
"cpe", "cpf", "cpp", "cre", "crp", "cs", "cu", "cus",
"cv", "cy", "da", "dak", "day", "de", "del", "den",
"dgr", "din", "doi", "dra", "dua", "dum", "dv", "dyu",
"dz", "ee", "efi", "egy", "eka", "el", "elx", "en",
"enm", "eo", "es", "et", "eu", "ewe", "ewo", "fa",
"fan", "fat", "ff", "fi", "fiu", "fj", "fo", "fon",
"fr", "frm", "fro", "fur", "fy", "ga", "gaa", "gay",
"gba", "gd", "gem", "gez", "gil", "gl", "gmh", "gn",
"goh", "gon", "gor", "got", "grb", "grc", "gu", "gv",
"gwi", "ha", "hai", "haw", "he", "hi", "hil", "him",
"hit", "hmn", "ho", "hr", "hu", "hup", "hy", "hz",
"ia", "iba", "id", "ie", "ig", "ii", "ijo", "ik",
"ilo", "inc", "ine", "inh", "ira", "iro", "is", "it",
"iu", "ja", "jpr", "jrb", "jv", "ka", "kaa", "kab",
"kac", "kam", "kar", "kaw", "kbd", "kg", "kha", "khi",
"kho", "ki", "kj", "kk", "kl", "km", "kmb", "kn",
"ko", "kok", "kos", "kpe", "kr", "kro", "kru", "ks",
"ku", "kum", "kut", "kv", "kw", "ky", "la", "lad",
"lah", "lam", "lb", "lez", "lg", "ln", "lo", "lol",
"loz", "lt", "lu", "lua", "lui", "lun", "luo", "lus",
"lv", "mad", "mag", "mai", "mak", "man", "map", "mas",
"mdr", "men", "mg", "mga", "mh", "mi", "mic", "min",
"mis", "mk", "mkh", "ml", "mn", "mnc", "mni", "mno",
"mo", "moh", "mos", "mr", "ms", "mt", "mul", "mun",
"mus", "mwr", "my", "myn", "na", "nah", "nai", "nap",
"nb", "nd", "nds", "ne", "new", "ng", "nia", "nic",
"niu", "nl", "nn", "no", "non", "nr", "nso", "nub",
"nv", "ny", "nym", "nyn", "nyo", "nzi", "oc", "oj",
"om", "or", "os", "osa", "ota", "oto", "pa", "paa",
"pag", "pal", "pam", "pap", "pau", "peo", "phi", "phn",
"pi", "pl", "pon", "pra", "pro", "ps", "pt", "qu",
"raj", "rap", "rar", "rm", "rn", "ro", "roa", "rom",
"ru", "rw", "sa", "sad", "sah", "sai", "sal", "sam",
"sas", "sat", "sc", "sco", "sd", "se", "sel", "sem",
"sg", "sga", "sgn", "shn", "si", "sid", "sio", "sit",
"sk", "sl", "sla", "sm", "sma", "smi", "smj", "smn",
"sms", "sn", "snk", "so", "sog", "son", "sq", "sr",
"srr", "ss", "ssa", "st", "su", "suk", "sus", "sux",
"sv", "sw", "syr", "ta", "tai", "te", "tem", "ter",
"tet", "tg", "th", "ti", "tig", "tiv", "tk", "tkl",
"tl", "tli", "tmh", "tn", "to", "tog", "tpi", "tr",
"ts", "tsi", "tt", "tum", "tup", "tut", "tvl", "tw",
"ty", "tyv", "ug", "uga", "uk", "umb", "und", "ur",
"uz", "vai", "ve", "vi", "vo", "vot", "wa", "wak",
"wal", "war", "was", "wen", "wo", "xh", "yao", "yap",
"yi", "yo", "ypk", "za", "zap", "zen", "zh", "znd",
"zu", "zun",
NULL,
"in", "iw", "ji", "jw", "sh",
NULL
};
static const char * const _languages3[] = {
"aar", "abk", "ace", "ach", "ada", "ave", "afr", "afa",
"afh", "aka", "akk", "ale", "alg", "amh", "ang", "apa",
"ara", "arc", "arn", "arp", "art", "arw", "asm", "ast",
"ath", "aus", "ava", "awa", "aym", "aze", "bak", "bad",
"bai", "bal", "bam", "ban", "bas", "bat", "bel", "bej",
"bem", "ber", "bul", "bih", "bho", "bis", "bik", "bin",
"bla", "bm", "ben", "bnt", "bod", "bre", "bra", "bos",
"btk", "bua", "bug", "cat", "cad", "cai", "car", "cau",
"che", "ceb", "cel", "cha", "chb", "chg", "chk", "chm",
"chn", "cho", "chp", "chr", "chy", "cmc", "cos", "cop",
"cpe", "cpf", "cpp", "cre", "crp", "ces", "chu", "cus",
"chv", "cym", "dan", "dak", "day", "deu", "del", "den",
"dgr", "din", "doi", "dra", "dua", "dum", "div", "dyu",
"dzo", "ewe", "efi", "egy", "eka", "ell", "elx", "eng",
"enm", "epo", "spa", "est", "eus", "ewe", "ewo", "fas",
"fan", "fat", "ful", "fin", "fiu", "fij", "fao", "fon",
"fra", "frm", "fro", "fur", "fry", "gle", "gaa", "gay",
"gba", "gla", "gem", "gez", "gil", "glg", "gmh", "grn",
"goh", "gon", "gor", "got", "grb", "grc", "guj", "glv",
"gwi", "hau", "hai", "haw", "heb", "hin", "hil", "him",
"hit", "hmn", "hmo", "hrv", "hun", "hup", "hye", "her",
"ina", "iba", "ind", "ile", "ibo", "iii", "ijo", "ipk",
"ilo", "inc", "ine", "inh", "ira", "iro", "isl", "ita",
"iku", "jpn", "jpr", "jrb", "jaw", "kat", "kaa", "kab",
"kac", "kam", "kar", "kaw", "kbd", "kon", "kha", "khi",
"kho", "kik", "kua", "kaz", "kal", "khm", "kmb", "kan",
"kor", "kok", "kos", "kpe", "kau", "kro", "kru", "kas",
"kur", "kum", "kut", "kom", "cor", "kir", "lat", "lad",
"lah", "lam", "ltz", "lez", "lug", "lin", "lao", "lol",
"loz", "lit", "lub", "lua", "lui", "lun", "luo", "lus",
"lav", "mad", "mag", "mai", "mak", "man", "map", "mas",
"mdr", "men", "mlg", "mga", "mah", "mri", "mic", "min",
"mis", "mkd", "mkh", "mal", "mon", "mnc", "mni", "mno",
"mol", "moh", "mos", "mar", "msa", "mlt", "mul", "mun",
"mus", "mwr", "mya", "myn", "nau", "nah", "nai", "nap",
"nob", "nde", "nds", "nep", "new", "ndo", "nia", "nic",
"niu", "nld", "nno", "nor", "non", "nbl", "nso", "nub",
"nav", "nya", "nym", "nyn", "nyo", "nzi", "oci", "oji",
"orm", "ori", "oss", "osa", "ota", "oto", "pan", "paa",
"pag", "pal", "pam", "pap", "pau", "peo", "phi", "phn",
"pli", "pol", "pon", "pra", "pro", "pus", "por", "que",
"raj", "rap", "rar", "roh", "run", "ron", "roa", "rom",
"rus", "kin", "san", "sad", "sah", "sai", "sal", "sam",
"sas", "sat", "srd", "sco", "snd", "sme", "sel", "sem",
"sag", "sga", "sgn", "shn", "sin", "sid", "sio", "sit",
"slk", "slv", "sla", "smo", "sma", "smi", "smj", "smn",
"sms", "sna", "snk", "som", "sog", "son", "sqi", "srp",
"srr", "ssw", "ssa", "sot", "sun", "suk", "sus", "sux",
"swe", "swa", "syr", "tam", "tai", "tel", "tem", "ter",
"tet", "tgk", "tha", "tir", "tig", "tiv", "tuk", "tkl",
"tgl", "tli", "tmh", "tsn", "ton", "tog", "tpi", "tur",
"tso", "tsi", "tat", "tum", "tup", "tut", "tvl", "twi",
"tah", "tyv", "uig", "uga", "ukr", "umb", "und", "urd",
"uzb", "vai", "ven", "vie", "vol", "vot", "wln", "wak",
"wal", "war", "was", "wen", "wol", "xho", "yao", "yap",
"yid", "yor", "ypk", "zha", "zap", "zen", "zho", "znd",
"zul", "zun",
NULL,
"ind", "heb", "yid", "jaw", "srp",
NULL
};
static const char * const _countries[] = {
"AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN",
"AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AZ",
"BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI",
"BJ", "BM", "BN", "BO", "BR", "BS", "BT", "BV",
"BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG",
"CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR",
"CU", "CV", "CX", "CY", "CZ", "DE", "DJ", "DK",
"DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER",
"ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR",
"GA", "GB", "GD", "GE", "GF", "GH", "GI", "GL",
"GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU",
"GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU",
"ID", "IE", "IL", "IN", "IO", "IQ", "IR", "IS",
"IT", "JM", "JO", "JP", "KE", "KG", "KH", "KI",
"KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA",
"LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU",
"LV", "LY", "MA", "MC", "MD", "MG", "MH", "MK",
"ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS",
"MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA",
"NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP",
"NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG",
"PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT",
"PW", "PY", "QA", "RE", "RO", "RU", "RW", "SA",
"SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ",
"SK", "SL", "SM", "SN", "SO", "SR", "ST", "SV",
"SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ",
"TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV",
"TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ",
"VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF",
"WS", "YE", "YT", "YU", "ZA", "ZM", "ZW",
NULL,
"FX", "RO", "TP", "ZR",
NULL
};
static const char * const _countries3[] = {
"AND", "ARE", "AFG", "ATG", "AIA", "ALB", "ARM", "ANT",
"AGO", "ATA", "ARG", "ASM", "AUT", "AUS", "ABW", "AZE",
"BIH", "BRB", "BGD", "BEL", "BFA", "BGR", "BHR", "BDI",
"BEN", "BMU", "BRN", "BOL", "BRA", "BHS", "BTN", "BVT",
"BWA", "BLR", "BLZ", "CAN", "CCK", "COD", "CAF", "COG",
"CHE", "CIV", "COK", "CHL", "CMR", "CHN", "COL", "CRI",
"CUB", "CPV", "CXR", "CYP", "CZE", "DEU", "DJI", "DNK",
"DMA", "DOM", "DZA", "ECU", "EST", "EGY", "ESH", "ERI",
"ESP", "ETH", "FIN", "FJI", "FLK", "FSM", "FRO", "FRA",
"GAB", "GBR", "GRD", "GEO", "GUF", "GHA", "GIB", "GRL",
"GMB", "GIN", "GLP", "GNQ", "GRC", "SGS", "GTM", "GUM",
"GNB", "GUY", "HKG", "HMD", "HND", "HRV", "HTI", "HUN",
"IDN", "IRL", "ISR", "IND", "IOT", "IRQ", "IRN", "ISL",
"ITA", "JAM", "JOR", "JPN", "KEN", "KGZ", "KHM", "KIR",
"COM", "KNA", "PRK", "KOR", "KWT", "CYM", "KAZ", "LAO",
"LBN", "LCA", "LIE", "LKA", "LBR", "LSO", "LTU", "LUX",
"LVA", "LBY", "MAR", "MCO", "MDA", "MDG", "MHL", "MKD",
"MLI", "MMR", "MNG", "MAC", "MNP", "MTQ", "MRT", "MSR",
"MLT", "MUS", "MDV", "MWI", "MEX", "MYS", "MOZ", "NAM",
"NCL", "NER", "NFK", "NGA", "NIC", "NLD", "NOR", "NPL",
"NRU", "NIU", "NZL", "OMN", "PAN", "PER", "PYF", "PNG",
"PHL", "PAK", "POL", "SPM", "PCN", "PRI", "PSE", "PRT",
"PLW", "PRY", "QAT", "REU", "ROU", "RUS", "RWA", "SAU",
"SLB", "SYC", "SDN", "SWE", "SGP", "SHN", "SVN", "SJM",
"SVK", "SLE", "SMR", "SEN", "SOM", "SUR", "STP", "SLV",
"SYR", "SWZ", "TCA", "TCD", "ATF", "TGO", "THA", "TJK",
"TKL", "TLS", "TKM", "TUN", "TON", "TUR", "TTO", "TUV",
"TWN", "TZA", "UKR", "UGA", "UMI", "USA", "URY", "UZB",
"VAT", "VCT", "VEN", "VGB", "VIR", "VNM", "VUT", "WLF",
"WSM", "YEM", "MYT", "YUG", "ZAF", "ZMB", "ZWE",
NULL,
"FXX", "ROM", "TMP", "ZAR",
NULL
};
static void _load_installedLocales(void);
#define _isIDSeparator(a) (a == '_' || a == '-')
#define _isPrefixLetter(a) ((a=='x')||(a=='X')||(a=='i')||(a=='I'))
#define _isIDPrefix(s) (_isPrefixLetter(s[0])&&_isIDSeparator(s[1]))
#define _isTerminator(a) ((a==0)||(a=='.')||(a=='@'))
static int16_t _findIndex(const char* const* list, const char* key)
{
const char* const* anchor = list;
while (*list)
{
if (uprv_strcmp(key, *list) == 0)
{
return (int16_t)(list - anchor);
}
list++;
}
list++;
while (*list)
{
if (uprv_strcmp(key, *list) == 0)
{
return (int16_t)(list - anchor);
}
list++;
}
return -1;
}
static U_INLINE int32_t
_copyCount(char *dest, int32_t destCapacity, const char *src) {
const char *anchor;
char c;
anchor=src;
for(;;) {
if((c=*src)==0) {
return (int32_t)(src-anchor);
}
if(destCapacity<=0) {
return (int32_t)((src-anchor)+uprv_strlen(src));
}
++src;
*dest++=c;
--destCapacity;
}
}
U_CAPI const char* U_EXPORT2
uloc_getDefault()
{
return locale_get_default();
}
U_CAPI void U_EXPORT2
uloc_setDefault(const char* newDefaultLocale,
UErrorCode* err)
{
if (U_FAILURE(*err))
return;
locale_set_default(newDefaultLocale);
}
U_CAPI int32_t U_EXPORT2
uloc_getParent(const char* localeID,
char* parent,
int32_t parentCapacity,
UErrorCode* err)
{
const char *lastUnderscore;
int32_t i;
if (U_FAILURE(*err))
return 0;
if (localeID == NULL)
localeID = uloc_getDefault();
lastUnderscore=uprv_strrchr(localeID, '_');
if(lastUnderscore!=NULL) {
i=(int32_t)(lastUnderscore-localeID);
} else {
i=0;
}
if(i>0) {
uprv_memcpy(parent, localeID, uprv_min(i, parentCapacity));
}
return u_terminateChars(parent, parentCapacity, i, err);
}
static int32_t
_getLanguage(const char *localeID,
char *language, int32_t languageCapacity,
const char **pEnd) {
int32_t i=0;
int32_t offset;
char lang[4]={ 0, 0, 0, 0 };
if(_isIDPrefix(localeID)) {
if(i<languageCapacity) {
language[i]=(char)uprv_tolower(*localeID);
}
if(i<languageCapacity) {
language[i+1]='-';
}
i+=2;
localeID+=2;
}
while(!_isTerminator(*localeID) && !_isIDSeparator(*localeID)) {
if(i<languageCapacity) {
language[i]=(char)uprv_tolower(*localeID);
}
if(i<3) {
lang[i]=(char)uprv_tolower(*localeID);
}
i++;
localeID++;
}
if(i==3) {
offset=_findIndex(_languages3, lang);
if(offset>=0) {
i=_copyCount(language, languageCapacity, _languages[offset]);
}
}
if(pEnd!=NULL) {
*pEnd=localeID;
}
return i;
}
U_CAPI int32_t U_EXPORT2
uloc_getLanguage(const char* localeID,
char* language,
int32_t languageCapacity,
UErrorCode* err)
{
int32_t i=0;
if (err==NULL || U_FAILURE(*err)) {
return 0;
}
if(localeID==NULL) {
localeID=uloc_getDefault();
}
i=_getLanguage(localeID, language, languageCapacity, NULL);
return u_terminateChars(language, languageCapacity, i, err);
}
static int32_t
_getCountry(const char *localeID,
char *country, int32_t countryCapacity,
const char **pEnd) {
int32_t i=0;
char cnty[4]={ 0, 0, 0, 0 };
int32_t offset;
while(!_isTerminator(*localeID) && !_isIDSeparator(*localeID)) {
if(i<countryCapacity) {
country[i]=(char)uprv_toupper(*localeID);
}
if(i<3) {
cnty[i]=(char)uprv_toupper(*localeID);
}
i++;
localeID++;
}
if(i==3) {
offset=_findIndex(_countries3, cnty);
if(offset>=0) {
i=_copyCount(country, countryCapacity, _countries[offset]);
}
}
if(pEnd!=NULL) {
*pEnd=localeID;
}
return i;
}
U_CAPI int32_t U_EXPORT2
uloc_getCountry(const char* localeID,
char* country,
int32_t countryCapacity,
UErrorCode* err)
{
int32_t i=0;
if(err==NULL || U_FAILURE(*err)) {
return 0;
}
if(localeID==NULL) {
localeID=uloc_getDefault();
}
_getLanguage(localeID, NULL, 0, &localeID);
if(_isIDSeparator(*localeID)) {
i=_getCountry(localeID+1, country, countryCapacity, NULL);
}
return u_terminateChars(country, countryCapacity, i, err);
}
static int32_t
_getVariant(const char *localeID,
char prev,
char *variant, int32_t variantCapacity) {
int32_t i=0;
if(_isIDSeparator(prev)) {
while(!_isTerminator(*localeID)) {
if(i<variantCapacity) {
variant[i]=(char)uprv_toupper(*localeID);
if(variant[i]=='-') {
variant[i]='_';
}
}
i++;
localeID++;
}
}
if(i==0) {
if(prev=='@') {
} else if((localeID=uprv_strrchr(localeID, '@'))!=NULL) {
++localeID;
} else {
return 0;
}
while(!_isTerminator(*localeID)) {
if(i<variantCapacity) {
variant[i]=(char)uprv_toupper(*localeID);
if(variant[i]=='-' || variant[i]==',') {
variant[i]='_';
}
}
i++;
localeID++;
}
}
return i;
}
U_CAPI int32_t U_EXPORT2
uloc_getVariant(const char* localeID,
char* variant,
int32_t variantCapacity,
UErrorCode* err)
{
int32_t i=0;
UBool haveVariant=FALSE;
if(err==NULL || U_FAILURE(*err)) {
return 0;
}
if(localeID==NULL) {
localeID=uloc_getDefault();
}
_getLanguage(localeID, NULL, 0, &localeID);
if(_isIDSeparator(*localeID)) {
_getCountry(localeID+1, NULL, 0, &localeID);
if(_isIDSeparator(*localeID)) {
haveVariant=TRUE;
i=_getVariant(localeID+1, *localeID, variant, variantCapacity);
}
}
if(!haveVariant && (localeID=uprv_strrchr(localeID, '@'))!=NULL) {
i=_getVariant(localeID+1, '@', variant, variantCapacity);
}
return u_terminateChars(variant, variantCapacity, i, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getName(const char* localeID,
char* name,
int32_t nameCapacity,
UErrorCode* err)
{
int32_t i, fieldCount;
if(err==NULL || U_FAILURE(*err)) {
return 0;
}
if(localeID==NULL) {
localeID=uloc_getDefault();
}
fieldCount=0;
i=_getLanguage(localeID, name, nameCapacity, &localeID);
if(_isIDSeparator(*localeID)) {
++fieldCount;
if(i<nameCapacity) {
name[i]='_';
}
++i;
i+=_getCountry(localeID+1, name+i, nameCapacity-i, &localeID);
if(_isIDSeparator(*localeID)) {
++fieldCount;
if(i<nameCapacity) {
name[i]='_';
}
++i;
i+=_getVariant(localeID+1, *localeID, name+i, nameCapacity-i);
}
}
if(fieldCount<2 && (localeID=uprv_strrchr(localeID, '@'))!=NULL) {
do {
if(i<nameCapacity) {
name[i]='_';
}
++i;
++fieldCount;
} while(fieldCount<2);
i+=_getVariant(localeID+1, '@', name+i, nameCapacity-i);
}
return u_terminateChars(name, nameCapacity, i, err);
}
U_CAPI const char* U_EXPORT2
uloc_getISO3Language(const char* localeID)
{
int16_t offset;
char lang[TEMPBUFSIZE];
UErrorCode err = U_ZERO_ERROR;
if (localeID == NULL)
{
localeID = uloc_getDefault();
}
uloc_getLanguage(localeID, lang, TEMPBUFSIZE, &err);
if (U_FAILURE(err))
return "";
offset = _findIndex(_languages, lang);
if (offset < 0)
return "";
return _languages3[offset];
}
U_CAPI const char* U_EXPORT2
uloc_getISO3Country(const char* localeID)
{
int16_t offset;
char cntry[TEMPBUFSIZE];
UErrorCode err = U_ZERO_ERROR;
if (localeID == NULL)
{
localeID = uloc_getDefault();
}
uloc_getCountry(localeID, cntry, TEMPBUFSIZE, &err);
if (U_FAILURE(err))
return "";
offset = _findIndex(_countries, cntry);
if (offset < 0)
return "";
return _countries3[offset];
}
U_CAPI uint32_t U_EXPORT2
uloc_getLCID(const char* localeID)
{
UErrorCode err = U_ZERO_ERROR;
uint32_t result = 0;
UResourceBundle* bundle = ures_open(NULL, localeID, &err);
if (U_SUCCESS(err))
{
UResourceBundle *resLocaleID = ures_getByKey(bundle, _kLocaleID, NULL, &err);
if (U_SUCCESS(err))
{
result = ures_getInt(resLocaleID, &err);
if (U_FAILURE(err))
{
result = 0;
}
ures_close(resLocaleID);
}
ures_close(bundle);
}
return result;
}
static const UChar *
_res_getTableStringWithFallback(const char *path, const char *locale,
const char *tableKey, const char *itemKey,
int32_t *pLength,
UErrorCode *pErrorCode)
{
char localeBuffer[80];
UResourceBundle *rb, table;
const UChar *item;
UErrorCode errorCode;
for(;;) {
errorCode=U_ZERO_ERROR;
rb=ures_open(path, locale, &errorCode);
if(U_FAILURE(errorCode)) {
*pErrorCode=errorCode;
return NULL;
} else if(errorCode==U_USING_DEFAULT_WARNING ||
(errorCode==U_USING_FALLBACK_WARNING && *pErrorCode!=U_USING_DEFAULT_WARNING)
) {
*pErrorCode=errorCode;
}
errorCode=U_ZERO_ERROR;
ures_initStackObject(&table);
ures_getByKey(rb, tableKey, &table, &errorCode);
if(U_FAILURE(errorCode)) {
ures_close(rb);
*pErrorCode=errorCode;
return NULL;
} else if(errorCode==U_USING_DEFAULT_WARNING ||
(errorCode==U_USING_FALLBACK_WARNING && *pErrorCode!=U_USING_DEFAULT_WARNING)
) {
*pErrorCode=errorCode;
}
errorCode=U_ZERO_ERROR;
item=ures_getStringByKey(&table, itemKey, pLength, &errorCode);
if(U_SUCCESS(errorCode)) {
ures_close(&table);
ures_close(rb);
if(errorCode==U_USING_DEFAULT_WARNING ||
(errorCode==U_USING_FALLBACK_WARNING && *pErrorCode!=U_USING_DEFAULT_WARNING)
) {
*pErrorCode=errorCode;
}
return item;
}
errorCode=U_ZERO_ERROR;
locale=ures_getLocale(&table, &errorCode);
if(U_FAILURE(errorCode)) {
ures_close(&table);
ures_close(rb);
*pErrorCode=U_INTERNAL_PROGRAM_ERROR;
return NULL;
}
if(*locale==0 || 0==uprv_strcmp(locale, "root")) {
ures_close(&table);
ures_close(rb);
*pErrorCode=U_MISSING_RESOURCE_ERROR;
return NULL;
}
errorCode=U_ZERO_ERROR;
uloc_getParent(locale, localeBuffer, sizeof(localeBuffer), &errorCode);
ures_close(&table);
ures_close(rb);
if(U_FAILURE(errorCode) || errorCode==U_STRING_NOT_TERMINATED_WARNING) {
*pErrorCode=U_INTERNAL_PROGRAM_ERROR;
return NULL;
}
locale=localeBuffer;
}
}
static int32_t
_getStringOrCopyKey(const char *path, const char *locale,
const char *tableKey, const char *itemKey,
const char *substitute,
UChar *dest, int32_t destCapacity,
UErrorCode *pErrorCode) {
const UChar *s;
int32_t length;
if(itemKey==NULL) {
UResourceBundle *rb;
rb=ures_open(path, locale, pErrorCode);
if(U_SUCCESS(*pErrorCode)) {
s=ures_getStringByKey(rb, tableKey, &length, pErrorCode);
ures_close(rb);
}
} else {
s=_res_getTableStringWithFallback(path, locale,
tableKey, itemKey,
&length,
pErrorCode);
}
if(U_SUCCESS(*pErrorCode)) {
int32_t copyLength=uprv_min(length, destCapacity);
if(copyLength>0) {
u_memcpy(dest, s, copyLength);
}
} else {
length=(int32_t)uprv_strlen(substitute);
u_charsToUChars(substitute, dest, uprv_min(length, destCapacity));
*pErrorCode=U_USING_DEFAULT_WARNING;
}
return u_terminateUChars(dest, destCapacity, length, pErrorCode);
}
U_CAPI int32_t U_EXPORT2
uloc_getDisplayLanguage(const char *locale,
const char *displayLocale,
UChar *dest, int32_t destCapacity,
UErrorCode *pErrorCode) {
char localeBuffer[200];
int32_t length;
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return 0;
}
if(destCapacity<0 || (destCapacity>0 && dest==NULL)) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
*pErrorCode=U_ZERO_ERROR;
length=uloc_getLanguage(locale, localeBuffer, sizeof(localeBuffer), pErrorCode);
if(U_FAILURE(*pErrorCode) || *pErrorCode==U_STRING_NOT_TERMINATED_WARNING) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if(length==0) {
return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
}
return _getStringOrCopyKey(NULL, displayLocale,
_kLanguages, localeBuffer,
localeBuffer,
dest, destCapacity,
pErrorCode);
}
U_CAPI int32_t U_EXPORT2
uloc_getDisplayCountry(const char *locale,
const char *displayLocale,
UChar *dest, int32_t destCapacity,
UErrorCode *pErrorCode) {
char localeBuffer[200];
int32_t length;
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return 0;
}
if(destCapacity<0 || (destCapacity>0 && dest==NULL)) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
*pErrorCode=U_ZERO_ERROR;
length=uloc_getCountry(locale, localeBuffer, sizeof(localeBuffer), pErrorCode);
if(U_FAILURE(*pErrorCode) || *pErrorCode==U_STRING_NOT_TERMINATED_WARNING) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if(length==0) {
return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
}
return _getStringOrCopyKey(NULL, displayLocale,
_kCountries, localeBuffer,
localeBuffer,
dest, destCapacity,
pErrorCode);
}
U_CAPI int32_t U_EXPORT2
uloc_getDisplayVariant(const char *locale,
const char *displayLocale,
UChar *dest, int32_t destCapacity,
UErrorCode *pErrorCode) {
char localeBuffer[200];
int32_t length;
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return 0;
}
if(destCapacity<0 || (destCapacity>0 && dest==NULL)) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
*pErrorCode=U_ZERO_ERROR;
localeBuffer[0]=localeBuffer[1]='%';
length=uloc_getVariant(locale, localeBuffer+2, sizeof(localeBuffer)-2, pErrorCode);
if(U_FAILURE(*pErrorCode) || *pErrorCode==U_STRING_NOT_TERMINATED_WARNING) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if(length==0) {
return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
}
return _getStringOrCopyKey(NULL, displayLocale,
localeBuffer, NULL,
localeBuffer+2,
dest, destCapacity,
pErrorCode);
}
U_CAPI int32_t U_EXPORT2
uloc_getDisplayName(const char *locale,
const char *displayLocale,
UChar *dest, int32_t destCapacity,
UErrorCode *pErrorCode) {
int32_t length, length2;
UBool hasLanguage, hasCountry, hasVariant;
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return 0;
}
if(destCapacity<0 || (destCapacity>0 && dest==NULL)) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
length=uloc_getDisplayLanguage(locale, displayLocale,
dest, destCapacity,
pErrorCode);
hasLanguage= length>0;
if(hasLanguage) {
if(length<destCapacity) {
dest[length]=0x20;
}
++length;
if(length<destCapacity) {
dest[length]=0x28;
}
++length;
}
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
*pErrorCode=U_ZERO_ERROR;
}
if(length<destCapacity) {
length2=uloc_getDisplayCountry(locale, displayLocale,
dest+length, destCapacity-length,
pErrorCode);
} else {
length2=uloc_getDisplayCountry(locale, displayLocale,
NULL, 0,
pErrorCode);
}
hasCountry= length2>0;
length+=length2;
if(hasCountry) {
if(length<destCapacity) {
dest[length]=0x2c;
}
++length;
if(length<destCapacity) {
dest[length]=0x20;
}
++length;
}
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
*pErrorCode=U_ZERO_ERROR;
}
if(length<destCapacity) {
length2=uloc_getDisplayVariant(locale, displayLocale,
dest+length, destCapacity-length,
pErrorCode);
} else {
length2=uloc_getDisplayVariant(locale, displayLocale,
NULL, 0,
pErrorCode);
}
hasVariant= length2>0;
length+=length2;
if(hasCountry && !hasVariant) {
length-=2;
}
if(hasLanguage) {
if(hasCountry || hasVariant) {
if(length<destCapacity) {
dest[length]=0x29;
}
++length;
} else {
length-=2;
}
}
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
*pErrorCode=U_ZERO_ERROR;
}
return u_terminateUChars(dest, destCapacity, length, pErrorCode);
}
U_CAPI const char* U_EXPORT2
uloc_getAvailable(int32_t offset)
{
_load_installedLocales();
if (offset > _installedLocalesCount)
return NULL;
else
return _installedLocales[offset];
}
U_CAPI int32_t U_EXPORT2
uloc_countAvailable()
{
_load_installedLocales();
return _installedLocalesCount;
}
UBool uloc_cleanup(void) {
char ** temp;
if (_installedLocales) {
temp = _installedLocales;
_installedLocales = NULL;
_installedLocalesCount = 0;
uprv_free(temp);
}
return TRUE;
}
static void _load_installedLocales()
{
UBool localesLoaded;
umtx_lock(NULL);
localesLoaded = _installedLocales != NULL;
umtx_unlock(NULL);
if (localesLoaded == FALSE) {
UResourceBundle *index = NULL;
UResourceBundle installed;
UErrorCode status = U_ZERO_ERROR;
char ** temp;
int32_t i = 0;
int32_t localeCount;
ures_initStackObject(&installed);
index = ures_openDirect(NULL, _kIndexLocaleName, &status);
ures_getByKey(index, _kIndexTag, &installed, &status);
if(U_SUCCESS(status)) {
localeCount = ures_getSize(&installed);
temp = (char **) uprv_malloc(sizeof(char*) * (localeCount+1));
ures_resetIterator(&installed);
while(ures_hasNext(&installed)) {
ures_getNextString(&installed, NULL, (const char **)&temp[i++], &status);
}
temp[i] = NULL;
umtx_lock(NULL);
if (_installedLocales == NULL)
{
_installedLocales = temp;
_installedLocalesCount = localeCount;
temp = NULL;
}
umtx_unlock(NULL);
uprv_free(temp);
ures_close(&installed);
}
ures_close(index);
}
}
U_CAPI const char* const* U_EXPORT2
uloc_getISOLanguages()
{
return _languages;
}
U_CAPI const char* const* U_EXPORT2
uloc_getISOCountries()
{
return _countries;
}