Skip to content

Commit cf275ee

Browse files
committed
Issue #2
1 parent 19d4c1e commit cf275ee

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

SCStringsUtility/Others/NSString+SCAdditions.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ + (BOOL)detectFileEncoding:(NSStringEncoding *)encoding path:(NSString *)path er
6363
const char *head = [data bytes];
6464
if (*head == '\xFF' && *(head + 1) == '\xFE') {
6565
*encoding = NSUTF16LittleEndianStringEncoding;
66+
} else if (*head == '\xFE' && *(head + 1) == '\xFF') {
67+
*encoding = NSUTF16BigEndianStringEncoding;
6668
}
69+
6770
[fileHandle closeFile];
6871
}
6972

SCStringsUtility/Readers&Writers/SCReader.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ - (id)initWithPath:(NSString*)path
4040
fileHandle = [NSFileHandle fileHandleForReadingAtPath:self.filePath];
4141
if (fileHandle == nil) return nil;
4242

43-
if (![NSString detectFileEncoding:&encoding path:filePath error:nil]) {
43+
NSError *error;
44+
if (![NSString detectFileEncoding:&encoding path:filePath error:&error]) {
4445
// fallback
4546
encoding = NSUTF8StringEncoding;
4647
}

SCStringsUtility/Readers&Writers/SCStringsWriter.m

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
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];

SCStringsUtility/SCStringsController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ -(void)executeGenStringsAtPath:(NSString*)path withRoutine:(NSString*)routine po
238238

239239
NSString *tempFilePath = NSTemporaryDirectory();
240240

241-
NSString *argument = [NSString stringWithFormat:@"find . -name \\*.m | xargs genstrings -o %@", tempFilePath];
241+
NSString *argument = [NSString stringWithFormat:@"find . -name '*.m' | xargs genstrings -o %@", tempFilePath];
242242
if([routine length]) argument = [argument stringByAppendingString:[NSString stringWithFormat:@" -s %@", routine]];
243243
if(!positionalParameters) argument = [argument stringByAppendingString:@" -noPositionalParameters"];
244244

0 commit comments

Comments
 (0)