Localisator.m
5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// Localisator.m
// CustomLocalisator
//
// Created by Michael Azevedo on 05/03/2014.
//
#import "Localisator.h"
static NSString * const kSaveLanguageDefaultKey = @"kSaveLanguageDefaultKey";
static NSString * const kSaveSystemLanguageKey = @"kSaveSystemLanguageKey";
@interface Localisator()
@property NSDictionary * dicoLocalisation;
@property NSUserDefaults * defaults;
@end
@implementation Localisator
#pragma mark - Singleton Method
+ (Localisator*)sharedInstance
{
static Localisator *_sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedInstance = [[Localisator alloc] init];
});
return _sharedInstance;
}
#pragma mark - Init methods
- (id)init
{
self = [super init];
if (self)
{
_defaults = [NSUserDefaults standardUserDefaults];
_availableLanguagesArray = @[kSimplifiedChinese, kTraditionalChinese, kEnglish];
_dicoLocalisation = nil;
_currentLanguage = @"DeviceLanguage";
NSString *languageSaved = nil;
//系统语言优先级最高
NSString *savedSystemLanguage = [_defaults objectForKey:kSaveSystemLanguageKey];
NSString *currentSystemLanguage = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0];
if ([currentSystemLanguage hasPrefix:@"zh-Hans"]) {
currentSystemLanguage = kSimplifiedChinese;
}
if ([currentSystemLanguage hasPrefix:@"zh-Hant"] || [currentSystemLanguage isEqualToString:@"zh-HK"] || [currentSystemLanguage isEqualToString:@"zh-TW"]) {
currentSystemLanguage = kTraditionalChinese;
}
if (![currentSystemLanguage isEqualToString:savedSystemLanguage]) {
[_defaults setObject:currentSystemLanguage forKey:kSaveSystemLanguageKey];
if ([currentSystemLanguage isEqualToString:kSimplifiedChinese] || [currentSystemLanguage isEqualToString:kTraditionalChinese]) {
languageSaved = currentSystemLanguage;
}
else {
languageSaved = kEnglish;
}
if ([self loadDictionaryForLanguage:languageSaved]) {
self.saveInUserDefaults = YES;
}
}
else {
languageSaved = [_defaults objectForKey:kSaveLanguageDefaultKey];
if (languageSaved && ![languageSaved isEqualToString:@"DeviceLanguage"])
{
[self loadDictionaryForLanguage:languageSaved];
}
else {
if ([currentSystemLanguage isEqualToString:kSimplifiedChinese] || [currentSystemLanguage isEqualToString:kTraditionalChinese]) {
languageSaved = currentSystemLanguage;
}
else {
languageSaved = kEnglish;
}
if ([self loadDictionaryForLanguage:languageSaved]) {
self.saveInUserDefaults = YES;
}
}
}
}
return self;
}
#pragma mark - saveInIUserDefaults custom accesser/setter
-(void)setSaveInUserDefaults:(BOOL)saveInUserDefaults
{
if (saveInUserDefaults)
{
[self.defaults setObject:_currentLanguage forKey:kSaveLanguageDefaultKey];
}
else
{
[self.defaults removeObjectForKey:kSaveLanguageDefaultKey];
}
[self.defaults synchronize];
}
#pragma mark - Private Instance methods
-(BOOL)loadDictionaryForLanguage:(NSString *)newLanguage
{
NSURL * urlPath = [[NSBundle bundleForClass:[self class]] URLForResource:@"Localizable" withExtension:@"strings" subdirectory:nil localization:newLanguage];
if ([[NSFileManager defaultManager] fileExistsAtPath:urlPath.path])
{
self.currentLanguage = [newLanguage copy];
self.dicoLocalisation = [[NSDictionary dictionaryWithContentsOfFile:urlPath.path] copy];
return YES;
}
return NO;
}
#pragma mark - Public Instance methods
-(NSString *)localizedStringForKey:(NSString*)key
{
if (self.dicoLocalisation == nil)
{
return NSLocalizedString(key, key);
}
else
{
NSString * localizedString = self.dicoLocalisation[key];
if (localizedString == nil)
localizedString = key;
return localizedString;
}
}
-(BOOL)setLanguage:(NSString *)newLanguage
{
if (newLanguage == nil || [newLanguage isEqualToString:self.currentLanguage] || ![self.availableLanguagesArray containsObject:newLanguage])
return NO;
NSDictionary *dic = @{kLastLanguage: self.currentLanguage, kCurrentLanguage : newLanguage};
if ([newLanguage isEqualToString:@"DeviceLanguage"])
{
self.currentLanguage = [newLanguage copy];
self.dicoLocalisation = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLanguageChanged
object:nil
userInfo:dic];
return YES;
}
else
{
BOOL isLoadingOk = [self loadDictionaryForLanguage:newLanguage];
if (isLoadingOk)
{
self.saveInUserDefaults = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLanguageChanged
object:nil
userInfo:dic];
}
return isLoadingOk;
}
}
@end