new optimization that strongly improve the lookup time of labels

This commit is contained in:
Wolfgang Sourdeau
2012-10-18 13:00:59 -04:00
parent 4c6ca4efc2
commit 1e84bccf42

View File

@@ -20,6 +20,7 @@
* Boston, MA 02111-1307, USA.
*/
#import <Foundation/NSDictionary.h>
#import <Foundation/NSBundle.h>
#import <NGObjWeb/SoObjects.h>
@@ -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;
}