为啥要说应用程序自身?因为普通的本地化是根据设备当前的设置来完成的。而实际中一些应用往往需要与设备设置无关的本地化。例如一款游戏在游戏中,玩家可以选择游戏的语言,但是并不会改变所用设备的语言。
近期工作需要,尝试着实现一下这类需求,经测试还能使用,暂且记下,如大家有更好或者更简单的方法还望不吝赐教。
本例为Xcode 4.2版本
一个简易应用程序本地化类:
#import <Foundation/Foundation.h>
@interface XUI_language_Base : NSObject{
NSBundle *language_bundle;
NSString *language_path;
NSArray *language_array;
}
@property (nonatomic,retain) NSBundle *language_bundle;
@property (nonatomic,retain) NSString *language_path;
@property (nonatomic,retain) NSArray *language_array;
-(void)initialize;
-(void)setAppLanguage:(NSString *)language_temp;
-(NSString *)getAppLanguageString:(NSString *)string_key;
-(UIImage *)getAppLanguageImage:(NSString *)imageName;
@end
#import "XUI_language_Base.h"
@implementation XUI_language_Base
@synthesize language_array;
@synthesize language_path;
@synthesize language_bundle;
-(void)dealloc{
[language_bundle release];
[language_array release];
[language_path release];
[super dealloc];
}
-(void)initialize{
self.language_array = [ [NSArray alloc] initWithObjects:
@"en",
@"zh-Hans",
nil];
BOOL isLanguageSetted = YES;
//todo get app config find the switch
//end do
NSString *language_select;
if (isLanguageSetted) {
// todo get the language of config
language_select = [language_array objectAtIndex:0];
//end do
} else {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
language_select = [languages objectAtIndex:0];
BOOL isSupported = NO;
//check the language of locale isSupported
for(NSString *lan_temp in language_array){
if ( [lan_temp isEqualToString:language_select] ) {
isSupported = YES;
break;
}
}
if ( NO == isSupported ) {
language_select = [language_array objectAtIndex:0];
}
}
self.language_path = [ [NSBundle mainBundle] pathForResource:language_select ofType:@"lproj"];
self.language_bundle = [NSBundle bundleWithPath:self.language_path];
}
-(void)setAppLanguage:(NSString *)language_select{
BOOL isSupported = NO;
//check the language of locale isSupported
for(NSString *lan_temp in language_array){
if ( [lan_temp isEqualToString:language_select] ) {
isSupported = YES;
break;
}
}
if ( NO == isSupported ) {
language_select = [language_array objectAtIndex:0];
}
self.language_path = [ [NSBundle mainBundle] pathForResource:language_select ofType:@"lproj"];
self.language_bundle = [NSBundle bundleWithPath:self.language_path];
//todo set the app language and the switch value is yes
//end do
}
-(NSString *)getAppLanguageString:(NSString *)string_key{
return [self.language_bundle localizedStringForKey:string_key
value:string_key table:nil];
}
-(UIImage *)getAppLanguageImage:(NSString *)imageName{
NSString *image_path = [ self.language_bundle pathForResource:imageName ofType:@"png"];
// need the name with type
// NSString *image_path = [ NSString stringWithFormat:@"%@/%@",self.language_path,imageName];
NSLog(@"img path %@",image_path);
return [UIImage imageWithContentsOfFile:image_path];
}
@end
也许不用实例方法而用 类方法也可以实现一些功能,但个人觉得一些路径存起来用着舒服些。可以在应用初始化创建一个对象,并调用initialize方法。相关配置自行存储于应用程序本地文件,具体实现方法也是各种各样。笔者的项目为程序,一些设置的存取都是调用人家给的接口,即上面的各种todo。另外运行时设置语言后,自然也需要即时处理当前显示内容。
转载请注明来自:http://blog.csdn.net/zhao_yin