forked from efrederickson/Multiplexer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRALocalizer.mm
38 lines (32 loc) · 1010 Bytes
/
RALocalizer.mm
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
#import "RALocalizer.h"
#import "headers.h"
@implementation RALocalizer
+ (instancetype)sharedInstance {
SHARED_INSTANCE2(RALocalizer, [sharedInstance loadTranslation]);
}
- (BOOL)attemptLoadForLanguageCode:(NSString*)code {
NSString *expandedPath = [NSString stringWithFormat:@"%@/Localizations/%@.strings",RA_BASE_PATH,code];
NSDictionary *plist = [NSDictionary dictionaryWithContentsOfFile:expandedPath];
if (plist) {
translation = plist;
return YES;
}
return NO;
}
- (void)loadTranslation {
NSArray *langs = [NSLocale preferredLanguages];
for (NSString *lang in langs) {
NSDictionary *components = [NSLocale componentsFromLocaleIdentifier:lang];
NSString *languageDesignator = components[NSLocaleLanguageCode];
if ([self attemptLoadForLanguageCode:languageDesignator]) {
break;
}
}
if (!translation) {
[self attemptLoadForLanguageCode:@"en"];
}
}
- (NSString*)localizedStringForKey:(NSString*)key {
return ![translation objectForKey:key] ? key : translation[key];
}
@end