问题
I am new in iOS and I am facing problem in language conversion
For English I am using code like this
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults]synchronize];
//to set the selected language at runtime (dynamically)
NSLog(@"Language set=Malay");
[NSBundle setLanguage:@"en"];
MenuScreen *menu=[[MenuScreen alloc] initWithNibName:@"MenuScreen" bundle:nil];
[self.navigationController pushViewController:menu animated:YES];
For Thai Language I used code like this
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"th-TH", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults]synchronize];
//to set the selected language at runtime (dynamically)
NSLog(@"Language set=Malay");
[NSBundle setLanguage:@"th-TH"];
MenuScreen *menu=[[MenuScreen alloc] initWithNibName:@"MenuScreen" bundle:nil];
[self.navigationController pushViewController:menu animated:YES];
But every time it required to restart app. Is there is any solution for this or any thing I am doing wrong. Thanks in Advance!
回答1:
Muju I created sample project and I worked getting solution for your question.I got the solution perfectly.
In my below sample I want to change "Welcome to Thailand" to "ยินดีต้อนรับสู่ประเทศไทย".I use localization concept for this.
Before going to steps, I want you to see my storyboard designing
Please follow the below steps.
STEP 1:Click Project->info->Localization->Click +
Now it shows the drop down list of Language.From that we should select Thai
STEP 2:Once we choose or select the Language from drop down list,it shows the below window and we need to click Finish button
Now it looks like below
STEP 3:Create the String File for the localization and set the name.
above I set String file name as LocalizationThai
STEP 4:Click the LocalizationThai.strings also click the File Inspector.Click the Localization inside the File Inspector.Now it shows the below pop up box.
STEP 5:Click Localize.Once you Localize,it shows below like this
STEP 6:Click 3 Checkboxes
Now in bundle we have 3 files under LocalizationThai.strings
STEP 7:Write your required changing text in string files.
i.In LocalizationThai.strings(Thai) file I write below text
ii.In LocalizationThai.strings(English) file I write below text
iii.In LocalizationThai.strings(Base) file I write below text
STEP 8:Create the Header File for the multiple Languages.
STEP 9 : set the Header name(I set header name as LocalizationHeader) and define Languages in the Header file like below
LocalizationHeader.h
#ifndef LocalizationHeader_h
#define LocalizationHeader_h
#define ENGLISH 0
#define THAI 1
#endif /* LocalizationHeader_h */
STEP 10:Implement the below coding part
Localization.h
#import <Foundation/Foundation.h>
#import "LocalizationHeader.h"
@interface Localization : NSObject
+(Localization *)sharedInstance;
+(NSString*) strSelectLanguage:(int)curLang;
+(NSString*) languageSelectedStringForKey:(NSString*) key;
@end
Localization.m
#import "Localization.h"
int currentLanguage,selectedrow;
@implementation Localization
+(Localization *)sharedInstance
{
static Localization *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[Localization alloc] init];
});
return sharedInstance;
}
+(NSString*) strSelectLanguage:(int)curLang{
if(curLang==THAI){
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"th", nil]forKey:@"AppleLanguages"];
}
else{
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil]forKey:@"AppleLanguages"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
currentLanguage=curLang;
NSString *strLangSelect = [[[NSUserDefaults standardUserDefaults]objectForKey:@"AppleLanguages"] objectAtIndex:0];
return strLangSelect;
}
+(NSString*) languageSelectedStringForKey:(NSString*) key
{
NSString *path;
NSString *strSelectedLanguage = [[[NSUserDefaults standardUserDefaults]objectForKey:@"AppleLanguages"] objectAtIndex:0];
//When we check with iPhone,iPad device it shows "en-US".So we need to change it to "en"
if([strSelectedLanguage hasPrefix:@"en-"])
strSelectedLanguage = [strSelectedLanguage stringByReplacingOccurrencesOfString:@"en-US" withString:@"en"];
if([strSelectedLanguage isEqualToString:[NSString stringWithFormat: @"en"]]){
currentLanguage=ENGLISH;
selectedrow=ENGLISH;
path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
}
else{
currentLanguage=THAI;
selectedrow=THAI;
path = [[NSBundle mainBundle] pathForResource:@"th" ofType:@"lproj"];
}
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
NSString* str=[languageBundle localizedStringForKey:key value:@"" table:@"LocalizationThai"];
return str;
}
@end
ViewController.h
#import <UIKit/UIKit.h>
#import "Localization.h"
@interface ViewController : UIViewController{
Localization *localization;
}
@property (strong, nonatomic) IBOutlet UILabel *lblWelcome;
- (IBAction)actionChangeLanToThai:(id)sender;
- (IBAction)actionChangeLangToEng:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize lblWelcome;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
localization = [Localization sharedInstance];
lblWelcome.text = [Localization languageSelectedStringForKey:@"Welcome to Thailand"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)actionChangeLanToThai:(id)sender {
[Localization strSelectLanguage:THAI];
lblWelcome.text = [Localization languageSelectedStringForKey:@"Welcome to Thailand"];
}
- (IBAction)actionChangeLangToEng:(id)sender {
[Localization strSelectLanguage:ENGLISH];
lblWelcome.text = [Localization languageSelectedStringForKey:@"Welcome to Thailand"];
}
@end
When I run the app first
Then When I change the Language from English to Thai
Again when I change it to English
You have to follow the same steps for XIB
Below is for XIB
I create the ViewController with XIB.ViewController name is RootViewController
Now see the designing part
AppDelegate.h
#import <UIKit/UIKit.h>
#import "RootViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) RootViewController *viewController;
@end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[navController setNavigationBarHidden:YES];
[self.window makeKeyAndVisible];
return YES;
}
RootViewController.h
#import <UIKit/UIKit.h>
#import "Localization.h"
@interface RootViewController : UIViewController{
Localization *localization;
}
@property (strong, nonatomic) IBOutlet UILabel *lblWelcomeThaiLang;
- (IBAction)actionChangeLangToThai:(id)sender;
- (IBAction)actionChangeLangToEng:(id)sender;
@end
RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
@synthesize lblWelcomeThaiLang;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)actionChangeLangToThai:(id)sender {
[Localization strSelectLanguage:THAI];
lblWelcomeThaiLang.text = [Localization languageSelectedStringForKey:@"Welcome to Thailand"];
}
- (IBAction)actionChangeLangToEng:(id)sender {
[Localization strSelectLanguage:ENGLISH];
lblWelcomeThaiLang.text = [Localization languageSelectedStringForKey:@"Welcome to Thailand"];
}
@end
Now see the result
回答2:
Hi in ios we have bundle for to store the localization files.. When we switch the language using [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"th-TH", nil] forKey:@"AppleLanguages"]; [[NSUserDefaults standardUserDefaults]synchronize];
it just change the locale of current application not the bundle of localization.
To change the language inApp you need to change the language bundle for that. To change language inApp you need to use
BundleLocalization
https://github.com/cmaftuleac/BundleLocalization
[BundleLocalization sharedInstance].language = @"de"; NSLog(@"Application language: %@", [BundleLocalization sharedInstance].language);
来源:https://stackoverflow.com/questions/44353180/why-language-change-required-app-to-restart-in-objective-c