(fix) correctly localize mail labels from common bundle

This commit is contained in:
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];
}
ud = [[context activeUser] userDefaults];
userLanguage = [ud language];
strings = [languagesCache objectForKey: userLanguage];
if (!strings)
{
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];
} }
else {
lTable = nil;
lKey = _str;
} }
lVal = lKey;
label = [strings objectForKey: key]; if ([lKey hasPrefix:@"$"])
if (!label) lKey = [self valueForKeyPath:[lKey substringFromIndex:1]];
label = key;
return label; if ([lTable hasPrefix:@"$"])
lTable = [self valueForKeyPath:[lTable substringFromIndex:1]];
/* 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"
@ -88,12 +91,23 @@
// 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;
int i;
v = [defaults mailLabelsColors]; v = [defaults mailLabelsColors];
// TODO - translate + refactor to not pass self since it's not a component labels = [SOGoMailLabel labelsFromDefaults: v component: self];
//[defaults setMailLabelsColors: [SOGoMailLabel labelsFromDefaults: v component: self]]; 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]; [defaults setMailLabelsColors: v];
dirty = YES; dirty = YES;
} }