iOS開發--應用國際化,應用內切換語言

來源:互聯網
上載者:User

標籤:

1.前言

自己負責的項目需要做國際化,並且要求應用內部切換語言。這個是可以做到的,也並不難,可以直接戳Github看一下

https://github.com/leo90821/LocaliztionStringExchange

2.實現原理

原理比較簡單,利用NSBundle切換語言套件,主要利用NSBundle的方法 

- localizedStringForKey:value:table: 進行本地字串的切換

3.實現代碼

1)商務邏輯

標頭檔

#import <Foundation/Foundation.h>#define USER_DEFAULT_STRING @"userLanguage"#define APPLE_LANGUAGES @"AppleLanguages"/** * App BLL **/@interface BLL : NSObject/** * BLL single **/+ (instancetype)currentLogic;/** * App bundle **/+ (NSBundle *)bundle;/** * 初始化語言 **/- (void)initUserLanguage;/** * 獲得當前語言 **/- (NSString *)getCurrentLanguage;/** * 設定語言 **/- (void)setUserLanguageWithLanguage:(NSString *)language;@end

實現檔案

 

#import "BLL.h"@implementation BLLstatic NSBundle *bundle = nil;- (instancetype)init {    self = [super init];    return self;}+ (instancetype)currentLogic {    static BLL *logic = nil;    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        logic = [[BLL alloc] init];    });    return logic;}+ (NSBundle *)bundle {    return bundle;}/** * 初始化語言 **/- (void)initUserLanguage {    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];    NSString *string = [userDefault valueForKey:USER_DEFAULT_STRING];    if (string.length == 0) {        NSArray *languages = [userDefault objectForKey:APPLE_LANGUAGES];        NSString *current = [languages objectAtIndex:0];        string = current;        [userDefault setValue:string forKey:USER_DEFAULT_STRING];        [userDefault synchronize];    }        NSString *path = [[NSBundle mainBundle] pathForResource:string ofType:@"lproj"];    bundle = [NSBundle bundleWithPath:path];}/** * 獲得當前語言 **/- (NSString *)getCurrentLanguage {    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];    NSString *language = [userDefault valueForKey:USER_DEFAULT_STRING];    return language;}/** * 設定語言 **/- (void)setUserLanguageWithLanguage:(NSString *)language {    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];    bundle = [NSBundle bundleWithPath:path];    [userDefault setValue:language forKey:USER_DEFAULT_STRING];    [userDefault synchronize];}@end

2)視圖控制器

#import <UIKit/UIKit.h>#define NOTIFICATION_CHANGE_LANGUAGE @"ChangeLanguage"@interface ViewController : UIViewController@end
#import "ViewController.h"#import "BLL.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UILabel *tip;- (IBAction)changeLanguage:(id)sender;@property (weak, nonatomic) IBOutlet UIButton *button;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        [self.button setTitle:NSLocalizedStringFromTable(@"Change", @"MyString", nil) forState:UIControlStateNormal];    self.tip.text = NSLocalizedStringFromTable(@"Hello", @"MyString", nil);}- (void)viewWillAppear:(BOOL)animated {    [super viewWillAppear:animated];    [self addObservers];}- (void)viewWillDisappear:(BOOL)animated {    [super viewWillDisappear:animated];    [self removeObservers];}- (void)addObservers {    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshUI) name:NOTIFICATION_CHANGE_LANGUAGE object:nil];}- (void)removeObservers {    [[NSNotificationCenter defaultCenter] removeObserver:self name:NOTIFICATION_CHANGE_LANGUAGE object:nil];}- (void)refreshUI {        [self.button setTitle:[[BLL bundle] localizedStringForKey:@"Change" value:nil table:@"MyString"] forState:UIControlStateNormal];    self.tip.text = [[BLL bundle] localizedStringForKey:@"Hello" value:nil table:@"MyString"];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (IBAction)changeLanguage:(id)sender {    NSString *language = [[BLL currentLogic] getCurrentLanguage];        if ([language rangeOfString:@"en"].location != NSNotFound) {        [[BLL currentLogic] setUserLanguageWithLanguage:@"zh-Hans"];    } else {        [[BLL currentLogic] setUserLanguageWithLanguage:@"en"];    }    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_CHANGE_LANGUAGE object:nil     ];}@end

 

4.小結

原理比較簡單,利用NSBundle切換語言套件,主要利用NSBundle的方法 

- localizedStringForKey:value:table: 進行本地字串的切換,最麻煩的是重新整理每個頁面都需要設定訊息觀察者,這個就比較蛋疼。有沒有好的方法,歡迎留言交流。

iOS開發--應用國際化,應用內切換語言

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.