From 592e7e3a583527a51db805255088cfb1ca1b62f8 Mon Sep 17 00:00:00 2001 From: Francesco Frison Date: Fri, 28 Jun 2013 15:42:40 +0100 Subject: [PATCH] Adding a translation file, now ZFLangFile is a container for the translations and the real file --- .../Controllers/ZFExportFilesController.m | 14 +- .../Controllers/ZFFileDetailController.h | 4 +- .../Controllers/ZFFileDetailController.m | 14 +- HiperStrings/Controllers/ZFFilesController.h | 2 +- HiperStrings/Controllers/ZFFilesController.m | 35 +--- HiperStrings/Views/ZFFileExportCell.m | 53 +++-- HiperStrings/Views/ZFFileViewCell.h | 4 +- HiperStrings/Views/ZFFileViewCell.m | 2 +- HiperStrings/ZFAppDelegate.m | 2 +- Strings.xcodeproj/project.pbxproj | 28 ++- Strings/ZFLangFile.h | 28 ++- Strings/ZFLangFile.m | 191 ++--------------- Strings/ZFStringScanner.m | 10 +- Strings/ZFTranslationFile.h | 38 ++++ Strings/ZFTranslationFile.m | 194 ++++++++++++++++++ 15 files changed, 362 insertions(+), 257 deletions(-) create mode 100644 Strings/ZFTranslationFile.h create mode 100644 Strings/ZFTranslationFile.m diff --git a/HiperStrings/Controllers/ZFExportFilesController.m b/HiperStrings/Controllers/ZFExportFilesController.m index c6d68e7..d4e8282 100644 --- a/HiperStrings/Controllers/ZFExportFilesController.m +++ b/HiperStrings/Controllers/ZFExportFilesController.m @@ -7,8 +7,9 @@ // #import "ZFExportFilesController.h" -#import "ZFLangFile.h" +#import "ZFTranslationFile.h" #import "ZFFileExportCell.h" +#import "ZFStringsConverter.h" @implementation ZFExportFilesController @@ -20,6 +21,15 @@ - (void)setScanner:(ZFStringScanner *)scanner { #pragma mark - actions - (IBAction)exportAction:(id)sender { + + ZFStringsConverter *converter = [[ZFStringsConverter alloc] init]; + [self.scanner.files enumerateObjectsUsingBlock:^(ZFTranslationFile *langFile, NSUInteger idx, BOOL *stop) { + + }]; + + + + [self.window close]; } @@ -35,7 +45,7 @@ - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { } - (id)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { - ZFLangFile *langFile = [self.scanner.files objectAtIndex:row]; + ZFTranslationFile *langFile = [self.scanner.files objectAtIndex:row]; ZFFileExportCell *cell = [tableView makeViewWithIdentifier:@"langExportCell" owner:self]; [cell setLangFile:langFile]; diff --git a/HiperStrings/Controllers/ZFFileDetailController.h b/HiperStrings/Controllers/ZFFileDetailController.h index 28350c1..95ce1c5 100644 --- a/HiperStrings/Controllers/ZFFileDetailController.h +++ b/HiperStrings/Controllers/ZFFileDetailController.h @@ -9,13 +9,13 @@ // #import -#import "ZFLangFile.h" +#import "ZFTranslationFile.h" @interface ZFFileDetailController : NSObject @property (nonatomic, strong) IBOutlet NSSegmentedControl *segmentedControl; @property (nonatomic, strong) IBOutlet NSTableView *tableView; -@property (nonatomic, strong) ZFLangFile *langFile; +@property (nonatomic, strong) ZFTranslationFile *langFile; - (IBAction)didSwithSegmentedControl:(NSSegmentedControl *)sender; diff --git a/HiperStrings/Controllers/ZFFileDetailController.m b/HiperStrings/Controllers/ZFFileDetailController.m index f89482c..15317cc 100644 --- a/HiperStrings/Controllers/ZFFileDetailController.m +++ b/HiperStrings/Controllers/ZFFileDetailController.m @@ -15,14 +15,14 @@ @interface ZFFileDetailController () @property (nonatomic, strong) NSArray *keys; -@property (nonatomic, strong) NSDictionary *rows; +@property (nonatomic, strong) NSArray *rows; @property (nonatomic, strong) NSArray *columns; @end @implementation ZFFileDetailController -- (void)setLangFile:(ZFLangFile *)langFile { +- (void)setLangFile:(ZFTranslationFile *)langFile { _langFile = langFile; self.columns = [[NSArray arrayWithObject:KEY_KEY] arrayByAddingObjectsFromArray:[_langFile allLanguages]]; @@ -53,7 +53,7 @@ - (void)setLangFile:(ZFLangFile *)langFile { #pragma mark - Segmented Controller - (IBAction)didSwithSegmentedControl:(NSSegmentedControl *)sender { - self.rows = (self.segmentedControl.selectedSegment == 0)? [self.langFile iOStranslations] : [self.langFile androidTranslations]; + self.rows = [self.langFile translationsByType:(self.segmentedControl.selectedSegment == 0)? ZFLangTypeIOS : ZFLangTypeAndorid andLanguageIdentifier:nil]; [self.tableView reloadData]; } @@ -65,7 +65,13 @@ - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { if ([tableColumn.identifier isEqualToString:KEY_KEY]) return [self.keys objectAtIndex:row]; - else return [[self.rows objectForKey:tableColumn.identifier] objectForKey:[self.keys objectAtIndex:row]]; + else { + NSArray *translation = [self.langFile translationsByType:(self.segmentedControl.selectedSegment == 0)? ZFLangTypeIOS : ZFLangTypeAndorid andLanguageIdentifier:tableColumn.identifier]; + ZFLangFile *lang = [translation lastObject]; + return [lang.translations objectForKey:[self.keys objectAtIndex:row]]; + } + + //return [[self.rows objectForKey:tableColumn.identifier] objectForKey:[self.keys objectAtIndex:row]]; } @end diff --git a/HiperStrings/Controllers/ZFFilesController.h b/HiperStrings/Controllers/ZFFilesController.h index 243076f..0500b70 100644 --- a/HiperStrings/Controllers/ZFFilesController.h +++ b/HiperStrings/Controllers/ZFFilesController.h @@ -12,7 +12,7 @@ #import "ZFFileDetailController.h" #import "ZFExportFilesController.h" -@interface ZFFilesController : NSObject +@interface ZFFilesController : NSObject @property (nonatomic, strong) IBOutlet ZFFileDetailController *fileDetailController; @property (nonatomic, strong) IBOutlet NSTableView *filesTable; diff --git a/HiperStrings/Controllers/ZFFilesController.m b/HiperStrings/Controllers/ZFFilesController.m index 8587786..f06c74b 100644 --- a/HiperStrings/Controllers/ZFFilesController.m +++ b/HiperStrings/Controllers/ZFFilesController.m @@ -11,7 +11,7 @@ #import "ZFFilesController.h" #import "ZFFileViewCell.h" #import "ZFStringScanner.h" -#import "ZFLangFile.h" +#import "ZFTranslationFile.h" @interface ZFFilesController () @@ -61,31 +61,6 @@ - (IBAction)exportAction:(id)sender { contextInfo:nil]; } -- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo -{ - /* - NSEnumerator *enumerator; - NSNumber *index; - NSMutableArray *tempArray; - id tempObject; - - if ( returnCode == NSAlertDefaultReturn ) { - enumerator = [tableView selectedRowEnumerator]; - tempArray = [NSMutableArray array]; - - while ( (index = [enumerator nextObject]) ) { - tempObject = [records objectAtIndex:[index intValue]]; - [tempArray addObject:tempObject]; - } - - [records removeObjectsInArray:tempArray]; - [tableView reloadData]; - [self saveData]; - } - */ -} - - - (void)setURLFromDialog:(void (^)(BOOL success)) completed { NSOpenPanel* openDlg = [NSOpenPanel openPanel]; [openDlg setCanChooseFiles:NO]; @@ -107,6 +82,8 @@ - (void)setURLFromDialog:(void (^)(BOOL success)) completed { else [openDlg beginSheetModalForWindow:self.window completionHandler:completitionBlock]; } +#pragma mark - NSTableViewDelegate + - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { return [self.scanner.files count]; } @@ -123,4 +100,10 @@ - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row { return YES; } +#pragma mark NSWindowDelegate + +- (void)windowWillClose:(NSNotification *)notification { + NSWindow *window = [notification object]; +} + @end diff --git a/HiperStrings/Views/ZFFileExportCell.m b/HiperStrings/Views/ZFFileExportCell.m index 0528321..84c7fcb 100644 --- a/HiperStrings/Views/ZFFileExportCell.m +++ b/HiperStrings/Views/ZFFileExportCell.m @@ -8,6 +8,10 @@ #import "ZFFileExportCell.h" +#define I_TO_DROID @"i >> A" +#define DROID_TO_I @"A >> i" +#define SKIP @"X" + @interface ZFFileExportCell () @property (nonatomic, strong) NSButton *button; @@ -34,27 +38,48 @@ - (void)awakeFromNib { [self addSubview:self.button]; } -- (void)setLangFile:(ZFLangFile *)langFile { +- (void)setLangFile:(ZFTranslationFile *)langFile { [super setLangFile:langFile]; + [self.button setTitle:[self stringFromConversionDriver:self.langFile.conversionDriver]]; +} + +- (NSString *)stringFromConversionDriver:(ZFTranslationFileConversionDriver)driver { + NSString *conversionS = nil; - NSString *btnTxt = (langFile.iOSName)? @"i" : @"A"; - [self.button setStringValue:btnTxt]; + switch (driver) { + case ZFTranslationFileConversionDriverIOS: + conversionS = I_TO_DROID; + break; + case ZFTranslationFileConversionDriverAndorid: + conversionS = DROID_TO_I; + break; + case ZFTranslationFileConversionDriverSkip: + default: + conversionS = SKIP; + break; + } + return conversionS; } - (void)btnAction:(NSButton *)sender { - NSString *btnTxt = sender.stringValue; - if ([btnTxt isEqualToString:@"i"]) { - if (self.langFile.androidName) btnTxt = @"A"; - else btnTxt = @"X"; - } - if ([btnTxt isEqualToString:@"A"]) { - btnTxt = @"X"; + ZFTranslationFileConversionDriver newDriver; + switch (self.langFile.conversionDriver) { + case ZFTranslationFileConversionDriverIOS: + newDriver = (self.langFile.androidName)? ZFTranslationFileConversionDriverAndorid : ZFTranslationFileConversionDriverSkip; + break; + case ZFTranslationFileConversionDriverAndorid: + newDriver = ZFTranslationFileConversionDriverSkip; + break; + case ZFTranslationFileConversionDriverSkip: + newDriver = (self.langFile.iOSName)? ZFTranslationFileConversionDriverIOS : ZFTranslationFileConversionDriverAndorid; + break; + default: + break; } - if ([btnTxt isEqualToString:@"X"]) { - if (self.langFile.iOSName) btnTxt = @"i"; - else btnTxt = @"A"; - } + + self.langFile.conversionDriver = newDriver; + [self.button setTitle:[self stringFromConversionDriver:self.langFile.conversionDriver]]; } @end diff --git a/HiperStrings/Views/ZFFileViewCell.h b/HiperStrings/Views/ZFFileViewCell.h index d358a1b..f2b0215 100644 --- a/HiperStrings/Views/ZFFileViewCell.h +++ b/HiperStrings/Views/ZFFileViewCell.h @@ -9,11 +9,11 @@ // #import -#import "ZFLangFile.h" +#import "ZFTranslationFile.h" @interface ZFFileViewCell : NSTableCellView -@property (nonatomic, strong) ZFLangFile *langFile; +@property (nonatomic, strong) ZFTranslationFile *langFile; @property (nonatomic, strong) NSTextField *titleLbl; @property (nonatomic, strong) NSTextField *detailLbl; diff --git a/HiperStrings/Views/ZFFileViewCell.m b/HiperStrings/Views/ZFFileViewCell.m index 959a81c..4c387df 100644 --- a/HiperStrings/Views/ZFFileViewCell.m +++ b/HiperStrings/Views/ZFFileViewCell.m @@ -25,7 +25,7 @@ - (void)awakeFromNib { } -- (void)setLangFile:(ZFLangFile *)langFile { +- (void)setLangFile:(ZFTranslationFile *)langFile { _langFile = langFile; if (_langFile.iOSName.length > 0) [self.titleLbl setStringValue:_langFile.iOSName]; if (_langFile.androidName.length > 0) [self.detailLbl setStringValue:_langFile.androidName]; diff --git a/HiperStrings/ZFAppDelegate.m b/HiperStrings/ZFAppDelegate.m index 661752a..5c6aadf 100644 --- a/HiperStrings/ZFAppDelegate.m +++ b/HiperStrings/ZFAppDelegate.m @@ -10,7 +10,7 @@ #import "ZFAppDelegate.h" #import "ZFStringScanner.h" -#import "ZFLangFile.h" +#import "ZFTranslationFile.h" @interface ZFAppDelegate () diff --git a/Strings.xcodeproj/project.pbxproj b/Strings.xcodeproj/project.pbxproj index ced7351..17b47b5 100644 --- a/Strings.xcodeproj/project.pbxproj +++ b/Strings.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 1323015F177DD7DD008EB3D5 /* ZFLangFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 1371BBDD177DBDD9008EB467 /* ZFLangFile.m */; }; 134E98D617799C4400D7D54B /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 134E98D517799C4400D7D54B /* Foundation.framework */; }; 134E98D917799C4400D7D54B /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 134E98D817799C4400D7D54B /* main.m */; }; 134E98DD17799C4400D7D54B /* Strings.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = 134E98DC17799C4400D7D54B /* Strings.1 */; }; @@ -36,9 +37,10 @@ 1371BBD5177B5954008EB467 /* icon.iconset in Resources */ = {isa = PBXBuildFile; fileRef = 1371BBD4177B5954008EB467 /* icon.iconset */; }; 1371BBD8177D8E63008EB467 /* ZFExportFilesController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1371BBD7177D8E63008EB467 /* ZFExportFilesController.m */; }; 1371BBDB177D90DD008EB467 /* ZFFileExportCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 1371BBDA177D90DD008EB467 /* ZFFileExportCell.m */; }; - 13B17AED177AFB6000C0473E /* ZFLangFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B17AEC177AFB6000C0473E /* ZFLangFile.m */; }; - 13B17AEE177AFE2800C0473E /* ZFLangFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B17AEC177AFB6000C0473E /* ZFLangFile.m */; }; - 13B17AEF177AFE2800C0473E /* ZFLangFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B17AEC177AFB6000C0473E /* ZFLangFile.m */; }; + 1371BBDE177DBDD9008EB467 /* ZFLangFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 1371BBDD177DBDD9008EB467 /* ZFLangFile.m */; }; + 13B17AED177AFB6000C0473E /* ZFTranslationFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B17AEC177AFB6000C0473E /* ZFTranslationFile.m */; }; + 13B17AEE177AFE2800C0473E /* ZFTranslationFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B17AEC177AFB6000C0473E /* ZFTranslationFile.m */; }; + 13B17AEF177AFE2800C0473E /* ZFTranslationFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B17AEC177AFB6000C0473E /* ZFTranslationFile.m */; }; 13B17AF2177B02F700C0473E /* ZFUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B17AF1177B02F700C0473E /* ZFUtils.m */; }; 13B17AF3177B051C00C0473E /* ZFUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B17AF1177B02F700C0473E /* ZFUtils.m */; }; 13B17AF4177B052100C0473E /* ZFUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B17AF1177B02F700C0473E /* ZFUtils.m */; }; @@ -105,8 +107,10 @@ 1371BBD7177D8E63008EB467 /* ZFExportFilesController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ZFExportFilesController.m; path = Controllers/ZFExportFilesController.m; sourceTree = ""; }; 1371BBD9177D90DD008EB467 /* ZFFileExportCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ZFFileExportCell.h; path = Views/ZFFileExportCell.h; sourceTree = ""; }; 1371BBDA177D90DD008EB467 /* ZFFileExportCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ZFFileExportCell.m; path = Views/ZFFileExportCell.m; sourceTree = ""; }; - 13B17AEB177AFB6000C0473E /* ZFLangFile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZFLangFile.h; sourceTree = ""; }; - 13B17AEC177AFB6000C0473E /* ZFLangFile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ZFLangFile.m; sourceTree = ""; }; + 1371BBDC177DBDD9008EB467 /* ZFLangFile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZFLangFile.h; sourceTree = ""; }; + 1371BBDD177DBDD9008EB467 /* ZFLangFile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ZFLangFile.m; sourceTree = ""; }; + 13B17AEB177AFB6000C0473E /* ZFTranslationFile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZFTranslationFile.h; sourceTree = ""; }; + 13B17AEC177AFB6000C0473E /* ZFTranslationFile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ZFTranslationFile.m; sourceTree = ""; }; 13B17AF0177B02F700C0473E /* ZFUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZFUtils.h; sourceTree = ""; }; 13B17AF1177B02F700C0473E /* ZFUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ZFUtils.m; sourceTree = ""; }; /* End PBXFileReference section */ @@ -198,10 +202,12 @@ 134E98E717799DC900D7D54B /* ZFStringsConverter.m */, 134E990A1779A80700D7D54B /* ZFStringScanner.h */, 134E990B1779A80700D7D54B /* ZFStringScanner.m */, - 13B17AEB177AFB6000C0473E /* ZFLangFile.h */, - 13B17AEC177AFB6000C0473E /* ZFLangFile.m */, + 13B17AEB177AFB6000C0473E /* ZFTranslationFile.h */, + 13B17AEC177AFB6000C0473E /* ZFTranslationFile.m */, 13B17AF0177B02F700C0473E /* ZFUtils.h */, 13B17AF1177B02F700C0473E /* ZFUtils.m */, + 1371BBDC177DBDD9008EB467 /* ZFLangFile.h */, + 1371BBDD177DBDD9008EB467 /* ZFLangFile.m */, ); name = Models; sourceTree = ""; @@ -425,8 +431,9 @@ 134E98D917799C4400D7D54B /* main.m in Sources */, 134E98E917799DC900D7D54B /* ZFStringsConverter.m in Sources */, 134E990C1779A80700D7D54B /* ZFStringScanner.m in Sources */, - 13B17AED177AFB6000C0473E /* ZFLangFile.m in Sources */, + 13B17AED177AFB6000C0473E /* ZFTranslationFile.m in Sources */, 13B17AF2177B02F700C0473E /* ZFUtils.m in Sources */, + 1371BBDE177DBDD9008EB467 /* ZFLangFile.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -438,7 +445,7 @@ 134E99031779A73900D7D54B /* ZFAppDelegate.m in Sources */, 134E990D1779AAD800D7D54B /* ZFStringsConverter.m in Sources */, 134E990E1779AADA00D7D54B /* ZFStringScanner.m in Sources */, - 13B17AEE177AFE2800C0473E /* ZFLangFile.m in Sources */, + 13B17AEE177AFE2800C0473E /* ZFTranslationFile.m in Sources */, 13B17AF3177B051C00C0473E /* ZFUtils.m in Sources */, 1371BBC1177B23A5008EB467 /* ZFFilesController.m in Sources */, 1371BBC6177B2410008EB467 /* ZFFileViewCell.m in Sources */, @@ -446,6 +453,7 @@ 1371BBCC177B2C1A008EB467 /* ZFTranslationView.m in Sources */, 1371BBD8177D8E63008EB467 /* ZFExportFilesController.m in Sources */, 1371BBDB177D90DD008EB467 /* ZFFileExportCell.m in Sources */, + 1323015F177DD7DD008EB3D5 /* ZFLangFile.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -456,7 +464,7 @@ 134E99221779E11C00D7D54B /* StringsTests.m in Sources */, 134E992B1779E16500D7D54B /* ZFStringConverterTests.m in Sources */, 134E992C1779E6B300D7D54B /* ZFStringsConverter.m in Sources */, - 13B17AEF177AFE2800C0473E /* ZFLangFile.m in Sources */, + 13B17AEF177AFE2800C0473E /* ZFTranslationFile.m in Sources */, 13B17AF4177B052100C0473E /* ZFUtils.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Strings/ZFLangFile.h b/Strings/ZFLangFile.h index 59577e3..b74068e 100644 --- a/Strings/ZFLangFile.h +++ b/Strings/ZFLangFile.h @@ -2,27 +2,25 @@ // ZFLangFile.h // Strings // -// Created by Francesco on 26/06/2013. -// -// Open Source Initiative OSI - The MIT License (MIT):Licensing [OSI Approved License] The MIT License (MIT) -// Copyright (c) 2013 ziofritz. +// Created by Francesco on 28/06/2013. +// Copyright (c) 2013 ziofritz. All rights reserved. // #import -@interface ZFLangFile : NSObject - -@property (nonatomic, strong) NSString *iOSName; -@property (nonatomic, strong) NSString *androidName; +typedef enum { + ZFLangTypeIOS, + ZFLangTypeAndorid +} ZFLangType; -@property (nonatomic, strong) NSMutableDictionary *iOStranslations; -@property (nonatomic, strong) NSMutableDictionary *androidTranslations; +@interface ZFLangFile : NSObject -@property (nonatomic, strong) NSArray *allKeys; -@property (nonatomic, strong) NSArray *allLanguages; +@property (nonatomic, strong, readonly) NSURL *url; +@property (nonatomic, strong, readonly) NSString *fileName; +@property (nonatomic, assign, readonly) ZFLangType type; +@property (nonatomic, strong, readonly) NSString *language; +@property (nonatomic, strong, readonly) NSMutableDictionary *translations; -- (BOOL)addFileAtURL:(NSURL *) url; -- (BOOL)mergeWithFile:(ZFLangFile *)file; -- (void)finalizeMerge; +- (id)initWithURL:(NSURL *)url; @end diff --git a/Strings/ZFLangFile.m b/Strings/ZFLangFile.m index dae2f2f..0fab945 100644 --- a/Strings/ZFLangFile.m +++ b/Strings/ZFLangFile.m @@ -2,76 +2,15 @@ // ZFLangFile.m // Strings // -// Created by Francesco on 26/06/2013. -// -// Open Source Initiative OSI - The MIT License (MIT):Licensing [OSI Approved License] The MIT License (MIT) -// Copyright (c) 2013 ziofritz. +// Created by Francesco on 28/06/2013. +// Copyright (c) 2013 ziofritz. All rights reserved. // #import "ZFLangFile.h" #import "ZFStringsConverter.h" -#import "ZFUtils.h" - -#define FAV_LANG @"en" -#define KEY_COMAPARISON 5 - -@interface ZFLangFile () - -@property (nonatomic, strong) NSArray *hashKeys; - -@end - @implementation ZFLangFile -- (id)init { - self = [super init]; - if (self) { - self.iOStranslations = [NSMutableDictionary dictionary]; - self.androidTranslations = [NSMutableDictionary dictionary]; - } - return self; -} - - -/*! - @abstract - Generate a list of minimum 5 keys to be used for comparison - - @return NSArray of keys - - @discussion The method will first look at allKeys, then if missing or too small, will try to get them from either iOStranslation or androidTRanslations. - - */ - -- (NSArray *)hashKeys { - if (!_hashKeys) { - - _hashKeys = [self allKeys]; - if (!_hashKeys || _hashKeys.count < KEY_COMAPARISON) { - NSDictionary *source = (self.iOStranslations.count > 0)? self.iOStranslations : self.androidTranslations; - NSString *key = ([[source allKeys] containsObject:@"en"])? @"en" : [[source allKeys] lastObject]; - - NSArray *keys = [[[source objectForKey:key] allKeys] sortedArrayUsingSelector:@selector(compare:)]; - _hashKeys = [keys subarrayWithRange:NSMakeRange(0, MIN(KEY_COMAPARISON, keys.count))]; - } - - } - return _hashKeys; -} - -- (BOOL)isEqual:(id)object { - if (![object isKindOfClass:[self class]]) return [super isEqual:object]; - - __block int count = 0; - [self.hashKeys enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) { - if ([[(ZFLangFile *)object hashKeys] containsObject:obj]) count++; - *stop = (count >= KEY_COMAPARISON); - }]; - - return (count >= MIN(KEY_COMAPARISON, self.hashKeys.count)); -} - /*! @abstract @@ -85,121 +24,25 @@ - (BOOL)isEqual:(id)object { */ -- (BOOL)addFileAtURL:(NSURL *)url { - BOOL isIOS; - NSString *lang = [[ZFUtils sharedUtils] langFromURL:url isIOS:&isIOS]; - if (!lang) return NO; - - BOOL alreadyExists = (isIOS)? ([self.iOStranslations objectForKey:lang] != nil) : ([self.androidTranslations objectForKey:lang] != nil); - if (alreadyExists) return NO; - - if (isIOS && !self.iOSName) self.iOSName = [url lastPathComponent]; - if (!isIOS && !self.androidName) self.androidName = [url lastPathComponent]; - - ZFStringsConverter *converter = [[ZFStringsConverter alloc] init]; - NSDictionary *translations = (isIOS)? [converter translationsForStringsAtURL:url] : [converter translationsForXMLAtURL:url]; - if (!translations || translations.count == 0) return NO; - if (isIOS) { - [self.iOStranslations setObject:translations forKey:lang]; - } - else { - [self.androidTranslations setObject:translations forKey:lang]; - } - - self.allKeys = nil; - self.allLanguages = nil; - - return YES; -} - -/*! - @abstract - Tries to merge a given file if and only if the there is amtch between the keys - - @param file, the fiel to be merged - - @return YES if the file have been merged, NO otherwise - - @discussion The act of cross reference the keys and then merging the languages and the translations is expensive. Maybe need refactoring - - */ - -- (BOOL)mergeWithFile:(ZFLangFile *)file { - if (![self isEqual:file]) return NO; - - __block BOOL mergeIOS = NO; - [file.iOStranslations enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary *obj, BOOL *stop) { - if ([self.iOStranslations.allKeys containsObject:key]) return; +- (id)initWithURL:(NSURL *)url { + self = [self init]; + if (self) { + BOOL isIOS; + NSString *lang = [[ZFUtils sharedUtils] langFromURL:url isIOS:&isIOS]; + if (!lang) return nil; - [self.iOStranslations setObject:obj forKey:key]; - mergeIOS = YES; - }]; - if (!self.iOSName && file.iOSName) self.iOSName = file.iOSName; - - __block BOOL mergeAndroid = NO; - [file.androidTranslations enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary *obj, BOOL *stop) { - if ([self.androidTranslations.allKeys containsObject:key]) return; + _url = url; + _fileName = [url lastPathComponent]; + _type = (isIOS)? ZFLangTypeIOS : ZFLangTypeAndorid; + _language = lang; - [self.androidTranslations setObject:obj forKey:key]; - mergeAndroid = YES; - }]; - - - if (!self.androidName && file.androidName) self.androidName = file.androidName; - - - BOOL didMerge = (mergeIOS || mergeAndroid); - if (didMerge) { - self.allKeys = nil; - self.allLanguages = nil; + ZFStringsConverter *converter = [[ZFStringsConverter alloc] init]; + NSDictionary *translations = (isIOS)? [converter translationsForStringsAtURL:url] : [converter translationsForXMLAtURL:url]; + + _translations = [NSMutableDictionary dictionaryWithDictionary:translations]; } - return didMerge; -} - -/*! - @abstract - Fill the blanks in languages and translation keys - - @discussion Used to generate the getters for allKeys and allLanguages. Those two get constantly nilled by addFileAtURL and mergeWithFile, so they may become source of problems (dead loops in primis) - - */ - -- (void)fillGaps { - NSMutableArray *allKeys = [NSMutableArray array]; - NSMutableArray *allLanguages = [NSMutableArray array]; - - [self.iOStranslations enumerateKeysAndObjectsUsingBlock:^(NSString *lang, NSDictionary *translation, BOOL *stop) { - if (![allLanguages containsObject:lang]) [allLanguages addObject:lang]; - [translation.allKeys enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop) { - if ([allKeys containsObject:key]) return; - [allKeys addObject:key]; - }]; - }]; - - [self.androidTranslations enumerateKeysAndObjectsUsingBlock:^(NSString *lang, NSDictionary *translation, BOOL *stop) { - if (![allLanguages containsObject:lang]) [allLanguages addObject:lang]; - [translation.allKeys enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop) { - if ([allKeys containsObject:key]) return; - [allKeys addObject:key]; - }]; - }]; - - _allKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)]; - _allLanguages = [allLanguages sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) { - if ([obj1 isEqualToString:FAV_LANG]) return NSOrderedAscending; - else if ([obj2 isEqualToString:FAV_LANG]) return NSOrderedDescending; - else return [obj1 compare:obj2]; - }]; -} - -- (NSArray *)allKeys { - if (!_allKeys) [self fillGaps]; - return _allKeys; -} + return self; -- (NSArray *)allLanguages { - if (!_allLanguages) [self fillGaps]; - return _allLanguages; } @end diff --git a/Strings/ZFStringScanner.m b/Strings/ZFStringScanner.m index 6233557..df7cf5c 100644 --- a/Strings/ZFStringScanner.m +++ b/Strings/ZFStringScanner.m @@ -10,7 +10,7 @@ #import "ZFStringScanner.h" #import "ZFStringsConverter.h" -#import "ZFLangFile.h" +#import "ZFTranslationFile.h" @interface ZFStringScanner () @@ -62,7 +62,7 @@ - (void)scanAtURL:(NSURL *)URL { if (!isDir) { - ZFLangFile *file = [[ZFLangFile alloc] init]; + ZFTranslationFile *file = [[ZFTranslationFile alloc] init]; BOOL valid = [file addFileAtURL:fileURL]; if (valid) [self.files addObject:file]; @@ -88,7 +88,7 @@ - (void)startScanAtURL:(NSURL *)URL { [self scanAtURL:URL]; NSMutableArray *groups = [NSMutableArray array]; - [self.files enumerateObjectsUsingBlock:^(ZFLangFile *file, NSUInteger idx, BOOL *stop) { + [self.files enumerateObjectsUsingBlock:^(ZFTranslationFile *file, NSUInteger idx, BOOL *stop) { __block BOOL inserted = NO; [groups enumerateObjectsUsingBlock:^(NSMutableArray *group, NSUInteger idx, BOOL *stop) { if ([file isEqual:[group objectAtIndex:0]]) { @@ -103,8 +103,8 @@ - (void)startScanAtURL:(NSURL *)URL { NSMutableArray *result = [NSMutableArray array]; [groups enumerateObjectsUsingBlock:^(NSMutableArray *group, NSUInteger idx, BOOL *stop) { - ZFLangFile *file = [group objectAtIndex:0]; - [group enumerateObjectsUsingBlock:^(ZFLangFile *anotherFile, NSUInteger idx, BOOL *stop) { + ZFTranslationFile *file = [group objectAtIndex:0]; + [group enumerateObjectsUsingBlock:^(ZFTranslationFile *anotherFile, NSUInteger idx, BOOL *stop) { if (idx == 0) return; [file mergeWithFile:anotherFile]; }]; diff --git a/Strings/ZFTranslationFile.h b/Strings/ZFTranslationFile.h new file mode 100644 index 0000000..f13c00a --- /dev/null +++ b/Strings/ZFTranslationFile.h @@ -0,0 +1,38 @@ +// +// ZFLangFile.h +// Strings +// +// Created by Francesco on 26/06/2013. +// +// Open Source Initiative OSI - The MIT License (MIT):Licensing [OSI Approved License] The MIT License (MIT) +// Copyright (c) 2013 ziofritz. +// + +#import +#import "ZFLangFile.h" + +typedef enum { + ZFTranslationFileConversionDriverSkip, + ZFTranslationFileConversionDriverIOS, + ZFTranslationFileConversionDriverAndorid +} ZFTranslationFileConversionDriver; + +@interface ZFTranslationFile : NSObject + +@property (nonatomic, assign) ZFTranslationFileConversionDriver conversionDriver; + +@property (nonatomic, strong) NSString *iOSName; +@property (nonatomic, strong) NSString *androidName; + +@property (nonatomic, strong) NSMutableArray *translations; + +@property (nonatomic, strong) NSArray *allKeys; +@property (nonatomic, strong) NSArray *allLanguages; + +- (BOOL)addFileAtURL:(NSURL *) url; +- (BOOL)mergeWithFile:(ZFTranslationFile *)file; +- (void)finalizeMerge; + +- (NSArray *)translationsByType:(ZFLangType)type andLanguageIdentifier:(NSString *)identifier; + +@end diff --git a/Strings/ZFTranslationFile.m b/Strings/ZFTranslationFile.m new file mode 100644 index 0000000..6ddb0ea --- /dev/null +++ b/Strings/ZFTranslationFile.m @@ -0,0 +1,194 @@ +// +// ZFLangFile.m +// Strings +// +// Created by Francesco on 26/06/2013. +// +// Open Source Initiative OSI - The MIT License (MIT):Licensing [OSI Approved License] The MIT License (MIT) +// Copyright (c) 2013 ziofritz. +// + +#import "ZFTranslationFile.h" +#import "ZFLangFile.h" +#import "ZFUtils.h" + +#define FAV_LANG @"en" +#define KEY_COMAPARISON 5 + +@interface ZFTranslationFile () + +@property (nonatomic, strong) NSArray *hashKeys; + +@end + + +@implementation ZFTranslationFile + +- (id)init { + self = [super init]; + if (self) { + self.translations = [NSMutableArray array]; + self.conversionDriver = ZFTranslationFileConversionDriverSkip; + } + return self; +} + + +/*! + @abstract + Generate a list of minimum 5 keys to be used for comparison + + @return NSArray of keys + + @discussion The method will first look at allKeys, then if missing or too small, will try to get them from either iOStranslation or androidTRanslations. + + */ + +- (NSArray *)hashKeys { + if (!_hashKeys) { + + _hashKeys = [self allKeys]; + if (!_hashKeys || _hashKeys.count < KEY_COMAPARISON) { + + [self.translations enumerateObjectsUsingBlock:^(ZFLangFile *obj, NSUInteger idx, BOOL *stop) { + _hashKeys = obj.translations.allKeys; + *stop = (_hashKeys.count >= KEY_COMAPARISON); + }]; + + _hashKeys = [_hashKeys subarrayWithRange:NSMakeRange(0, MIN(KEY_COMAPARISON, _hashKeys.count))]; + } + + } + return _hashKeys; +} + +- (BOOL)isEqual:(id)object { + if (![object isKindOfClass:[self class]]) return [super isEqual:object]; + + __block int count = 0; + [self.hashKeys enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) { + if ([[(ZFTranslationFile *)object hashKeys] containsObject:obj]) count++; + *stop = (count >= KEY_COMAPARISON); + }]; + + return (count >= MIN(KEY_COMAPARISON, self.hashKeys.count)); +} + + +/*! + @abstract + check lang file to add to the names + + @param langFile to be analized + + */ + +- (void)checkUpNameForLang:(ZFLangFile *)langFile { + if (!langFile) return; + if ((langFile.type == ZFLangTypeIOS) && !self.iOSName) self.iOSName = langFile.fileName; + if ((langFile.type == ZFLangTypeAndorid) && !self.androidName) self.androidName = langFile.fileName; + + if (self.conversionDriver == ZFTranslationFileConversionDriverSkip) self.conversionDriver = (self.iOSName)? ZFTranslationFileConversionDriverIOS : ZFTranslationFileConversionDriverAndorid; +} + + +/*! + @abstract + Preferred method to convert a file at URL to ZFLangFile + + @param url of the file in the disk + + @return YES if the operation of adding result succesfull No otherwise + + @discussion This method covnert the url in a dictioanry of keys and transaltions to be addedd to the translations for the relative language identifier + + */ + +- (BOOL)addFileAtURL:(NSURL *)url { + + ZFLangFile *lang = [[ZFLangFile alloc] initWithURL:url]; + if (!lang) return NO; + + [self.translations addObject:lang]; + [self checkUpNameForLang:lang]; + + self.allKeys = nil; + self.allLanguages = nil; + + return YES; +} + +/*! + @abstract + Tries to merge a given file if and only if the there is amtch between the keys + + @param file, the fiel to be merged + + @return YES if the file have been merged, NO otherwise + + @discussion The act of cross reference the keys and then merging the languages and the translations is expensive. Maybe need refactoring + + */ + +- (BOOL)mergeWithFile:(ZFTranslationFile *)file { + if (![self isEqual:file]) return NO; + + [self.translations addObjectsFromArray:file.translations]; + + ZFLangFile *lastLang = [file.translations lastObject]; + [self checkUpNameForLang:lastLang]; + + if (lastLang) { + self.allKeys = nil; + self.allLanguages = nil; + } + + return (lastLang != nil); +} + +/*! + @abstract + Fill the blanks in languages and translation keys + + @discussion Used to generate the getters for allKeys and allLanguages. Those two get constantly nilled by addFileAtURL and mergeWithFile, so they may become source of problems (dead loops in primis) + + */ + +- (void)fillGaps { + NSMutableArray *allKeys = [NSMutableArray array]; + NSMutableArray *allLanguages = [NSMutableArray array]; + + [self.translations enumerateObjectsUsingBlock:^(ZFLangFile *lang, NSUInteger idx, BOOL *stop) { + if (![allLanguages containsObject:lang.language]) [allLanguages addObject:lang.language]; + [lang.translations.allKeys enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop) { + if ([allKeys containsObject:key]) return; + [allKeys addObject:key]; + }]; + }]; + + _allKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)]; + _allLanguages = [allLanguages sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) { + if ([obj1 isEqualToString:FAV_LANG]) return NSOrderedAscending; + else if ([obj2 isEqualToString:FAV_LANG]) return NSOrderedDescending; + else return [obj1 compare:obj2]; + }]; +} + +- (NSArray *)allKeys { + if (!_allKeys) [self fillGaps]; + return _allKeys; +} + +- (NSArray *)allLanguages { + if (!_allLanguages) [self fillGaps]; + return _allLanguages; +} + + + +- (NSArray *)translationsByType:(ZFLangType)type andLanguageIdentifier:(NSString *)identifier { + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type = %d && language = %@", type, identifier]; + return [self.translations filteredArrayUsingPredicate:predicate]; +} + +@end