From 1e84bccf42e3321d9786378e8d43fd54e93f9b27 Mon Sep 17 00:00:00 2001 From: Wolfgang Sourdeau Date: Thu, 18 Oct 2012 13:00:59 -0400 Subject: [PATCH] new optimization that strongly improve the lookup time of labels --- UI/Common/WODirectAction+SOGo.m | 54 +++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/UI/Common/WODirectAction+SOGo.m b/UI/Common/WODirectAction+SOGo.m index a135ca641..4a50c768a 100644 --- a/UI/Common/WODirectAction+SOGo.m +++ b/UI/Common/WODirectAction+SOGo.m @@ -20,6 +20,7 @@ * Boston, MA 02111-1307, USA. */ +#import #import #import @@ -92,32 +93,59 @@ - (NSString *) labelForKey: (NSString *) key { - NSString *userLanguage, *label; + NSString *bundleId, *userLanguage, *label; NSArray *paths; NSBundle *bundle; NSDictionary *strings; SOGoUserDefaults *ud; + static NSMutableDictionary *bundlesCache = nil; + NSMutableDictionary *languagesCache, *stringsCache; + + if (!bundlesCache) + bundlesCache = [NSMutableDictionary new]; bundle = [NSBundle bundleForClass: [self class]]; if (!bundle) bundle = [NSBundle mainBundle]; + bundleId = [bundle executablePath]; + languagesCache = [bundlesCache objectForKey: bundleId]; + if (!languagesCache) + { + languagesCache = [NSMutableDictionary new]; + [bundlesCache setObject: languagesCache forKey: bundleId]; + [languagesCache release]; + } + ud = [[context activeUser] userDefaults]; userLanguage = [ud language]; - paths = [bundle pathsForResourcesOfType: @"strings" - inDirectory: [NSString stringWithFormat: @"%@.lproj", - userLanguage] - forLocalization: userLanguage]; - if ([paths count] > 0) + stringsCache = [languagesCache objectForKey: userLanguage]; + if (!stringsCache) { - strings = [NSDictionary - dictionaryFromStringsFile: [paths objectAtIndex: 0]]; - label = [strings objectForKey: key]; - if (!label) - label = key; + stringsCache = [NSMutableDictionary new]; + [bundlesCache setObject: stringsCache forKey: userLanguage]; + [stringsCache release]; + } + + label = [stringsCache objectForKey: key]; + if (!label) + { + paths = [bundle pathsForResourcesOfType: @"strings" + inDirectory: [NSString stringWithFormat: @"%@.lproj", + userLanguage] + forLocalization: userLanguage]; + if ([paths count] > 0) + { + strings = [NSDictionary + dictionaryFromStringsFile: [paths objectAtIndex: 0]]; + label = [strings objectForKey: key]; + if (!label) + label = key; + } + else + label = key; + [stringsCache setObject: label forKey: key]; } - else - label = key; return label; }