2011-02-03 17:12:22 +01:00
|
|
|
/* WOResourceManager+SOGo.m - this file is part of SOGo
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Inverse inc
|
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2012-01-31 21:18:49 +01:00
|
|
|
#import <Foundation/NSCharacterSet.h>
|
2011-02-03 17:12:22 +01:00
|
|
|
#import <Foundation/NSData.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
|
|
|
|
|
|
|
#import "WOResourceManager+SOGo.h"
|
|
|
|
|
|
|
|
@implementation WOResourceManager (SOGoExtensions)
|
|
|
|
|
|
|
|
- (NSString *) pathToLocaleForLanguageNamed: (NSString *) _name
|
|
|
|
{
|
|
|
|
static Class MainProduct = Nil;
|
|
|
|
NSString *lpath;
|
2012-01-31 21:18:49 +01:00
|
|
|
NSRange range;
|
|
|
|
NSMutableArray *languages;
|
2011-02-03 17:12:22 +01:00
|
|
|
|
2012-01-31 21:18:49 +01:00
|
|
|
languages = [NSMutableArray arrayWithObject: _name];
|
|
|
|
|
|
|
|
// If the language has a CamelCase form, add the first part to the lookup languages.
|
|
|
|
range = [_name rangeOfCharacterFromSet: [NSCharacterSet uppercaseLetterCharacterSet]
|
|
|
|
options: NSBackwardsSearch
|
|
|
|
range: NSMakeRange(1, [_name length] - 1)];
|
|
|
|
if (range.location != NSNotFound && range.location > 0)
|
|
|
|
[languages addObject: [_name substringToIndex: range.location]];
|
2011-12-22 19:02:59 +01:00
|
|
|
|
2011-02-03 17:12:22 +01:00
|
|
|
lpath = [self pathForResourceNamed: @"Locale"
|
|
|
|
inFramework: nil
|
2012-01-31 21:18:49 +01:00
|
|
|
languages: languages];
|
2011-02-03 17:12:22 +01:00
|
|
|
if (![lpath length])
|
|
|
|
{
|
|
|
|
if (!MainProduct)
|
|
|
|
{
|
|
|
|
MainProduct = NSClassFromString (@"MainUIProduct");
|
|
|
|
if (!MainProduct)
|
|
|
|
[self errorWithFormat: @"did not find MainUIProduct class!"];
|
|
|
|
}
|
|
|
|
|
|
|
|
lpath = [(id) MainProduct performSelector: NSSelectorFromString (@"pathToLocaleForLanguageNamed:")
|
|
|
|
withObject: _name];
|
|
|
|
if (![lpath length])
|
|
|
|
lpath = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *) localeForLanguageNamed: (NSString *) _name
|
|
|
|
{
|
|
|
|
NSString *lpath;
|
|
|
|
id data;
|
|
|
|
NSDictionary *locale;
|
|
|
|
static NSMutableDictionary *localeLUT = nil;
|
|
|
|
|
|
|
|
locale = nil;
|
|
|
|
if ([_name length] > 0)
|
|
|
|
{
|
|
|
|
if (!localeLUT)
|
|
|
|
localeLUT = [NSMutableDictionary new];
|
|
|
|
|
|
|
|
locale = [localeLUT objectForKey: _name];
|
|
|
|
if (!locale)
|
|
|
|
{
|
|
|
|
lpath = [self pathToLocaleForLanguageNamed:_name];
|
|
|
|
if (lpath)
|
|
|
|
{
|
|
|
|
data = [NSData dataWithContentsOfFile: lpath];
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
data = [[[NSString alloc] initWithData: data
|
|
|
|
encoding: NSUTF8StringEncoding] autorelease];
|
|
|
|
locale = [data propertyList];
|
|
|
|
if (locale)
|
|
|
|
[localeLUT setObject: locale forKey: _name];
|
|
|
|
else
|
|
|
|
[self logWithFormat:@"%s couldn't load locale with name:%@",
|
|
|
|
__PRETTY_FUNCTION__,
|
|
|
|
_name];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self logWithFormat:@"%s didn't find locale with name: %@",
|
|
|
|
__PRETTY_FUNCTION__,
|
|
|
|
_name];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self errorWithFormat:@"did not find locale for language: %@", _name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self errorWithFormat:@"%s: name parameter must not be nil!",
|
|
|
|
__PRETTY_FUNCTION__];
|
|
|
|
|
|
|
|
return locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|