1010#import " SCReader.h"
1111#import " NSString+SCAdditions.h"
1212
13+ static NSString *kKeyFileHandle = @" kKeyFileHandle" ;
14+ static NSString *kKeyEncoding = @" kKeyEncoding" ;
15+
1316@interface SCStringsWriter ()
1417@property (nonatomic , strong ) NSMutableDictionary *fileHandlers;
1518@property (nonatomic , strong ) NSArray *headers;
16- @property (nonatomic , assign ) NSStringEncoding fileEncoding;
1719@end
1820
1921@implementation SCStringsWriter
20- @synthesize fileHandlers;
21- @synthesize headers;
22- @synthesize fileEncoding;
2322
2423- (id )initWithHeaders : (NSArray *)heads
2524{
@@ -45,12 +44,13 @@ - (id)initWithTranslationFiles:(NSDictionary *)files
4544 NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath: [x objectForKey: @" Path" ]];
4645 if (fileHandle == nil ) {SCLog (@" Unable to open file for writing at path %@ " , [x objectForKey: @" Path" ]); return nil ;}
4746
48- if (![NSString detectFileEncoding: &fileEncoding path: [x objectForKey: @" Path" ] error: nil ]) {
47+ NSStringEncoding encoding;
48+ if (![NSString detectFileEncoding: &encoding path: [x objectForKey: @" Path" ] error: nil ]) {
4949 // fallback
50- fileEncoding = NSUTF8StringEncoding;
50+ encoding = NSUTF8StringEncoding;
5151 }
52-
53- [self .fileHandlers setObject: fileHandle forKey: [x objectForKey: @" Language" ]];
52+
53+ [self .fileHandlers setObject: @{ kKeyFileHandle : fileHandle, kKeyEncoding : @(encoding)} forKey: [x objectForKey: @" Language" ]];
5454 [heads addObject: [x objectForKey: @" Language" ]];
5555 }
5656 }
@@ -70,29 +70,40 @@ - (void)writeTranslations:(NSDictionary*)translations failure:(void(^)(NSError *
7070 {
7171 NSString *comment = [NSString stringWithFormat: @" %@ \n " , [[translationDict objectForKey: @" Comment" ] stringByReplacingOccurrencesOfString: @" \\ n" withString: @" \n " ]];
7272 for (NSString *fileHandleKey in self.fileHandlers )
73- [(NSFileHandle *)[self .fileHandlers objectForKey: fileHandleKey] writeData: [comment dataUsingEncoding: self .fileEncoding]];
73+ {
74+ NSFileHandle *fileHandle = [[self .fileHandlers objectForKey: fileHandleKey] objectForKey: kKeyFileHandle ];
75+ NSStringEncoding encoding = [[[self .fileHandlers objectForKey: fileHandleKey] objectForKey: kKeyEncoding ] unsignedIntValue ];
76+
77+ [fileHandle writeData: [comment dataUsingEncoding: encoding]];
78+ }
7479 }
7580
7681 for (NSString *header in self.headers )
7782 {
7883 NSString *translation = [translationDict objectForKey: header];
7984 if (!translation.length ) translation = key;
8085
86+ NSFileHandle *fileHandle = [[self .fileHandlers objectForKey: header] objectForKey: kKeyFileHandle ];
87+ NSStringEncoding encoding = [[[self .fileHandlers objectForKey: header] objectForKey: kKeyEncoding ] unsignedIntValue ];
88+
8189 NSString *line = [NSString stringWithFormat: @" \" %@ \" = \" %@ \" ;\n\n " , key, translation];
82- [( NSFileHandle *)[ self .fileHandlers objectForKey: header] writeData: [line dataUsingEncoding: self .fileEncoding ]];
90+ [fileHandle writeData: [line dataUsingEncoding: encoding ]];
8391 }
8492 }
8593
8694 for (NSString *fileHandleKey in self.fileHandlers )
87- [(NSFileHandle *)[self .fileHandlers objectForKey: fileHandleKey] closeFile ];
95+ {
96+ NSFileHandle *fileHandle = [[self .fileHandlers objectForKey: fileHandleKey] objectForKey: kKeyFileHandle ];
97+ [fileHandle closeFile ];
98+ }
8899}
89100
90101- (void )writeTranslations : (NSDictionary *)translations toPath : (NSString *)path failure : (void (^)(NSError *error))failure
91102{
92103 self.fileHandlers = [NSMutableDictionary dictionary ];
93104
94105 NSError *error;
95- for (NSString *x in headers)
106+ for (NSString *x in self. headers )
96107 {
97108 NSString *languageIdentifier = [NSLocale canonicalLanguageIdentifierFromString: x];
98109
@@ -109,7 +120,7 @@ - (void)writeTranslations:(NSDictionary*)translations toPath:(NSString*)path fai
109120 NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath: stringsPath];
110121 if (fileHandle == nil ) SCLog (@" Unable to open file for writing at path %@ " , stringsPath);
111122
112- [self .fileHandlers setObject: fileHandle forKey: x];
123+ [self .fileHandlers setObject: @{ kKeyFileHandle : fileHandle, kKeyEncoding : @(NSUTF8StringEncoding)} forKey: x];
113124 }
114125
115126 [self writeTranslations: translations failure: failure];
0 commit comments