為啥要說應用程式自身?因為普通的本地化是根據裝置當前的設定來完成的。而實際中一些應用往往需要與裝置設定無關的本地化。例如一款遊戲在遊戲中,玩家可以選擇遊戲的語言,但是並不會改變所用裝置的語言。
近期工作需要,嘗試著實現一下這類需求,經測試還能使用,暫且記下,如大家有更好或者更簡單的方法還望不吝賜教。
本例為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