IOS应用程序自身的本地化

来源:互联网 发布:移动销售软件 编辑:程序博客网 时间:2024/06/02 12:42

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

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



本例为Xcode 4.2版本


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


[csharp] view plaincopyprint?
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface XUI_language_Base : NSObject{  
  4.     NSBundle    *language_bundle;  
  5.     NSString    *language_path;  
  6.     NSArray     *language_array;  
  7. }  
  8.   
  9. @property (nonatomic,retain)    NSBundle    *language_bundle;  
  10. @property (nonatomic,retain)    NSString    *language_path;  
  11. @property (nonatomic,retain)    NSArray     *language_array;  
  12.   
  13. -(void)initialize;  
  14. -(void)setAppLanguage:(NSString *)language_temp;  
  15.   
  16. -(NSString *)getAppLanguageString:(NSString *)string_key;  
  17. -(UIImage *)getAppLanguageImage:(NSString *)imageName;  
  18.   
  19. @end  


[csharp] view plaincopyprint?
  1. #import "XUI_language_Base.h"  
  2.   
  3. @implementation XUI_language_Base  
  4.   
  5. @synthesize language_array;  
  6. @synthesize language_path;  
  7. @synthesize language_bundle;  
  8.   
  9.   
  10. -(void)dealloc{  
  11.     [language_bundle    release];  
  12.     [language_array     release];  
  13.     [language_path     release];  
  14.       
  15.     [super  dealloc];  
  16. }  
  17.   
  18.   
  19. -(void)initialize{  
  20.     self.language_array = [ [NSArray alloc] initWithObjects:  
  21.                              
  22.                            @"en",  
  23.                            @"zh-Hans",  
  24.                              
  25.                            nil];  
  26.       
  27.     BOOL isLanguageSetted = YES;  
  28.       
  29.     //todo get app config find the switch  
  30.     //end do  
  31.       
  32.     NSString *language_select;  
  33.       
  34.     if (isLanguageSetted) {  
  35.         // todo get the language of config  
  36.           
  37.         language_select = [language_array objectAtIndex:0];  
  38.           
  39.         //end do  
  40.                   
  41.     } else {  
  42.         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];   
  43.         NSArray *languages = [defaults objectForKey:@"AppleLanguages"];   
  44.         language_select = [languages objectAtIndex:0];  
  45.           
  46.         BOOL isSupported = NO;  
  47.         //check the language of locale isSupported  
  48.           
  49.         for(NSString *lan_temp in language_array){  
  50.             if ( [lan_temp isEqualToString:language_select] ) {  
  51.                 isSupported = YES;  
  52.                 break;  
  53.             }  
  54.         }  
  55.           
  56.         if ( NO == isSupported ) {  
  57.             language_select = [language_array objectAtIndex:0];  
  58.         }  
  59.     }  
  60.       
  61.     self.language_path = [ [NSBundle mainBundle] pathForResource:language_select ofType:@"lproj"];  
  62.       
  63.     self.language_bundle = [NSBundle bundleWithPath:self.language_path];  
  64. }  
  65.   
  66.   
  67. -(void)setAppLanguage:(NSString *)language_select{  
  68.     BOOL isSupported = NO;  
  69.     //check the language of locale isSupported  
  70.       
  71.     for(NSString *lan_temp in language_array){  
  72.         if ( [lan_temp isEqualToString:language_select] ) {  
  73.             isSupported = YES;  
  74.             break;  
  75.         }  
  76.     }  
  77.       
  78.     if ( NO == isSupported ) {  
  79.         language_select = [language_array objectAtIndex:0];  
  80.     }  
  81.       
  82.     self.language_path = [ [NSBundle mainBundle] pathForResource:language_select ofType:@"lproj"];    
  83.       
  84.     self.language_bundle = [NSBundle bundleWithPath:self.language_path];    
  85.       
  86.     //todo set the app language and the switch value is yes  
  87.       
  88.     //end do  
  89. }  
  90.   
  91.   
  92.   
  93. -(NSString *)getAppLanguageString:(NSString *)string_key{  
  94.     return [self.language_bundle localizedStringForKey:string_key  
  95.                                                  value:string_key table:nil];  
  96. }  
  97.   
  98.   
  99.   
  100. -(UIImage *)getAppLanguageImage:(NSString *)imageName{  
  101.     NSString *image_path = [ self.language_bundle pathForResource:imageName ofType:@"png"];  
  102.       
  103.       
  104.     // need the name with type  
  105.     // NSString *image_path = [ NSString stringWithFormat:@"%@/%@",self.language_path,imageName];  
  106.       
  107.       
  108.     NSLog(@"img path %@",image_path);  
  109.       
  110.       
  111.     return [UIImage imageWithContentsOfFile:image_path];  
  112. }  
  113.   
  114.   
  115.   
  116. @end  


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


转载请注明来自:http://blog.csdn.net/zhao_yin


0 0
原创粉丝点击