问题描述
我是iOS的新手,我在语言转换方面遇到了问题
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!
推荐答案
Muju我创建了示例项目,并努力为您的问题找到解决方案.我完美地找到了解决方案.
Muju I created sample project and I worked getting solution for your question.I got the solution perfectly.
在下面的示例中,我要将"欢迎来到泰国"更改为ยtoนดีต้อนรับสู่ประเทศไทย".为此,我使用了本地化概念.
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
请按照以下步骤操作.
步骤1:单击项目"->信息"->本地化"->单击+
现在它显示语言的下拉列表.从中我们应该选择泰语
第2步:选择或从下拉列表中选择语言后,它将显示以下窗口,我们需要单击完成"按钮
现在看起来像下面
步骤3:为本地化创建字符串文件并设置名称.
以上,我将String文件名设置为LocalizationThai
above I set String file name as LocalizationThai
步骤4:单击LocalizationThai.字符串也单击File Inspector.单击File Inspector内部的Localization.现在它显示以下弹出框.
步骤5:单击本地化".完成本地化后,它会显示在下面
步骤6:点击3个复选框
现在捆绑包中,我们在LocalizationThai.strings下有3个文件
Now in bundle we have 3 files under LocalizationThai.strings
步骤7:在字符串文件中写入所需的更改文本.
i.我在LocalizationThai.strings(Thai)文件中写在文本下方
i.In LocalizationThai.strings(Thai) file I write below text
ii.在LocalizationThai.strings(英文)文件中,我在文本下方写上
ii.In LocalizationThai.strings(English) file I write below text
iii.在LocalizationThai.strings(Base)文件中,我在文本下方写上
iii.In LocalizationThai.strings(Base) file I write below text
步骤8:为多种语言创建头文件.
第9步:设置标题名称(我将标题名称设置为LocalizationHeader)并在标题文件中定义语言,如下所示
LocalizationHeader.h
LocalizationHeader.h
#ifndef LocalizationHeader_h
#define LocalizationHeader_h
#define ENGLISH 0
#define THAI 1
#endif /* LocalizationHeader_h */
第10步:实施以下编码部分
Localization.h
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
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
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
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
再次将其更改为英语
您必须对XIB遵循相同的步骤
You have to follow the same steps for XIB
以下是XIB
我用XIB创建了ViewController.ViewController的名字是RootViewController
I create the ViewController with XIB.ViewController name is RootViewController
现在请参阅设计部分
Now see the designing part
AppDelegate.h
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
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
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
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
现在查看结果
这篇关于为什么需要更改语言才能在目标C中重新启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!