標籤:
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開發--應用國際化,應用內切換語言