IOS應用程式自身的本地化

來源:互聯網
上載者:User

為啥要說應用程式自身?因為普通的本地化是根據裝置當前的設定來完成的。而實際中一些應用往往需要與裝置設定無關的本地化。例如一款遊戲在遊戲中,玩家可以選擇遊戲的語言,但是並不會改變所用裝置的語言。

近期工作需要,嘗試著實現一下這類需求,經測試還能使用,暫且記下,如大家有更好或者更簡單的方法還望不吝賜教。

本例為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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.