forked from tagyro/AMLocalizedString
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalizationSystem.m
More file actions
111 lines (88 loc) · 2.59 KB
/
LocalizationSystem.m
File metadata and controls
111 lines (88 loc) · 2.59 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
//
// 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]; });
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
{
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{
NSLog(@"preferredLang: %@", l);
NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:@"lproj" ];
if (path == nil)
//in case the language does not exists
[self resetLocalization];
else
bundle = [NSBundle bundleWithPath:path];
}
// 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
{
bundle = [NSBundle mainBundle];
}
@end