-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLocalizationSystem.m
executable file
·132 lines (110 loc) · 3.3 KB
/
LocalizationSystem.m
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
//
// LocalizationSystem.m
// Battle of Puppets
//
// Created by Juan Albero Sanchis on 27/02/10.
// Copyright Aggressive Mediocrity 2010. All rights reserved.
//
#import "LocalizationSystem.h"
@implementation LocalizationSystem
//Singleton instance
static LocalizationSystem *_sharedLocalSystem = nil;
//Current application bungle to get the languages.
static NSBundle *bundle = nil;
+(LocalizationSystem *)sharedLocalSystem {
static dispatch_once_t once;
dispatch_once(&once, ^ {
_sharedLocalSystem = [[super allocWithZone:nil] init];
[_sharedLocalSystem setLanguage:[_sharedLocalSystem getLanguage]];
});
return _sharedLocalSystem;
}
- (id)init {
if (_sharedLocalSystem) {
return _sharedLocalSystem;
}
if (self = [super init]) {
//
}
return self;
}
- (id)copy {
return [[self class] sharedLocalSystem];
}
+ (id)allocWithZone:(NSZone *)zone {
return [self sharedLocalSystem];
}
+(id)alloc
{
@synchronized([LocalizationSystem class])
{
NSAssert(_sharedLocalSystem == nil, @"Attempted to allocate a second instance of a singleton.");
_sharedLocalSystem = [super alloc];
return _sharedLocalSystem;
}
// to avoid compiler warning
return nil;
}
// Gets the current localized string as in NSLocalizedString.
//
// example calls:
// AMLocalizedString(@"Text to localize",@"Alternative text, in case hte other is not find");
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)comment
{
if (!bundle) {
bundle = nil;
}
return [bundle localizedStringForKey:key value:comment table:nil];
}
// Sets the desired language of the ones you have.
// example calls:
// LocalizationSetLanguage(@"Italian");
// LocalizationSetLanguage(@"German");
// LocalizationSetLanguage(@"Spanish");
//
// If this function is not called it will use the default OS language.
// If the language does not exists y returns the default OS language.
- (void) setLanguage:(NSString*) l{
#ifdef DEBUG
NSLog(@"preferredLang: %@", l);
#endif
NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:@"lproj" ];
if ([l isEqualToString:@"en"]) {
path = [[ NSBundle mainBundle ] pathForResource:@"Base" ofType:@"lproj" ];
}
if (path == nil) {
//in case the language does not exists
[self resetLocalization];
}
else {
bundle = [NSBundle bundleWithPath:path];
//
[[NSUserDefaults standardUserDefaults] setObject:@[l] forKey:@"AppleLanguages"];
//
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
// Just gets the current setted up language.
// returns "es","fr",...
//
// example call:
// NSString * currentL = LocalizationGetLanguage;
- (NSString*) getLanguage{
NSArray* languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
NSString *preferredLang = [languages objectAtIndex:0];
return preferredLang;
}
// Resets the localization system, so it uses the OS default language.
//
// example call:
// LocalizationReset;
- (void) resetLocalization
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"AppleLanguages"];
//
[[NSUserDefaults standardUserDefaults] synchronize];
//
NSString *path = [[NSBundle mainBundle] pathForResource:@"Base" ofType:@"lproj" ];
bundle = [NSBundle bundleWithPath:path];
}
@end