@@ -21,12 +21,12 @@ - (void)addEntryToTables:(DTLocalizableStringEntry *)entry;
2121
2222@implementation DTLocalizableStringAggregator
2323{
24- NSDictionary *_validMacros;
25- NSMutableDictionary *_stringTables;
26-
27- NSOperationQueue *_processingQueue;
28- dispatch_queue_t _tableQueue;
29- dispatch_group_t _tableGroup;
24+ NSDictionary *_validMacros;
25+ NSMutableDictionary *_stringTables;
26+
27+ NSOperationQueue *_processingQueue;
28+ dispatch_queue_t _tableQueue;
29+ dispatch_group_t _tableGroup;
3030
3131 DTLocalizableStringEntryWriteCallback _entryWriteCallback;
3232}
@@ -37,38 +37,38 @@ @implementation DTLocalizableStringAggregator
3737@synthesize inputEncoding = _inputEncoding;
3838@synthesize tablesToSkip = _tablesToSkip;
3939@synthesize customMacroPrefix = _customMacroPrefix;
40- @synthesize customTableName = _customTableName ;
40+ @synthesize defaultTableName = _defaultTableName ;
4141
4242- (id )init
4343{
44- self = [super init ];
45- if (self)
46- {
47- _tableQueue = dispatch_queue_create (" DTLocalizableStringAggregator" , 0 );
48- _tableGroup = dispatch_group_create ();
49-
50- _processingQueue = [[NSOperationQueue alloc ] init ];
51- [_processingQueue setMaxConcurrentOperationCount: 10 ];
52-
53- _wantsPositionalParameters = YES ; // default
54- _inputEncoding = NSUTF8StringEncoding; // default
55- }
56- return self;
44+ self = [super init ];
45+ if (self)
46+ {
47+ _tableQueue = dispatch_queue_create (" DTLocalizableStringAggregator" , 0 );
48+ _tableGroup = dispatch_group_create ();
49+
50+ _processingQueue = [[NSOperationQueue alloc ] init ];
51+ [_processingQueue setMaxConcurrentOperationCount: 10 ];
52+
53+ _wantsPositionalParameters = YES ; // default
54+ _inputEncoding = NSUTF8StringEncoding; // default
55+ }
56+ return self;
5757}
5858
59- - (void )dealloc
59+ - (void )dealloc
6060{
6161 dispatch_release (_tableQueue);
62- dispatch_release (_tableGroup);
62+ dispatch_release (_tableGroup);
6363}
6464
65- - (void )setCustomMacroPrefix : (NSString *)customMacroPrefix
65+ - (void )setCustomMacroPrefix : (NSString *)customMacroPrefix
6666{
67- if (customMacroPrefix != _customMacroPrefix)
68- {
69- _customMacroPrefix = customMacroPrefix;
70- _validMacros = nil ;
71- }
67+ if (customMacroPrefix != _customMacroPrefix)
68+ {
69+ _customMacroPrefix = customMacroPrefix;
70+ _validMacros = nil ;
71+ }
7272}
7373
7474#define KEY @" rawKey"
@@ -77,144 +77,147 @@ - (void)setCustomMacroPrefix:(NSString *)customMacroPrefix
7777#define BUNDLE @" bundle"
7878#define TABLE @" tableName"
7979
80- - (NSDictionary *)validMacros
80+ - (NSDictionary *)validMacros
8181{
82- if (!_validMacros)
83- {
84- // we know the allowed formats for NSLocalizedString() macros, so we can hard-code them
85- // there's no need to parse this stuff when we know what format things must be
86- NSArray *prefixes = [NSArray arrayWithObjects: @" NSLocalizedString" , @" CFCopyLocalizedString" , _customMacroPrefix, nil ];
87- NSDictionary *suffixes = [NSDictionary dictionaryWithObjectsAndKeys:
88- [NSArray arrayWithObjects: KEY, COMMENT, nil ], @" " ,
89- [NSArray arrayWithObjects: KEY, TABLE, COMMENT, nil ], @" FromTable" ,
90- [NSArray arrayWithObjects: KEY, TABLE, BUNDLE, COMMENT, nil ], @" FromTableInBundle" ,
91- [NSArray arrayWithObjects: KEY, TABLE, BUNDLE, VALUE, COMMENT, nil ], @" WithDefaultValue" ,
92- nil ];
93-
94- NSMutableDictionary *validMacros = [NSMutableDictionary dictionary ];
95- for (NSString *prefix in prefixes)
96- {
97- for (NSString *suffix in suffixes)
98- {
99- NSString *macroName = [prefix stringByAppendingString: suffix];
100- NSArray *parameters = [suffixes objectForKey: suffix];
101-
102- [validMacros setObject: parameters forKey: macroName];
103- }
104- }
105-
106- _validMacros = validMacros;
107- }
108-
109- return _validMacros;
82+ if (!_validMacros)
83+ {
84+ // we know the allowed formats for NSLocalizedString() macros, so we can hard-code them
85+ // there's no need to parse this stuff when we know what format things must be
86+ NSArray *prefixes = [NSArray arrayWithObjects: @" NSLocalizedString" , @" CFCopyLocalizedString" , _customMacroPrefix, nil ];
87+ NSDictionary *suffixes = [NSDictionary dictionaryWithObjectsAndKeys:
88+ [NSArray arrayWithObjects: KEY, COMMENT, nil ], @" " ,
89+ [NSArray arrayWithObjects: KEY, TABLE, COMMENT, nil ], @" FromTable" ,
90+ [NSArray arrayWithObjects: KEY, TABLE, BUNDLE, COMMENT, nil ], @" FromTableInBundle" ,
91+ [NSArray arrayWithObjects: KEY, TABLE, BUNDLE, VALUE, COMMENT, nil ], @" WithDefaultValue" ,
92+ nil ];
93+
94+ NSMutableDictionary *validMacros = [NSMutableDictionary dictionary ];
95+ for (NSString *prefix in prefixes)
96+ {
97+ for (NSString *suffix in suffixes)
98+ {
99+ NSString *macroName = [prefix stringByAppendingString: suffix];
100+ NSArray *parameters = [suffixes objectForKey: suffix];
101+
102+ [validMacros setObject: parameters forKey: macroName];
103+ }
104+ }
105+
106+ _validMacros = validMacros;
107+ }
108+
109+ return _validMacros;
110110}
111111
112112#define QUOTE @" \" "
113113
114114- (void )beginProcessingFile:(NSURL *)fileURL
115115{
116- NSDictionary *validMacros = [self validMacros ];
117-
118- DTLocalizableStringScanner *scanner = [[DTLocalizableStringScanner alloc ] initWithContentsOfURL: fileURL encoding: _inputEncoding validMacros: validMacros];
119-
120- [scanner setEntryFoundCallback: ^(DTLocalizableStringEntry *entry)
116+ NSDictionary *validMacros = [self validMacros ];
117+
118+ DTLocalizableStringScanner *scanner = [[DTLocalizableStringScanner alloc ] initWithContentsOfURL: fileURL encoding: _inputEncoding validMacros: validMacros];
119+
120+ [scanner setEntryFoundCallback: ^(DTLocalizableStringEntry *entry)
121121 {
122- NSString *key = [entry rawKey ];
123- NSString *value = [entry rawValue ];
124- BOOL shouldBeAdded = ([key hasPrefix: QUOTE] && [key hasSuffix: QUOTE]);
125-
126- if (value)
127- {
128- shouldBeAdded &= ([value hasPrefix: QUOTE] && [value hasSuffix: QUOTE]);
129- }
130-
131- if (shouldBeAdded)
132- {
133- dispatch_group_async (_tableGroup, _tableQueue, ^{
134- [self addEntryToTables: entry];
135- });
136- }
137- else
138- {
139- NSLog (@" skipping: %@ " , entry);
140- }
122+ NSString *key = [entry rawKey ];
123+ NSString *value = [entry rawValue ];
124+ BOOL shouldBeAdded = ([key hasPrefix: QUOTE] && [key hasSuffix: QUOTE]);
125+
126+ if (value)
127+ {
128+ shouldBeAdded &= ([value hasPrefix: QUOTE] && [value hasSuffix: QUOTE]);
129+ }
130+
131+ if (shouldBeAdded)
132+ {
133+ dispatch_group_async (_tableGroup, _tableQueue, ^{
134+ [self addEntryToTables: entry];
135+ });
136+ }
137+ else
138+ {
139+ NSLog (@" skipping: %@ " , entry);
140+ }
141141 }];
142-
143- [_processingQueue addOperation: scanner];
142+
143+ [_processingQueue addOperation: scanner];
144144}
145145
146146- (void )addEntryToTables : (DTLocalizableStringEntry *)entry
147147{
148- NSAssert (dispatch_get_current_queue() == _tableQueue, @"method called from invalid queue");
149- if (!_stringTables)
150- {
151- _stringTables = [NSMutableDictionary dictionary ];
152- }
153-
154- if ([entry.tableName length ] == 0 )
155- entry.tableName = _customTableName ?: @" Localizable" ;
156-
157- NSString *tableName = [entry tableName ];
148+ NSAssert (dispatch_get_current_queue() == _tableQueue, @"method called from invalid queue");
149+ if (!_stringTables)
150+ {
151+ _stringTables = [NSMutableDictionary dictionary ];
152+ }
158153
159- BOOL shouldSkip = [_tablesToSkip containsObject: tableName];
160-
161- if (!shouldSkip)
162- {
163- // find the string table for this token, or create it
164- DTLocalizableStringTable *table = [_stringTables objectForKey: tableName];
165- if (!table)
166- {
167- // need to create it
154+ // use default table name is no name set
155+ if (![entry.tableName length ])
156+ {
157+ entry.tableName = _defaultTableName ? _defaultTableName : @" Localizable" ;
158+ }
159+
160+ NSString *tableName = [entry tableName ];
161+
162+ BOOL shouldSkip = [_tablesToSkip containsObject: tableName];
163+
164+ if (!shouldSkip)
165+ {
166+ // find the string table for this token, or create it
167+ DTLocalizableStringTable *table = [_stringTables objectForKey: tableName];
168+ if (!table)
169+ {
170+ // need to create it
168171 table = [[DTLocalizableStringTable alloc ] initWithName: tableName];
169- [_stringTables setObject: table forKey: tableName];
170- }
171-
172- if (entry.rawValue )
173- {
172+ [_stringTables setObject: table forKey: tableName];
173+ }
174+
175+ if (entry.rawValue )
176+ {
174177 // ...WithDefaultValue
175- if (_wantsPositionalParameters)
176- {
178+ if (_wantsPositionalParameters)
179+ {
177180 entry.rawValue = [entry.rawValue stringByNumberingFormatPlaceholders ];
178181 }
179182
180183 [table addEntry: entry];
181184 }
182- else
183- {
185+ else
186+ {
184187 // all other options use the key and variations thereof
185188
186189 // support for predicate token splitting
187190 NSArray *keyVariants = [entry.rawKey variantsFromPredicateVariations ];
188191
189192 // add all variants
190- for (NSString *oneVariant in keyVariants)
191- {
193+ for (NSString *oneVariant in keyVariants)
194+ {
192195 DTLocalizableStringEntry *splitEntry = [entry copy ];
193196
194197 NSString *value = oneVariant;
195- if (_wantsPositionalParameters)
196- {
198+ if (_wantsPositionalParameters)
199+ {
197200 value = [oneVariant stringByNumberingFormatPlaceholders ];
198201 }
199-
202+
200203 // adjust key and value of the new entry
201204 splitEntry.rawKey = oneVariant;
202205 splitEntry.rawValue = value;
203-
206+
204207 // add token to this table
205208 [table addEntry: splitEntry];
206209 }
207210 }
208- }
211+ }
209212}
210213
211- - (NSArray *)aggregatedStringTables
214+ - (NSArray *)aggregatedStringTables
212215{
213- // wait for both of these things to finish
214- [_processingQueue waitUntilAllOperationsAreFinished ];
215- dispatch_group_wait (_tableGroup, DISPATCH_TIME_FOREVER);
216-
217- return [_stringTables allValues ];
216+ // wait for both of these things to finish
217+ [_processingQueue waitUntilAllOperationsAreFinished ];
218+ dispatch_group_wait (_tableGroup, DISPATCH_TIME_FOREVER);
219+
220+ return [_stringTables allValues ];
218221}
219222
220223@end
0 commit comments