为什么语言更改需要在Objective C中重新启动应用程序

时间:2017-06-04 10:43:15

标签: ios objective-c multilingual

我是iOS的新手,我在语言转换方面面临问题

对于英语我使用的代码是

 [[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];

对于泰语我使用了这样的代码

[[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];

但每次都需要重启app。对于这个或我做错的任何事情是否有任何解决方案。 在此先感谢!

2 个答案:

答案 0 :(得分:2)

Muju我创建了示例项目,我为您的问题找到了解决方案。我得到了完美的解决方案。

在我的下面示例中,我想将“欢迎来到泰国”更改为“ยินดีต้อนรับสู่ประเทศไทย”。我使用本地化概念。

在开始步骤之前,我希望你能看到我的故事板设计

enter image description here

请按照以下步骤操作。

第1步:点击项目 - >信息 - >本地化 - >点击+

现在它显示了Language.From的下拉列表,我们应该选择泰语

enter image description here

第2步:我们选择或从下拉列表中选择语言后,会显示以下窗口,我们需要点击完成按钮

enter image description here

现在看起来像下面

enter image description here

第3步:为本地化创建字符串文件并设置名称。

enter image description here

enter image description here

enter image description here

上面我将String文件名设置为LocalizationThai

步骤4:单击LocalizationThai.strings也单击File Inspector。单击File Inspector中的Localization。现在它显示下面的弹出框。

enter image description here

第5步:点击Localize。一旦你进行本地化,它就会显示如下

enter image description here

第6步:点击3个复选框

enter image description here

现在在捆绑中,我们在LocalizationThai.strings

下有3个文件

enter image description here

第7步:在字符串文件中写下所需的更改文本。

i.In LocalizationThai.strings(Thai)file我在文字下面写

enter image description here

ii。在LocalizationThai.strings(英文)文件中我写下文

enter image description here

iii。在LocalizationThai.strings(Base)文件中,我在文字下面写

enter image description here

步骤8:为多种语言创建标题文件。

enter image description here

步骤9:设置标题名称(我将标题名称设置为LocalizationHeader)并在标题文件中定义语言,如下所示

enter image description here

LocalizationHeader.h

#ifndef LocalizationHeader_h
#define LocalizationHeader_h


#define ENGLISH 0
#define THAI 1


#endif /* LocalizationHeader_h */

第10步:实施以下编码部分

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

当我第一次运行应用程序时

enter image description here

然后当我将语言从英语更改为泰语

enter image description here

再次将其更改为英语

enter image description here

您必须按照XIB的相同步骤

以下是XIB

我使用XIB创建ViewController .ViewController名称是RootViewController enter image description here

现在看看设计部分 enter image description here

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

现在看结果

enter image description here

enter image description here

答案 1 :(得分:0)

您好ios我们有捆绑存储本地化文件.. 当我们使用时切换语言 [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@&#34; th-TH&#34;,nil] forKey:@&#34; AppleLanguages&#34;];             [[NSUserDefaults standardUserDefaults] synchronize];

它只是更改当前应用程序的语言环境而不是本地化包。

要更改inApp中的语言,您需要更改语言包。 要更改App中的语言,您需要使用

BundleLocalization

https://github.com/cmaftuleac/BundleLocalization

[BundleLocalization sharedInstance] .language = @&#34; de&#34 ;; NSLog(@&#34;应用程序语言:%@&#34;,[BundleLocalization sharedInstance] .language);