forked from groue/GRMustache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGRMustacheLocalizer.m
275 lines (227 loc) · 8.85 KB
/
GRMustacheLocalizer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// The MIT License
//
// Copyright (c) 2013 Gwendal Roué
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "GRMustache.h"
#import "GRMustacheLocalizer.h"
static NSString *const GRMustacheLocalizerValuePlaceholder = @"GRMustacheLocalizerValuePlaceholder";
@interface GRMustacheLocalizer()<GRMustacheTagDelegate>
@property (nonatomic, strong) NSMutableArray *formatArguments;
- (NSString *)localizedStringForKey:(NSString *)key;
- (NSString *)stringWithFormat:(NSString *)format argumentArray:(NSArray *)arguments;
@end
@implementation GRMustacheLocalizer
@synthesize formatArguments=_formatArguments;
@synthesize bundle=_bundle;
@synthesize tableName=_tableName;
- (void)dealloc
{
[_bundle release];
[_tableName release];
[super dealloc];
}
- (id)initWithBundle:(NSBundle *)bundle tableName:(NSString *)tableName
{
self = [super init];
if (self) {
_bundle = [(bundle ?: [NSBundle mainBundle]) retain];
_tableName = [tableName retain];
}
return self;
}
- (NSString *)localizedStringForKey:(NSString *)key
{
return [_bundle localizedStringForKey:key value:@"" table:_tableName];
}
#pragma mark - GRMustacheFilter
/**
* Support for {{ localize(value) }}
*/
- (id)transformedValue:(id)object
{
// Specific case for [NSNull null]
if (object == [NSNull null]) {
return @"";
}
// Turns other objects into strings, and localize
NSString *string = [object description];
return [self localizedStringForKey:string];
}
#pragma mark - GRMustacheRendering
/**
* Support for {{# localize }}...{{ value }}...{{ value }}...{{/ localize }}
*/
- (NSString *)renderForMustacheTag:(GRMustacheTag *)tag context:(GRMustacheContext *)context HTMLSafe:(BOOL *)HTMLSafe error:(NSError *__autoreleasing *)error
{
/**
* Perform a first rendering of the section tag, that will turn variable
* tags into a custom placeholder.
*
* "...{{name}}..." will get turned into "...GRMustacheLocalizerValuePlaceholder...".
*
* For that, we make sure we are notified of tag rendering, so that our
* mustacheTag:willRenderObject: implementation tells the tags to render
* GRMustacheLocalizerValuePlaceholder instead of the regular values,
* "Arthur" or "Barbara". This behavior is trigerred by the nil value of
* self.formatArguments.
*/
// Set up first pass behavior
self.formatArguments = nil;
// Get notified of tag rendering
context = [context contextByAddingTagDelegate:self];
// Render the localizable format
NSString *localizableFormat = [tag renderContentWithContext:context HTMLSafe:HTMLSafe error:error];
/**
* Perform a second rendering that will fill our formatArguments array with
* HTML-escaped tag renderings.
*
* Now our mustacheTag:willRenderObject: implementation will let the regular
* values go through normal rendering ("Arthur" or "Barbara"). Our
* mustacheTag:didRenderObject:as: method will fill self.formatArguments.
*
* This behavior is not the same as the previous one, and is trigerred by
* the non-nil value of self.formatArguments.
*/
// Set up second pass behavior
self.formatArguments = [NSMutableArray array];
// Fill formatArguments
[tag renderContentWithContext:context HTMLSafe:HTMLSafe error:error];
/**
* Localize the format, and render.
*/
NSString *rendering = nil;
if (self.formatArguments.count == 0)
{
// Don't format anything if there is no format argument.
rendering = [self localizedStringForKey:localizableFormat];
}
else
{
/**
* When rendering {{#localize}}%d {{name}}{{/localize}},
* The localizableFormat string we have just built is
* "%d GRMustacheLocalizerValuePlaceholder".
*
* In order to get an actual format string, we have to:
* - turn GRMustacheLocalizerValuePlaceholder into %@
* - escape % into %%.
*
* The format string will then be "%%d %@".
*/
localizableFormat = [localizableFormat stringByReplacingOccurrencesOfString:@"%" withString:@"%%"];
localizableFormat = [localizableFormat stringByReplacingOccurrencesOfString:GRMustacheLocalizerValuePlaceholder withString:@"%@"];
NSString *localizedFormat = [self localizedStringForKey:localizableFormat];
rendering = [self stringWithFormat:localizedFormat argumentArray:self.formatArguments];
}
/**
* Cleanup and return
*/
self.formatArguments = nil;
return rendering;
}
#pragma mark - GRMustacheTagDelegate
/**
* Support for {{# localize }}...{{ value }}...{{ value }}...{{/ localize }}
*/
- (id)mustacheTag:(GRMustacheTag *)tag willRenderObject:(id)object
{
/**
* We are only interested in the rendering of variable tags such as
* {{name}}. We do not want to mess with Mustache handling of boolean
* sections such as {{#true}}...{{/}}.
*/
if (tag.type != GRMustacheTagTypeVariable) {
return object;
}
/**
* We behave as stated in renderForMustacheTag:context:HTMLSafe:error:
*/
if (self.formatArguments) {
return object;
} else {
return GRMustacheLocalizerValuePlaceholder;
}
}
/**
* Support for {{# localize }}...{{ value }}...{{ value }}...{{/ localize }}
*/
- (void)mustacheTag:(GRMustacheTag *)tag didRenderObject:(id)object as:(NSString *)rendering
{
/**
* Without messing with section tags...
*/
if (tag.type == GRMustacheTagTypeVariable) {
/**
* ... we behave as stated in renderForMustacheTag:context:HTMLSafe:error:
*/
[self.formatArguments addObject:rendering];
}
}
#pragma mark - Private
- (NSString *)stringWithFormat:(NSString *)format argumentArray:(NSArray *)arguments
{
/**
* NSString formatting methods do not accept an array of format arguments.
*
* Faking va_list as in http://stackoverflow.com/questions/688070/is-there-any-way-to-pass-an-nsarray-to-a-method-that-expects-a-variable-number-o
* used to compile, but it does no longer:
*
* id fake_va_list[arguments.count];
* [arguments getObjects:fake_va_list];
* rendering = [[[NSString alloc] initWithFormat:format arguments:(va_list)fake_va_list] autorelease];
* ^ ~~~~~~~~~~~~
* error: used type 'va_list' (aka '__builtin_va_list') where arithmetic or pointer type is required
*
* Removing the (va_list) cast only generates a warning, but the code crashes when run.
*
* NSInvocation? NSInvocation does not support variadic functions.
*
* So I guess we have to do it by hand :-(
*/
NSUInteger count = arguments.count;
id args[] = {
(count > 0) ? [arguments objectAtIndex:0] : nil,
(count > 1) ? [arguments objectAtIndex:1] : nil,
(count > 2) ? [arguments objectAtIndex:2] : nil,
(count > 3) ? [arguments objectAtIndex:3] : nil,
(count > 4) ? [arguments objectAtIndex:4] : nil,
(count > 5) ? [arguments objectAtIndex:5] : nil,
(count > 6) ? [arguments objectAtIndex:6] : nil,
(count > 7) ? [arguments objectAtIndex:7] : nil,
(count > 8) ? [arguments objectAtIndex:8] : nil,
(count > 9) ? [arguments objectAtIndex:9] : nil,
};
if (count > 10) {
[NSException raise:NSGenericException format:@"Not implemented: format with %ld parameters", (unsigned long)count];
return nil;
}
return [NSString stringWithFormat:format,
args[0],
args[1],
args[2],
args[3],
args[4],
args[5],
args[6],
args[7],
args[8],
args[9]];
}
@end