Skip to content

Commit 706a90a

Browse files
author
gugmaster
committed
Added parser for strings files
1 parent 412d8be commit 706a90a

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//
2+
// DTLocalizableStringsParser.h
3+
// genstrings2
4+
//
5+
// Created by Stefan Gugarel on 2/27/13.
6+
// Copyright (c) 2013 Drobnik KG. All rights reserved.
7+
//
8+
9+
10+
@class DTLocalizableStringsParser;
11+
12+
@protocol DTLocalizableStringsParserDelegate <NSObject>
13+
14+
@optional
15+
16+
/**
17+
Sent to the delegate for each comment block found
18+
*/
19+
- (void)parser:(DTLocalizableStringsParser *)parser foundComment:(NSString *)comment;
20+
21+
/**
22+
Sent to the delegate for each comment block found
23+
*/
24+
- (void)parser:(DTLocalizableStringsParser *)parser foundKey:(NSString *)key value:(NSString *)value;
25+
26+
/**
27+
Sent to the delegate once parsing has finished
28+
*/
29+
- (void)parserDidStartDocument:(DTLocalizableStringsParser *)parser;
30+
31+
/**
32+
Sent to the delegate once parsing has finished
33+
*/
34+
- (void)parserDidEndDocument:(DTLocalizableStringsParser *)parser;
35+
36+
/**
37+
Sent to the delegate if an error occurs
38+
*/
39+
- (void)parser:(DTLocalizableStringsParser *)parser parseErrorOccurred:(NSError *)parseError;
40+
41+
@end
42+
43+
/**
44+
Parser for strings files. You initialize it with a file URL, set a delegate and start parsing with parse. This returns `YES` in case of success.
45+
*/
46+
@interface DTLocalizableStringsParser : NSObject
47+
48+
/**
49+
@name Creating a Parser
50+
*/
51+
52+
/**
53+
Instantiates a strings file parser
54+
*/
55+
- (id)initWithFileURL:(NSURL *)url;
56+
57+
/**
58+
@name Parsing File Contents
59+
*/
60+
61+
/**
62+
Parses the file.
63+
*/
64+
- (BOOL)parse;
65+
66+
/**
67+
The parser delegate
68+
*/
69+
@property (nonatomic, unsafe_unretained) id <DTLocalizableStringsParserDelegate> delegate;
70+
71+
/**
72+
The last reported parse error
73+
*/
74+
@property (nonatomic, readonly) NSError *parseError;
75+
76+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// DTLocalizableStringsParser.m
3+
// genstrings2
4+
//
5+
// Created by Stefan Gugarel on 2/27/13.
6+
// Copyright (c) 2013 Drobnik KG. All rights reserved.
7+
//
8+
9+
#import "DTLocalizableStringsParser.h"
10+
11+
@implementation DTLocalizableStringsParser
12+
13+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// DTLocalizableStringsParserTest.h
3+
// genstrings2
4+
//
5+
// Created by Stefan Gugarel on 2/28/13.
6+
// Copyright (c) 2013 Drobnik KG. All rights reserved.
7+
//
8+
9+
#import <SenTestingKit/SenTestingKit.h>
10+
11+
@interface DTLocalizableStringsParserTest : SenTestCase
12+
13+
@end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// DTLocalizableStringsParserTest.m
3+
// genstrings2
4+
//
5+
// Created by Stefan Gugarel on 2/28/13.
6+
// Copyright (c) 2013 Drobnik KG. All rights reserved.
7+
//
8+
9+
#import "DTLocalizableStringsParserTest.h"
10+
#import "DTLocalizableStringsParser.h"
11+
12+
@interface DTLocalizableStringsParserTest() <DTLocalizableStringsParserDelegate>
13+
14+
@end
15+
16+
@implementation DTLocalizableStringsParserTest
17+
18+
19+
- (void)testKeyValueParsing
20+
{
21+
NSURL *localizableStringsURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"Localizable" withExtension:@"strings"];
22+
23+
DTLocalizableStringsParser *parser = [[DTLocalizableStringsParser alloc] initWithFileURL:localizableStringsURL];
24+
parser.delegate = self;
25+
26+
STAssertTrue([parser parse], @"Failed to parse: %@", parser.parseError);
27+
}
28+
29+
#pragma mark - DTLocalizableStringsParserDelegate
30+
31+
/*
32+
- (void)parser:(DTLocalizableStringsParser *)parser foundKey:(NSString *)key value:(NSString *)value
33+
{
34+
NSLog(@"%@ - %@", key, value);
35+
}
36+
37+
- (void)parser:(DTLocalizableStringsParser *)parser parseErrorOccurred:(NSError *)parseError
38+
{
39+
NSLog(@"%@", [parseError localizedDescription]);
40+
}
41+
*/
42+
43+
@end

Test/Resources/Localizable.strings

1.78 KB
Binary file not shown.

0 commit comments

Comments
 (0)