The core is to use
NSString *path = [[NSBundle mainBundle]pathForResource:currentLanguage ofType:@"lproj"]; if (path) { localeBundle = [NSBundle bundleWithPath:path]; }else{ localeBundle= [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj" ]]; }
Change the value of localeBundle
Ideas:
1. Use
NSLocalizedStringFromTableInBundle(@"TNC_title", nil, localeBundle] , @""),
Different internationalization files can be transformed in real time
*Internationalization files are generally placed in the folder with the lproj suffix in the file directory system: the form is zh-Hant.lproj (traditional) zh-Hants.lproj (simplified) en.lproj (English)
Therefore, the corresponding file name also needs to be corresponding to the pathForResource:currentLanguage above.
2. Refresh the text acquisition of the UI again
Project-based code example:
Create a tool class.h:
#import <Foundation/Foundation.h> @interface LanguageTool : NSObject +(instancetype)getInstance; - (NSBundle *)getLocaleBundle; -(NSString *)getTap; -(NSString *)getCurrentLanguage; -(void)changeLanguage:(NSString*)language; @end
.m:
#import "LanguageTool.h" #import "SessionManager.h" #define CNS @"zh" #define IN @"in" #define tap @"change_language" static NSBundle *localeBundle = nil; static NSString *currentLanguage = nil; @interface LanguageTool() @end @implementation LanguageTool // singleton +(instancetype)getInstance{ static LanguageTool *manager = nil; static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ manager = [[LanguageTool alloc] init]; }); return manager; } -(void)changeLanguage:(NSString*)language{ currentLanguage = language; NSString *path = [[NSBundle mainBundle]pathForResource:language ofType:@"lproj"]; if (path && ![@"en" isEqualToString:language]) { localeBundle = [NSBundle bundleWithPath:path]; }else{ localeBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj" ]]; } } //The purpose of this method is to use the system default language if nothing is selected, otherwise use the language selected by the user, and it will not be changed by closing the app. -(void)judgeLanguage{ //Get last change language NSString *lastChangeLanguage = nil; //Use a persistence to keep the last language selection lastChangeLanguage = [SessionManager getSession:[SessionManager getLastLanguageTap]]; NSLog(@"last chage language: %@",lastChangeLanguage); //Language priority: currentLanguage > lastChangeLanguage > system default language //User does not set the language: get the system default language if (!lastChangeLanguage) { if (!currentLanguage) { //Get the system default language: //Intercept: zh-Hant-HK Remove area reservation: zh-Hant NSLog(@"-------system language:%@",[NSLocale preferredLanguages][0]); currentLanguage = [[NSLocale preferredLanguages][0] substringToIndex:2]; if ([currentLanguage containsString:@"zh"]) { currentLanguage = [NSString stringWithFormat:@"%@%@",currentLanguage,@"-Hant"]; } } }else if(!currentLanguage){ //There is currently no language switching, there is a set language in the app, and the language in the app is preferred currentLanguage = lastChangeLanguage; } } - (NSBundle *)getLocaleBundle{ [self judgeLanguage]; NSLog(@"------current language :%@",currentLanguage); NSLog(@"-------system language:%@",[NSLocale preferredLanguages][0]); NSString *path = [[NSBundle mainBundle]pathForResource:currentLanguage ofType:@"lproj"]; if (path) { localeBundle = [NSBundle bundleWithPath:path]; }else{ localeBundle= [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj" ]]; } return localeBundle; } //mark -(NSString *)getTap{ return tap; } -(NSString *)getCurrentLanguage{ // if (!currentLanguage) { // //Intercept: zh-Hant-HK Remove area reservation: zh-Hant // currentLanguage = [[NSLocale preferredLanguages][0] substringToIndex:2]; // } [self judgeLanguage]; NSLog(@"current language:%@",currentLanguage); return currentLanguage; } @end
Use to call after clicking where you need to switch languages:
//If it is currently in English, switch to traditional Chinese if ([@"en" isEqualToString:[[LanguageTool getInstance] getCurrentLanguage]]) { [self changeLanguage:@"zh-Hant"]; } else { [self changeLanguage:@"en"]; } //Notify other pages to switch languages too [self callLanguageChangeNotificationReceiver];
-(void)changeLanguage:(NSString*)language{ [[LanguageTool getInstance] changeLanguage:language]; // refresh some UI text [self initTableResource]; [_tableView reloadData]; }
//for example refresh like this:
-(void)initTableResource{ _dataArray = @[NSLocalizedStringFromTableInBundle(@"Home", nil, [[LanguageTool getInstance] getLocaleBundle] , @""), NSLocalizedStringFromTableInBundle(@"customization", nil, [[LanguageTool getInstance] getLocaleBundle] , @"") , NSLocalizedStringFromTableInBundle(@"TNC_title", nil, [[LanguageTool getInstance] getLocaleBundle] , @""), NSLocalizedStringFromTableInBundle(@"localizable", nil, [[LanguageTool getInstance] getLocaleBundle] , @"")]; NSString *app_version = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleShortVersionString"]; _app_version_string = [NSString stringWithFormat:@"%@ %@",NSLocalizedStringFromTableInBundle(@"slider_version_info", nil, [[LanguageTool getInstance] getLocaleBundle] , @""),app_version]; if (_version_label) { _version_label.text = _app_version_string; } [self initContentVersionLabel]; }
//Set the notification:
-(void)callLanguageChangeNotificationReceiver{ [SessionManager setSession:[SessionManager getLastLanguageTap] value:[[LanguageTool getInstance] getCurrentLanguage]]; NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center postNotificationName:[[LanguageTool getInstance] getTap] object:nil]; }
The notified page receives the notification and refreshes the UI. The operation is not complicated:
-(void)initNotification{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTextView) name:[[LanguageTool getInstance]getTap] object:nil]; } -(void)refreshTextView{ _tncTextView.text = NSLocalizedStringFromTableInBundle(@"TNC_text", nil, [[LanguageTool getInstance] getLocaleBundle] , @""); _navItem.title = NSLocalizedStringFromTableInBundle(@"TNC_title", nil, [[LanguageTool getInstance] getLocaleBundle] , @""); }
material:
1.http://blog.csdn.net/feng2qing/article/details/60479620