-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
Localization.m
50 lines (42 loc) · 1.37 KB
/
Localization.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
//
// Localization.m
// LiveContainer
//
// Created by s s on 2024/9/21.
//
#import "Localization.h"
@implementation NSString (Localization)
// Class method for the English language bundle
+ (NSBundle *)enBundle {
static NSBundle *enBundle = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *language = @"en";
NSString *path = [[NSUserDefaults lcMainBundle] pathForResource:language ofType:@"lproj"];
enBundle = [NSBundle bundleWithPath:path];
});
return enBundle;
}
// Instance method to return a localized string
- (NSString *)localized {
NSString *message = [[NSUserDefaults lcMainBundle] localizedStringForKey:self value:@"" table:nil];
if (![message isEqualToString:self]) {
return message;
}
NSString *forcedString = [[NSString enBundle] localizedStringForKey:self value:nil table:nil];
if (forcedString) {
return forcedString;
} else {
return self;
}
}
// Instance method for localization with format
- (NSString *)localizeWithFormat:(NSString*)arg1, ... {
va_list args;
va_start(args, arg1);
NSString *formattedString = [NSString localizedStringWithFormat:[self localized], arg1, args];
// NSString *formattedString = [[NSString alloc] localized:[self localized] arguments:arg1, args];
va_end(args);
return formattedString;
}
@end