作者:zhao_yin

  为啥要说应用程序自身?因为普通的本地化是根据设备当前的设置来完成的。而实际中一些应用往往需要与设备设置无关的本地化。例如一款游戏在游戏中,玩家可以选择游戏的语言,但是并不会改变所用设备的语言。

  近期工作需要,尝试着实现一下这类需求,经测试还能使用,暂且记下,如大家有更好或者更简单的方法还望不吝赐教。

本例为Xcode 4.2版本

一个简易应用程序本地化类:

#import lt;Foundation/Foundation.hgt;

@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)setAppLanguageNSString *)language_temp;

-(NSString *)getAppLanguageString:(NSString *)string_key;
-(UIImage *)getAppLanguageImage:(NSString *)imageName;

@end



#import quot;XUI_language_Base.hquot;

@implementation XUI_language_Base

@synthesize language_array;
@synthesize language_path;
@synthesize language_bundle;


-(void)dealloc{
[language_bundle release];
[language_array release];
[language_path release];

[superdealloc];
}


-(void)initialize{
self.language_array = [ [NSArray alloc] initWithObjects:

@quot;enquot;,
@quot;zh-Hansquot;,

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 {
language_select = [ [NSLocale currentLocale] localeIdentifier];
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 ofTypequot;lprojquot;];

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:@quot;lprojquot;];

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:@quot;pngquot;];


// need the name with type
// NSString *image_path = [ NSString stringWithFormat:@quot;%@/%@quot;,self.language_path,imageName];


NSLog(@quot;img path %@quot;,image_path);


return [UIImage imageWithContentsOfFile:image_path];
}



@end

  也许不用实例方法而用 类方法也可以实现一些功能,但个人觉得一些路径存起来用着舒服些。可以在应用初始化创建一个对象,并调用initialize方法。相关配置自行存储于应用程序本地文件,具体实现方法也是各种各样。笔者的项目为程序,一些设置的存取都是调用人家给的接口,即上面的各种todo。另外运行时设置语言后,自然也需要即时处理当前显示内容。

锐亚教育

锐亚教育,游戏开发论坛|游戏制作人|游戏策划|游戏开发|独立游戏|游戏产业|游戏研发|游戏运营| unity|unity3d|unity3d官网|unity3d 教程|金融帝国3|8k8k8k|mcafee8.5i|游戏蛮牛|蛮牛 unity|蛮牛