如何强制应用更改iOS / Objective-C中的语言?

时间:2015-12-07 10:02:15

标签: ios objective-c

我遇到问题,就像this app那样立即使用应用更改语言。

我发现了很多类似的问题,例如thisthis

但不幸的是,这些问题的相关答案对我不起作用。当我点击一个单元格时,我立即需要我的应用程序更改语言。

有什么想法吗?提前谢谢。

4 个答案:

答案 0 :(得分:4)

我的解决方案

首先,您需要创建字符串文件

Go to Project Right Click.
New File->iOS
Under iOS you can see the Resource
Click Resource and you can see the String File among the group of files.
Click that give name whatever you want.

现在您可以在下面创建字符串文件。

"Date"="Date";

"Time"="Time";

"Done"="Done";

 "Back"="Back";

在appDelegate.h中

-(NSString*) languageSelectedStringForKey:(NSString*) key
{
  NSString *path;
  AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
  if(app.currentLanguage==ENGLISH)
  path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
  else if(app.currentLanguage==TAMIL)
  path = [[NSBundle mainBundle] pathForResource:@"ta-IN" ofType:@"lproj"];
  else if(app.currentLanguage==SPANISH)
  path = [[NSBundle mainBundle] pathForResource:@"es" ofType:@"lproj"];
  else if(app.currentLanguage==FRENCH)
  path = [[NSBundle mainBundle] pathForResource:@"fr" ofType:@"lproj"];
  else if(app.currentLanguage==JAPANESE)
  path = [[NSBundle mainBundle] pathForResource:@"ja" ofType:@"lproj"];
  else if(app.currentLanguage==GERMAN)
  path = [[NSBundle mainBundle] pathForResource:@"de" ofType:@"lproj"];
  else if(app.currentLanguage==KOREAN)
  path = [[NSBundle mainBundle] pathForResource:@"ko" ofType:@"lproj"];
  else if(app.currentLanguage==RUSSIAN)
  path = [[NSBundle mainBundle] pathForResource:@"ru" ofType:@"lproj"];
  else if(app.currentLanguage==HINDI)
  path = [[NSBundle mainBundle] pathForResource:@"hi" ofType:@"lproj"];
  else if(app.currentLanguage==CHINESE)
  path = [[NSBundle mainBundle] pathForResource:@"zh-Hans" ofType:@"lproj"];
  else if(app.currentLanguage==ITALIAN)
  path = [[NSBundle mainBundle] pathForResource:@"it" ofType:@"lproj"];
  else if(app.currentLanguage==PORTUGUESE)
  path = [[NSBundle mainBundle] pathForResource:@"pt" ofType:@"lproj"];
  else if(app.currentLanguage==THAI)
  path = [[NSBundle mainBundle] pathForResource:@"th" ofType:@"lproj"];
  else if(app.currentLanguage==MALAY)
  path = [[NSBundle mainBundle] pathForResource:@"ms" ofType:@"lproj"];
  else if(app.currentLanguage==INDONESIAN)
  path = [[NSBundle mainBundle] pathForResource:@"id" ofType:@"lproj"];
  else if(app.currentLanguage==CHINESE1)
  path = [[NSBundle mainBundle] pathForResource:@"zh-Hant" ofType:@"lproj"];
  else
  {
    path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
  }
  NSBundle* languageBundle = [NSBundle bundleWithPath:path];
  NSString* str=[languageBundle localizedStringForKey:key value:@"" table:@"LocalizeSTRING"];
  return str;
 }

在appDelegate.m的didFinishLaunchingWithOptions中调用上面的方法

NSString *str=[[[NSUserDefaults standardUserDefaults]objectForKey:@"AppleLanguages"] objectAtIndex:0];

if([str isEqualToString:[NSString stringWithFormat: @"en"]])
{
    currentLanguage=ENGLISH;
    selectedrow=ENGLISH;
}
else if([str isEqualToString:[NSString stringWithFormat: @"ta-IN"]])
{
    currentLanguage=TAMIL;
    selectedrow=TAMIL;
}
else if([str isEqualToString:[NSString stringWithFormat: @"es"]])
{
    currentLanguage=SPANISH;
    selectedrow=SPANISH;
}
else if([str isEqualToString:[NSString stringWithFormat: @"fr"]])
{
    currentLanguage=FRENCH;
    selectedrow=FRENCH;
}
else if([str isEqualToString:[NSString stringWithFormat: @"ja"]])
{
    currentLanguage=JAPANESE;
    selectedrow=JAPANESE;
}
else if([str isEqualToString:[NSString stringWithFormat: @"de"]])
{
    currentLanguage=GERMAN;
    selectedrow=GERMAN;
}
else if([str isEqualToString:[NSString stringWithFormat: @"ko"]])
{
    currentLanguage=KOREAN;
    selectedrow=KOREAN;
}
else if([str isEqualToString:[NSString stringWithFormat: @"ru"]])
{
    currentLanguage=RUSSIAN;
    selectedrow=RUSSIAN;
}
else if([str isEqualToString:[NSString stringWithFormat: @"hi"]])
{
    currentLanguage=HINDI;
    selectedrow=HINDI;
}
else if([str isEqualToString:[NSString stringWithFormat: @"zh-Hans"]])
{
    currentLanguage=CHINESE;
    selectedrow=CHINESE;
}
else if([str isEqualToString:[NSString stringWithFormat: @"it"]])
{
    currentLanguage=ITALIAN;
    selectedrow=ITALIAN;
}
else if([str isEqualToString:[NSString stringWithFormat: @"pt"]])
{
    currentLanguage=PORTUGUESE;
    selectedrow=PORTUGUESE;
}
else if([str isEqualToString:[NSString stringWithFormat: @"th"]])
{
    currentLanguage=THAI;
    selectedrow=THAI;
}
else if([str isEqualToString:[NSString stringWithFormat: @"ms"]])
{
    currentLanguage=MALAY;
    selectedrow=MALAY;
}
else if([str isEqualToString:[NSString stringWithFormat: @"id"]])
{
    currentLanguage=INDONESIAN;
    selectedrow=INDONESIAN;
}

else if([str isEqualToString:[NSString stringWithFormat: @"zh-Hant"]])
{
    currentLanguage=CHINESE1;
    selectedrow=CHINESE1;
}

[self languageSelectedStringForKey:str];

然后在您的必需视图控制器

-(NSString*) languageSelectedStringForKey:(NSString*) key
{
  app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
  if(app.currentLanguage==ENGLISH)
  path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
  else if(app.currentLanguage==TAMIL)
  path = [[NSBundle mainBundle] pathForResource:@"ta-IN" ofType:@"lproj"];
  else if(app.currentLanguage==SPANISH)
  path = [[NSBundle mainBundle] pathForResource:@"es" ofType:@"lproj"];
  else if(app.currentLanguage==FRENCH)
  path = [[NSBundle mainBundle] pathForResource:@"fr" ofType:@"lproj"];
  else if(app.currentLanguage==JAPANESE)
  path = [[NSBundle mainBundle] pathForResource:@"ja" ofType:@"lproj"];
  else if(app.currentLanguage==GERMAN)
  path = [[NSBundle mainBundle] pathForResource:@"de" ofType:@"lproj"];
  else if(app.currentLanguage==KOREAN)
  path = [[NSBundle mainBundle] pathForResource:@"ko" ofType:@"lproj"];
  else if(app.currentLanguage==RUSSIAN)
  path = [[NSBundle mainBundle] pathForResource:@"ru" ofType:@"lproj"];
  else if(app.currentLanguage==HINDI)
  path = [[NSBundle mainBundle] pathForResource:@"hi" ofType:@"lproj"];
  else if(app.currentLanguage==CHINESE)
  path = [[NSBundle mainBundle] pathForResource:@"zh-Hans" ofType:@"lproj"];
  else if(app.currentLanguage==ITALIAN)
  path = [[NSBundle mainBundle] pathForResource:@"it" ofType:@"lproj"];
  else if(app.currentLanguage==PORTUGUESE)
  path = [[NSBundle mainBundle] pathForResource:@"pt" ofType:@"lproj"];
  else if(app.currentLanguage==THAI)
  path = [[NSBundle mainBundle] pathForResource:@"th" ofType:@"lproj"];
  else if(app.currentLanguage==MALAY)
  path = [[NSBundle mainBundle] pathForResource:@"ms" ofType:@"lproj"];
  else if(app.currentLanguage==INDONESIAN)
  path = [[NSBundle mainBundle] pathForResource:@"id" ofType:@"lproj"];
  else if(app.currentLanguage==CHINESE1)
  path = [[NSBundle mainBundle] pathForResource:@"zh-Hant" ofType:@"lproj"];
  else
  {
    path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
  }
  NSBundle* languageBundle = [NSBundle bundleWithPath:path];
  NSString* str=[languageBundle localizedStringForKey:key value:@"" table:@"LocalizeSTRING"];
  return str;
}

在viewDidLoad和viewWillAppear方法中调用上述方法或调用您要调用的方法

- (void)viewWillAppear:(BOOL)animated
{
  //If you want to set language in label,TextFiled
  localizeBackLabel.text=[self languageSelectedStringForKey:@"Back"];
  localizeDoneLabel.text=[self languageSelectedStringForKey:@"Done"];
  localizeTimeLabel.text=[self languageSelectedStringForKey:@"Time"];
  localizeDateLabel.text=[self languageSelectedStringForKey:@"Date"];

  //If you want to set language in button
  [yourButton setTitle:[self languageSelectedStringForKey:@"Back"]; forState:UIControlStateNormal];
  [yourButton setTitle:[self languageSelectedStringForKey:@"Done"]; forState:UIControlStateNormal];

   //If you want to set the Language in cell     
   cell.labelHomeList.text=[self languageSelectedStringForKey:@"Date"];
   cell.labelHomeList.text=[self languageSelectedStringForKey:@"Time"];
}

最后,如果您想将语言更改为其他语言。例如在(设置)表中您有两种语言。如果您想将其更改为其他语言,请按照以下编码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{  
   AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
   app.selectedrow=indexPath.row;
   if (indexPath.row==0)
   {
     app.currentLanguage=ENGLISH;
     [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil]forKey:@"AppleLanguages"];
     [[NSUserDefaults standardUserDefaults] synchronize];
      NSLog(@"%d", [[NSUserDefaults standardUserDefaults] synchronize]);
    }
    else 
    {
      app.currentLanguage=CHINESE1;
      [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-Hant", nil]forKey:@"AppleLanguages"];
      [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

效果很好。

答案 1 :(得分:1)

使用reloadData

[YourUITableView reloadData];

并且不要忘记将您的语言保存在NSUserDefaults

 [[NSUserDefaults standardUserDefaults] setValue:lang forKey:LANG];
 [[NSUserDefaults standardUserDefaults] synchronize];

因此您可以相应地加载视图和故事板,因为重新加载tableView后,您可以添加语言检查以在cellForRowAtIndexPath method中显示正确的视图。

修改.....

如果您想使用NSNotification,请在viewDidLoad中注册您的观察者,因为

[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(updateLanguage:)
                                             name:@"updateLanguageObserver"
                                           object:nil];

并在didSelectRowAtIndexPath添加侦听器

[[NSNotificationCenter defaultCenter] postNotificationName:@"updateLanguageObserver" object:self userInfo:nil];  

将调用您的方法updateLanguage,您可以在其中处理本地化和tableView重新加载。

并且不要忘记在viewController中添加侦听器以删除观察者

 - (void)dealloc {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
 }

答案 2 :(得分:0)

你应该看一次:HERE

答案 3 :(得分:-1)

我认为this会解决您的问题。

1个月前我遇到了same问题。

您可以关注this教程。

示例:

.strings文件中,您需要翻译:{{1​​}}

"hello" = "HOLA MUNDO";

编辑:这是用户在评论中提出的其他信息:
如果您要翻译您的语言选项更改的整个视图,我建议您制作将被调用的方法每当你改变语言时:

#import "LocalizationSystem.h"

LocalizationSetLanguage(@"Spanish");
CCLabel* label = [CCLabel labelWithString:AMLocalizedString(@"hello",@"Hello World") fontName:@"Marker Felt" fontSize:32];