希望在我的应用程序中添加一种在应用程序中更改语言的可能性,所以当当前的iPhone语言是英语时。但是用户在我的应用程序中设置土耳其语,我不得不强制我的应用程序用土耳其语进行本地化。
我已经在setLanguage中添加了文件
LocalizationSystem.h
LocalizationSystem.m
关于按钮操作,我编写了以下代码:
if([sender tag]==0)
{
LocalizationSetLanguage(@"en");
NSString * currentL = LocalizationGetLanguage;
NSLog(@"currentL EN:%@",currentL);
}
else
{
LocalizationSetLanguage(@"tr");
NSString * currentL = LocalizationGetLanguage;
NSLog(@"currentL TR:%@",currentL);
}这段代码不会改变语言。在这两种NSLog中,它都打印出以下一行:
2014-09-11 15:54:30.640 uyarbeni[6480:70b] currentL EN:en
2014-09-11 15:54:30.640 uyarbeni[6480:70b] currentL TR:en当我查看LocalizationSystem.m文件中的代码时
- (void) setLanguage:(NSString*) l
{
NSLog(@"preferredLang: %@", l);
NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:@"lproj" ];
if (path == nil)
[self resetLocalization];
else
bundle = [NSBundle bundleWithPath:path] ;
}请帮我解决这个问题。但是当我从设备设置中选择语言时,语言就会发生变化。
发布于 2014-09-11 11:40:56
您必须在@"AppleLanguages“中设置属性,才能通过NSUserDefaults设置所选语言代码。
就像这样:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:<your code>, nil] forKey:@"AppleLanguages"];
发布于 2014-09-11 11:49:59
我在我的应用程序中使用它,以更改语言使用:[NSBundle setLanguage:@"tr"];
使用本地化资源:[[NSBundle localBundle] pathForResource:@"List" ofType:@"plist"];
导入ProjectName中的头-前缀.Prefix.pch
//.h
#import <Foundation/Foundation.h>
@interface NSBundle (Language)
+ (NSBundle *)localBundle;
+ (void)setLanguage:(NSString*)language;
@end
//.m
#import "NSBundle + Language.h"
#import <objc/runtime.h>
NSString *currentLanguage;
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)
+ (NSBundle *)localBundle
{
if (currentLanguage)
{
NSString *path = [[NSBundle mainBundle] pathForResource:currentLanguage ofType:@"lproj"];
return [NSBundle bundleWithPath:path];
}
else
{
return [NSBundle mainBundle];
}
}
+(void)setLanguage:(NSString*)language
{
currentLanguage = 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);
}
@endhttps://stackoverflow.com/questions/25781690
复制相似问题