4 回答
TA贡献1804条经验 获得超2个赞
请勿在iOS 9上使用。这将对通过它传递的所有字符串返回零。
我找到了另一个解决方案,允许您更新语言字符串,w/o重新启动应用程序,并与genstring兼容:
将这个宏放在前缀中.pch:
#define currentLanguageBundle [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[[NSLocale preferredLanguages] objectAtIndex:0] ofType:@"lproj"]]
在任何需要本地化字符串的地方:
NSLocalizedStringFromTableInBundle(@"GalleryTitleKey", nil, currentLanguageBundle, @"")
设置语言使用:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:@"de"] forKey:@"AppleLanguages"];
即使在连续的语言跳跃中也能工作,例如:
NSLog(@"test %@", NSLocalizedStringFromTableInBundle(@"NewKey", nil, currentLanguageBundle, @""));
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:@"fr"] forKey:@"AppleLanguages"];
NSLog(@"test %@", NSLocalizedStringFromTableInBundle(@"NewKey", nil, currentLanguageBundle, @""));
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:@"it"] forKey:@"AppleLanguages"];
NSLog(@"test %@", NSLocalizedStringFromTableInBundle(@"NewKey", nil, currentLanguageBundle, @""));
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:@"de"] forKey:@"AppleLanguages"];
NSLog(@"test %@", NSLocalizedStringFromTableInBundle(@"NewKey", nil, currentLanguageBundle, @""));
TA贡献1790条经验 获得超9个赞
NSLocalizedString()
NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"de", @"en", @"fr", nil] forKey:@"AppleLanguages"]; [[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate
TA贡献1811条经验 获得超4个赞
头文件
@interface NSBundle (Language)+(void)setLanguage:(NSString*)language;@end
实施
#import <objc/runtime.h>static const char _bundle=0;@interface BundleEx : NSBundle@end@implementation BundleEx-(NSString*)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName{ NSBundle* bundle=objc_getAssociatedObject(self, &_bundle); return bundle ? [bundle localizedStringForKey:key value:value table:tableName] : [super localizedStringForKey: key value:value table:tableName];}@end@implementation NSBundle (Language)+(void)setLanguage:(NSString*)language{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^ { object_setClass([NSBundle mainBundle],[BundleEx class]); }); objc_setAssociatedObject([NSBundle mainBundle], &_bundle, language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}@end
[NSBundle setLanguage:@"en"];
[NSBundle setLanguage:@"fr"];
[NSBundle setLanguage:nil];
var bundleKey: UInt8 = 0class AnyLanguageBundle: Bundle { override func localizedString(forKey key: String, value: String?, table tableName: String?) -> String { guard let path = objc_getAssociatedObject(self, &bundleKey) as? String, let bundle = Bundle(path: path) else { return super.localizedString(forKey: key, value: value, table: tableName) } return bundle.localizedString(forKey: key, value: value, table: tableName) }}extension Bundle { class func setLanguage(_ language: String) { defer { object_setClass(Bundle.main, AnyLanguageBundle.self) } objc_setAssociatedObject(Bundle.main, &bundleKey, Bundle.main.path(forResource: language, ofType: "lproj"), .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }}
TA贡献1864条经验 获得超6个赞
@implementation Languagestatic NSBundle *bundle = nil;+(void)initialize { NSUserDefaults* defs = [NSUserDefaults standardUserDefaults]; NSArray* languages = [defs objectForKey:@"AppleLanguages"]; NSString *current = [[languages objectAtIndex:0] retain]; [self setLanguage:current];}/* example calls: [Language setLanguage:@"it"]; [Language setLanguage:@"de"]; */+(void)setLanguage:(NSString *)l{ NSLog(@"preferredLang: %@", l); NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:@"lproj" ]; bundle = [[NSBundle bundleWithPath:path] retain];}+(NSString *)get:(NSString *)key alter:(NSString *)alternate { return [bundle localizedStringForKey:key value:alternate table:nil];}@end
- 4 回答
- 0 关注
- 1156 浏览
添加回答
举报