1/* WOResourceManager+SOGo.m - this file is part of SOGo
2 *
3 * Copyright (C) 2011 Inverse inc
4 *
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6 *
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING.  If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#import <Foundation/NSCharacterSet.h>
24#import <Foundation/NSData.h>
25#import <Foundation/NSDictionary.h>
26
27#import <NGExtensions/NSObject+Logs.h>
28
29#import "WOResourceManager+SOGo.h"
30
31@implementation WOResourceManager (SOGoExtensions)
32
33- (NSString *) pathToLocaleForLanguageNamed: (NSString *) _name
34{
35  static Class MainProduct = Nil;
36  NSString *lpath;
37  NSRange range;
38  NSMutableArray *languages;
39
40  languages = [NSMutableArray arrayWithObject: _name];
41
42  // If the language has a CamelCase form, add the first part to the lookup languages.
43  range = [_name rangeOfCharacterFromSet: [NSCharacterSet uppercaseLetterCharacterSet]
44                                 options: NSBackwardsSearch
45                                   range: NSMakeRange(1, [_name length] - 1)];
46  if (range.location != NSNotFound && range.location > 0)
47    [languages addObject: [_name substringToIndex: range.location]];
48
49  lpath = [self pathForResourceNamed: @"Locale"
50			 inFramework: nil
51			   languages: languages];
52  if (![lpath length])
53    {
54      if (!MainProduct)
55        {
56          MainProduct = NSClassFromString (@"MainUIProduct");
57          if (!MainProduct)
58            [self errorWithFormat: @"did not find MainUIProduct class!"];
59        }
60
61      lpath = [(id) MainProduct performSelector: NSSelectorFromString (@"pathToLocaleForLanguageNamed:")
62				     withObject: _name];
63      if (![lpath length])
64        lpath = nil;
65    }
66
67  return lpath;
68}
69
70- (NSDictionary *) localeForLanguageNamed: (NSString *) _name
71{
72  NSString     *lpath;
73  id           data;
74  NSDictionary *locale;
75  static NSMutableDictionary *localeLUT = nil;
76
77  locale = nil;
78  if ([_name length] > 0)
79    {
80      if (!localeLUT)
81	localeLUT = [NSMutableDictionary new];
82
83      locale = [localeLUT objectForKey: _name];
84      if (!locale)
85        {
86          lpath = [self pathToLocaleForLanguageNamed:_name];
87          if (lpath)
88            {
89              data = [NSData dataWithContentsOfFile: lpath];
90              if (data)
91                {
92                  data = [[[NSString alloc] initWithData: data
93                                            encoding: NSUTF8StringEncoding] autorelease];
94                  locale = [data propertyList];
95                  if (locale)
96                    [localeLUT setObject: locale forKey: _name];
97                  else
98                    [self logWithFormat:@"%s couldn't load locale with name:%@",
99                          __PRETTY_FUNCTION__,
100                          _name];
101                }
102              else
103                [self logWithFormat:@"%s didn't find locale with name: %@",
104                      __PRETTY_FUNCTION__,
105                      _name];
106            }
107          else
108            [self errorWithFormat:@"did not find locale for language: %@", _name];
109        }
110    }
111  else
112    [self errorWithFormat:@"%s: name parameter must not be nil!",
113          __PRETTY_FUNCTION__];
114
115  return locale;
116}
117
118@end
119