1313@interface SCStringsWriter ()
1414@property (nonatomic , strong ) NSMutableDictionary *fileHandlers;
1515@property (nonatomic , strong ) NSArray *headers;
16+ @property (nonatomic , assign ) NSStringEncoding fileEncoding;
1617@end
1718
1819@implementation SCStringsWriter
1920@synthesize fileHandlers;
2021@synthesize headers;
22+ @synthesize fileEncoding;
2123
2224- (id )initWithHeaders : (NSArray *)heads
2325{
@@ -42,6 +44,11 @@ - (id)initWithTranslationFiles:(NSDictionary *)files
4244 {
4345 NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath: [x objectForKey: @" Path" ]];
4446 if (fileHandle == nil ) {SCLog (@" Unable to open file for writing at path %@ " , [x objectForKey: @" Path" ]); return nil ;}
47+
48+ if (![NSString detectFileEncoding: &fileEncoding path: [x objectForKey: @" Path" ] error: nil ]) {
49+ // fallback
50+ fileEncoding = NSUTF8StringEncoding;
51+ }
4552
4653 [self .fileHandlers setObject: fileHandle forKey: [x objectForKey: @" Language" ]];
4754 [heads addObject: [x objectForKey: @" Language" ]];
@@ -63,7 +70,7 @@ - (void)writeTranslations:(NSDictionary*)translations failure:(void(^)(NSError *
6370 {
6471 NSString *comment = [NSString stringWithFormat: @" %@ \n " , [[translationDict objectForKey: @" Comment" ] stringByReplacingOccurrencesOfString: @" \\ n" withString: @" \n " ]];
6572 for (NSString *fileHandleKey in self.fileHandlers )
66- [(NSFileHandle *)[self .fileHandlers objectForKey: fileHandleKey] writeData: [comment dataUsingEncoding: NSUTF8StringEncoding ]];
73+ [(NSFileHandle *)[self .fileHandlers objectForKey: fileHandleKey] writeData: [comment dataUsingEncoding: self .fileEncoding ]];
6774 }
6875
6976 for (NSString *header in self.headers )
@@ -72,7 +79,7 @@ - (void)writeTranslations:(NSDictionary*)translations failure:(void(^)(NSError *
7279 if (!translation.length ) translation = key;
7380
7481 NSString *line = [NSString stringWithFormat: @" \" %@ \" = \" %@ \" ;\n\n " , key, translation];
75- [(NSFileHandle *)[self .fileHandlers objectForKey: header] writeData: [line dataUsingEncoding: NSUTF8StringEncoding ]];
82+ [(NSFileHandle *)[self .fileHandlers objectForKey: header] writeData: [line dataUsingEncoding: self .fileEncoding ]];
7683 }
7784 }
7885
0 commit comments