@@ -25,10 +25,55 @@ #import "OFInvalidArgumentException.h" #import "OFInvalidEncodingException.h" static OFLocalization *sharedLocalization = nil; + +static void +parseLocale(char *locale, of_string_encoding_t *encoding, + OFString **language, OFString **territory) +{ + if ((locale = of_strdup(locale)) == NULL) + return; + + @try { + const of_string_encoding_t enc = OF_STRING_ENCODING_ASCII; + char *tmp; + + /* We don't care for extras behind the @ */ + if ((tmp = strrchr(locale, '@')) != NULL) + *tmp = '\0'; + + /* Encoding */ + if ((tmp = strrchr(locale, '.')) != NULL) { + *tmp++ = '\0'; + + @try { + if (encoding != NULL) + *encoding = of_string_parse_encoding( + [OFString stringWithCString: tmp + encoding: enc]); + } @catch (OFInvalidEncodingException *e) { + } + } + + /* Territory */ + if ((tmp = strrchr(locale, '_')) != NULL) { + *tmp++ = '\0'; + + if (territory != NULL) + *territory = [OFString stringWithCString: tmp + encoding: enc]; + } + + if (language != NULL) + *language = [OFString stringWithCString: locale + encoding: enc]; + } @finally { + free(locale); + } +} @implementation OFLocalization @synthesize language = _language, territory = _territory, encoding = _encoding; @synthesize decimalPoint = _decimalPoint; @@ -62,75 +107,46 @@ { [sharedLocalization addLanguageDirectory: path]; } #endif -- initWithLocale: (char*)locale +- init { self = [super init]; @try { - _localizedStrings = [[OFMutableArray alloc] init]; - } @catch (id e) { - [self release]; - @throw e; - } - - if (locale == NULL) { - _encoding = OF_STRING_ENCODING_UTF_8; - _decimalPoint = @"."; - - sharedLocalization = self; - - return self; - } - - locale = of_strdup(locale); - - @try { - char *tmp; - - /* We don't care for extras behind the @ */ - if ((tmp = strrchr(locale, '@')) != NULL) - *tmp = '\0'; - - /* Encoding */ - if ((tmp = strrchr(locale, '.')) != NULL) { - *tmp++ = '\0'; - - @try { - const of_string_encoding_t ascii = - OF_STRING_ENCODING_ASCII; - - _encoding = of_string_parse_encoding([OFString - stringWithCString: tmp - encoding: ascii]); - } @catch (OFInvalidEncodingException *e) { - } - } - - /* Territory */ - if ((tmp = strrchr(locale, '_')) != NULL) { - *tmp++ = '\0'; - - _territory = [[OFString alloc] - initWithCString: tmp - encoding: OF_STRING_ENCODING_ASCII]; - } - - _language = [[OFString alloc] - initWithCString: locale - encoding: OF_STRING_ENCODING_ASCII]; - - _decimalPoint = [[OFString alloc] - initWithCString: localeconv()->decimal_point - encoding: _encoding]; - } @catch (id e) { - [self release]; - @throw e; - } @finally { - free(locale); + char *locale, *messagesLocale = NULL; + + _encoding = OF_STRING_ENCODING_UTF_8; + _decimalPoint = @"."; + _localizedStrings = [[OFMutableArray alloc] init]; + + if ((locale = setlocale(LC_ALL, "")) != NULL) + _decimalPoint = [[OFString alloc] + initWithCString: localeconv()->decimal_point + encoding: _encoding]; + +#ifdef LC_MESSAGES + messagesLocale = setlocale(LC_MESSAGES, ""); +#endif + if (messagesLocale == NULL) + messagesLocale = locale; + + if (messagesLocale != NULL) { + void *pool = objc_autoreleasePoolPush(); + + parseLocale(messagesLocale, &_encoding, + &_language, &_language); + + [_language retain]; + [_territory retain]; + + objc_autoreleasePoolPop(pool); + } + } @catch (id e) { + [self release]; + @throw e; } sharedLocalization = self; return self;