想要添加在我的应用程序中更改应用程序语言的可能性,所以当前的iPhone语言是英语。但是用户在应用程序中为我的应用程序设置土耳其语我必须强制我的应用程序在土耳其语本地化。
我已经将文件添加到setLanguage
LocalizationSystem.h
LocalizationSystem.m
on按钮动作我写下面的代码:
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] ;
}
请帮我解决问题。 但是当我从设备设置中选择语言时,语言就会发生变化。
答案 0 :(得分:0)
您必须在@" AppleLanguages"中设置属性。通过NSUserDefaults设置选定的语言代码。
像这样:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:<your code>, nil] forKey:@"AppleLanguages"];
答案 1 :(得分:0)
我在我的应用程序中使用它来更改语言使用:[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);
}
@end