如何强制NSLocalizedString使用特定语言

如何强制NSLocalizedString使用特定语言

在iPhone上NSLocalizedString返回iPhone语言中的字符串。有没有可能强迫NSLocalizedString使用一种特定的语言使应用程序使用与设备不同的语言?



桃花长相依
浏览 1166回答 4
4回答

慕婉清6462132

请勿在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, @""));

富国沪深

NSLocalizedString()中的“AppleLanguage”键(及其变体)NSUserDefaults若要确定用户对首选语言的设置,请执行以下操作。这将返回一个语言代码数组,第一个代码是用户为其手机设置的代码,如果资源在首选语言中不可用,则返回后续代码作为后备。(在桌面上,用户可以使用系统首选项中的自定义顺序指定多种语言)如果您愿意,可以通过使用setObject:Forkey:Method设置您自己的语言列表来覆盖您自己的应用程序的全局设置。这将优先于全局设置值,并返回到正在执行本地化的应用程序中的任何代码。用于此的代码如下所示:[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"de", @"en", @"fr", nil] forKey:@"AppleLanguages"]; [[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate这将使德语成为您的应用程序的首选语言,以英语和法语作为后盾。您可能希望在应用程序启动的早期就将其命名为此。您可以在这里阅读更多关于语言/区域设置的首选项:国际化编程主题:获取当前语言和地区

波斯汪

我最近也有同样的问题,我不想开始修补我的整个NSLocalizedString也不强迫应用程序重新启动,以使新语言工作。我希望一切都能按原样运作。我的解决方案是动态更改主包的类并在那里加载适当的包:头文件@interface&nbsp;NSBundle&nbsp;(Language)+(void)setLanguage:(NSString*)language;@end实施#import&nbsp;<objc/runtime.h>static&nbsp;const&nbsp;char&nbsp;_bundle=0;@interface&nbsp;BundleEx&nbsp;:&nbsp; NSBundle@end@implementation&nbsp;BundleEx-(NSString*)localizedStringForKey:(NSString&nbsp;*)key&nbsp;value:(NSString&nbsp;*)value&nbsp;table:(NSString&nbsp;*)tableName{ &nbsp;&nbsp;&nbsp;&nbsp;NSBundle*&nbsp;bundle=objc_getAssociatedObject(self,&nbsp;&_bundle); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;bundle&nbsp;?&nbsp;[bundle&nbsp;localizedStringForKey:key&nbsp;value:value&nbsp;table:tableName]&nbsp;:&nbsp;[super&nbsp;localizedStringForKey: &nbsp;&nbsp;&nbsp;&nbsp;key&nbsp;value:value&nbsp;table:tableName];}@end@implementation&nbsp;NSBundle&nbsp;(Language)+(void)setLanguage:(NSString*)language{ &nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;dispatch_once_t&nbsp;onceToken; &nbsp;&nbsp;&nbsp;&nbsp;dispatch_once(&onceToken,&nbsp;^ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;object_setClass([NSBundle&nbsp;mainBundle],[BundleEx&nbsp;class]); &nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;objc_setAssociatedObject([NSBundle&nbsp;mainBundle],&nbsp;&_bundle,&nbsp;language&nbsp;?&nbsp;[NSBundle&nbsp;bundleWithPath:[[NSBundle&nbsp;mainBundle]&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;pathForResource:language&nbsp;ofType:@"lproj"]]&nbsp;:&nbsp;nil,&nbsp;OBJC_ASSOCIATION_RETAIN_NONATOMIC);}@end因此,基本上,当应用程序启动时,在加载第一个控制器之前,只需调用:[NSBundle&nbsp;setLanguage:@"en"];当用户在设置屏幕上更改其首选语言时,只需再次调用它:[NSBundle&nbsp;setLanguage:@"fr"];若要重置为系统默认值,只需传递0:[NSBundle&nbsp;setLanguage:nil];好好享受.。对于那些需要SWIFT版本的人:var&nbsp;bundleKey:&nbsp;UInt8&nbsp;=&nbsp;0class&nbsp;AnyLanguageBundle:&nbsp;Bundle&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;override&nbsp;func&nbsp;localizedString(forKey&nbsp;key:&nbsp;String, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value:&nbsp;String?, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;table&nbsp;tableName:&nbsp;String?)&nbsp;->&nbsp;String&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guard&nbsp;let&nbsp;path&nbsp;=&nbsp;objc_getAssociatedObject(self,&nbsp;&bundleKey)&nbsp;as?&nbsp;String, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;bundle&nbsp;=&nbsp;Bundle(path:&nbsp;path)&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;super.localizedString(forKey:&nbsp;key,&nbsp;value:&nbsp;value,&nbsp;table:&nbsp;tableName) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;bundle.localizedString(forKey:&nbsp;key,&nbsp;value:&nbsp;value,&nbsp;table:&nbsp;tableName) &nbsp;&nbsp;&nbsp;&nbsp;}}extension&nbsp;Bundle&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;class&nbsp;func&nbsp;setLanguage(_&nbsp;language:&nbsp;String)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defer&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;object_setClass(Bundle.main,&nbsp;AnyLanguageBundle.self) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objc_setAssociatedObject(Bundle.main,&nbsp;&bundleKey,&nbsp;&nbsp;&nbsp;&nbsp;Bundle.main.path(forResource:&nbsp;language,&nbsp;ofType:&nbsp;"lproj"),&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.OBJC_ASSOCIATION_RETAIN_NONATOMIC) &nbsp;&nbsp;&nbsp;&nbsp;}}

慕尼黑的夜晚无繁华

我通常是这样做的,但是您必须在您的项目中拥有所有的本地化文件。@implementation&nbsp;Languagestatic&nbsp;NSBundle&nbsp;*bundle&nbsp;=&nbsp;nil;+(void)initialize&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;NSUserDefaults*&nbsp;defs&nbsp;=&nbsp;[NSUserDefaults&nbsp;standardUserDefaults]; &nbsp;&nbsp;&nbsp;&nbsp;NSArray*&nbsp;languages&nbsp;=&nbsp;[defs&nbsp;objectForKey:@"AppleLanguages"]; &nbsp;&nbsp;&nbsp;&nbsp;NSString&nbsp;*current&nbsp;=&nbsp;[[languages&nbsp;objectAtIndex:0]&nbsp;retain]; &nbsp;&nbsp;&nbsp;&nbsp;[self&nbsp;setLanguage:current];}/* &nbsp;&nbsp;example&nbsp;calls: &nbsp;&nbsp;&nbsp;&nbsp;[Language&nbsp;setLanguage:@"it"]; &nbsp;&nbsp;&nbsp;&nbsp;[Language&nbsp;setLanguage:@"de"]; */+(void)setLanguage:(NSString&nbsp;*)l{ &nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"preferredLang:&nbsp;%@",&nbsp;l); &nbsp;&nbsp;&nbsp;&nbsp;NSString&nbsp;*path&nbsp;=&nbsp;[[&nbsp;NSBundle&nbsp;mainBundle&nbsp;]&nbsp;pathForResource:l&nbsp;ofType:@"lproj"&nbsp;]; &nbsp;&nbsp;&nbsp;&nbsp;bundle&nbsp;=&nbsp;[[NSBundle&nbsp;bundleWithPath:path]&nbsp;retain];}+(NSString&nbsp;*)get:(NSString&nbsp;*)key&nbsp;alter:(NSString&nbsp;*)alternate&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[bundle&nbsp;localizedStringForKey:key&nbsp;value:alternate&nbsp;table:nil];}@end
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS