(fix) correctly localize mail labels from common bundle

pull/91/head
Ludovic Marcotte 2015-04-03 17:37:46 -04:00 committed by Francis Lachapelle
parent 4252b66b6e
commit 772a682655
5 changed files with 87 additions and 56 deletions

View File

@ -43,7 +43,7 @@
- (NSString *) color; - (NSString *) color;
+ (NSArray *) labelsFromDefaults: (NSDictionary *) theDefaults + (NSArray *) labelsFromDefaults: (NSDictionary *) theDefaults
component: (UIxComponent *) theComponent; component: (id) theComponent;
@end @end

View File

@ -65,7 +65,7 @@
+ (NSArray *) labelsFromDefaults: (NSDictionary *) theDefaults + (NSArray *) labelsFromDefaults: (NSDictionary *) theDefaults
component: (UIxComponent *) theComponent component: (id) theComponent
{ {
NSMutableArray *allLabels, *allKeys; NSMutableArray *allLabels, *allKeys;
NSString *key, *name; NSString *key, *name;
@ -81,7 +81,6 @@
key = [allKeys objectAtIndex: i]; key = [allKeys objectAtIndex: i];
values = [theDefaults objectForKey: key]; values = [theDefaults objectForKey: key];
name = [theComponent commonLabelForKey: [values objectAtIndex: 0]]; name = [theComponent commonLabelForKey: [values objectAtIndex: 0]];
label = [[self alloc] initWithName: key label = [[self alloc] initWithName: key
label: name label: name
color: [values objectAtIndex: 1]]; color: [values objectAtIndex: 1]];

View File

@ -38,6 +38,7 @@
- (WOResponse *) responseWith204; - (WOResponse *) responseWith204;
- (WOResponse *) redirectToLocation: (NSString *) newLocation; - (WOResponse *) redirectToLocation: (NSString *) newLocation;
- (NSString *) commonLabelForKey: (NSString *) _str;
- (NSString *) labelForKey: (NSString *) _str; - (NSString *) labelForKey: (NSString *) _str;
- (WOResourceManager *) pageResourceManager; - (WOResourceManager *) pageResourceManager;

View File

@ -35,8 +35,20 @@
#import "WODirectAction+SOGo.h" #import "WODirectAction+SOGo.h"
static SoProduct *commonProduct = nil;
@implementation WODirectAction (SOGoExtension) @implementation WODirectAction (SOGoExtension)
+ (void) initialize
{
if (commonProduct == nil)
{
// @see commonLabelForKey:
commonProduct = [[SoProduct alloc] initWithBundle:
[NSBundle bundleForClass: NSClassFromString(@"CommonUIProduct")]];
}
}
- (WOResponse *) responseWithStatus: (unsigned int) status - (WOResponse *) responseWithStatus: (unsigned int) status
{ {
WOResponse *response; WOResponse *response;
@ -91,55 +103,60 @@
return response; return response;
} }
- (NSString *) labelForKey: (NSString *) key - (NSString *) labelForKey: (NSString *) _str
withResourceManager: (WOResourceManager *) rm
{ {
NSString *bundleId, *userLanguage, *label; NSString *lKey, *lTable, *lVal;
NSArray *paths; NSRange r;
NSBundle *bundle;
NSDictionary *strings;
SOGoUserDefaults *ud;
static NSMutableDictionary *bundlesCache = nil;
NSMutableDictionary *languagesCache;
if (!bundlesCache) if ([_str length] == 0)
bundlesCache = [NSMutableDictionary new]; return nil;
bundle = [NSBundle bundleForClass: [self class]]; if (rm == nil)
if (!bundle) [self warnWithFormat:@"missing resource manager!"];
bundle = [NSBundle mainBundle];
bundleId = [bundle executablePath]; /* get parameters */
languagesCache = [bundlesCache objectForKey: bundleId]; r = [_str rangeOfString:@"/"];
if (!languagesCache) if (r.length > 0) {
{ lTable = [_str substringToIndex:r.location];
languagesCache = [NSMutableDictionary new]; lKey = [_str substringFromIndex:(r.location + r.length)];
[bundlesCache setObject: languagesCache forKey: bundleId]; }
[languagesCache release]; else {
} lTable = nil;
lKey = _str;
}
lVal = lKey;
ud = [[context activeUser] userDefaults]; if ([lKey hasPrefix:@"$"])
userLanguage = [ud language]; lKey = [self valueForKeyPath:[lKey substringFromIndex:1]];
strings = [languagesCache objectForKey: userLanguage];
if (!strings) if ([lTable hasPrefix:@"$"])
{ lTable = [self valueForKeyPath:[lTable substringFromIndex:1]];
paths = [bundle pathsForResourcesOfType: @"strings"
inDirectory: [NSString stringWithFormat: @"%@.lproj",
userLanguage]
forLocalization: userLanguage];
if ([paths count] > 0)
{
strings = [NSDictionary
dictionaryFromStringsFile: [paths objectAtIndex: 0]];
if (strings)
[languagesCache setObject: strings forKey: userLanguage];
}
}
label = [strings objectForKey: key];
if (!label)
label = key;
return label; /* lookup string */
return [rm stringForKey: lKey
inTableNamed: lTable
withDefaultValue: lVal
languages: [context resourceLookupLanguages]];
}
- (NSString *) commonLabelForKey: (NSString *) _str
{
WOResourceManager *rm;
rm = [commonProduct resourceManager];
return [self labelForKey: _str withResourceManager: rm];
}
- (NSString *) labelForKey: (NSString *) _str
{
WOResourceManager *rm;
/* find resource manager */
rm = [self pageResourceManager];
return [self labelForKey: _str withResourceManager: rm];
} }
- (WOResourceManager *) pageResourceManager - (WOResourceManager *) pageResourceManager

View File

@ -18,6 +18,8 @@
* Boston, MA 02111-1307, USA. * Boston, MA 02111-1307, USA.
*/ */
#import <Foundation/NSDictionary.h>
#import <NGObjWeb/WOContext+SoObjects.h> #import <NGObjWeb/WOContext+SoObjects.h>
#import <NGObjWeb/WODirectAction.h> #import <NGObjWeb/WODirectAction.h>
#import <NGObjWeb/WOResponse.h> #import <NGObjWeb/WOResponse.h>
@ -30,6 +32,7 @@
#import <Mailer/SOGoMailLabel.h> #import <Mailer/SOGoMailLabel.h>
#import <SOGoUI/UIxComponent.h> #import <SOGoUI/UIxComponent.h>
#import <UI/Common/WODirectAction+SOGo.h>
#import "UIxJSONPreferences.h" #import "UIxJSONPreferences.h"
@ -75,27 +78,38 @@
if (![defaults contactsCategories]) if (![defaults contactsCategories])
{ {
categoryLabels = [[[self labelForKey: @"contacts_category_labels"] categoryLabels = [[[self labelForKey: @"contacts_category_labels"]
componentsSeparatedByString: @","] componentsSeparatedByString: @","]
sortedArrayUsingSelector: @selector (localizedCaseInsensitiveCompare:)]; sortedArrayUsingSelector: @selector (localizedCaseInsensitiveCompare:)];
if (!categoryLabels) if (!categoryLabels)
categoryLabels = [NSArray array]; categoryLabels = [NSArray array];
[defaults setContactsCategories: categoryLabels]; [defaults setContactsCategories: categoryLabels];
dirty = YES; dirty = YES;
} }
// Populate default mail lablels, based on the user's preferred language // Populate default mail lablels, based on the user's preferred language
if (![[defaults source] objectForKey: @"SOGoMailLabelsColors"]) if (![[defaults source] objectForKey: @"SOGoMailLabelsColors"])
{ {
NSDictionary *v; SOGoMailLabel *label;
NSArray *labels;
id v;
v = [defaults mailLabelsColors]; int i;
// TODO - translate + refactor to not pass self since it's not a component v = [defaults mailLabelsColors];
//[defaults setMailLabelsColors: [SOGoMailLabel labelsFromDefaults: v component: self]];
[defaults setMailLabelsColors: v]; labels = [SOGoMailLabel labelsFromDefaults: v component: self];
dirty = YES; v = [NSMutableDictionary dictionary];
for (i = 0; i < [labels count]; i++)
{
label = [labels objectAtIndex: i];
[v setObject: [NSArray arrayWithObjects: [label label], [label color], nil]
forKey: [label name]];
}
[defaults setMailLabelsColors: v];
dirty = YES;
} }
if (dirty) if (dirty)