ObjFW  Diff

Differences From Artifact [a996c854ff]:

To Artifact [306b376984]:


23
24
25
26
27
28
29













































30
31
32
33
34
35
36
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







#import "OFArray.h"
#import "OFDictionary.h"

#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;

+ (instancetype)sharedLocalization
{
60
61
62
63
64
65
66
67

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

91
92
93
94
95
96
97
98
99
100
101
102



103

104
105
106



107
108
109









110
111
112

113

114

115

116
117
118
119


120
121
122
123

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
105
106
107
108
109
110
111

112
113
114
115
116



















117
118











119
120
121
122
123



124
125
126



127
128
129
130
131
132
133
134
135
136


137

138
139
140

141




142
143




144



145
146
147


148
149
150
151
152
153
154







-
+




-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+

-
-
-
-
-
-
-
-
-
-
-
+
+
+

+
-
-
-
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+

-
-
+
-
+

+
-
+
-
-
-
-
+
+
-
-
-
-
+
-
-
-



-
-







#ifdef OF_HAVE_FILES
+ (void)addLanguageDirectory: (OFString*)path
{
	[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;
		char *locale, *messagesLocale = NULL;

		/* 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_ENCODING_UTF_8;
		_decimalPoint = @".";
		_localizedStrings = [[OFMutableArray alloc] init];

		if ((locale = setlocale(LC_ALL, "")) != NULL)
				_encoding = of_string_parse_encoding([OFString
				    stringWithCString: tmp
					     encoding: ascii]);
			_decimalPoint = [[OFString alloc]
			    initWithCString: localeconv()->decimal_point
				   encoding: _encoding];
			} @catch (OFInvalidEncodingException *e) {
			}
		}

#ifdef LC_MESSAGES
		messagesLocale = setlocale(LC_MESSAGES, "");
#endif
		if (messagesLocale == NULL)
			messagesLocale = locale;

		if (messagesLocale != NULL) {
			void *pool = objc_autoreleasePoolPush();

		/* Territory */
		if ((tmp = strrchr(locale, '_')) != NULL) {
			parseLocale(messagesLocale, &_encoding,
			*tmp++ = '\0';
			    &_language, &_language);

			[_language retain];
			_territory = [[OFString alloc]
			[_territory retain];
			    initWithCString: tmp
				   encoding: OF_STRING_ENCODING_ASCII];
		}


			objc_autoreleasePoolPop(pool);
		_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);
	}

	sharedLocalization = self;

	return self;
}