2015-12-07 43 views
0

我在使用应用更改语言时出现问题,例如this app如何强制应用程序在iOS/Objective-C中更改语言?

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

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

任何想法?预先感谢。

+0

是否使用XIB或STORYBOARD? – user3182143

+0

[iOS-​​App(iPhone和iPad)中手动语言选择的可能重复]](http://stackoverflow.com/questions/9939885/manual-language-selection-in-an-ios-app-iphone-and- ipad) –

+0

@JanakLN:我的第二个链接是你的链接。但我认为它不能解决我的问题。顺便说一句,谢谢你的帮助:) –

回答

-1

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

我在1个月前有same问题。

您可以按照this教程。

例子:

在你.strings文件你把翻译:"hello" = "HOLA MUNDO";

#import "LocalizationSystem.h" 

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

编辑:这是附加信息如用户要求在评论:
如果你想在那里翻译全看你的语言选项已更改,我建议您制作每次更改语言时都会调用的方法:

-(void)translateOnChoose { 
self.label.text = AMLocalizedString(@"hello",@"Hello World"); 
} 
+2

你的答案在这些链接断裂的时刻基本上是空的。您应该至少张贴解决方案的大纲,和/或源代码/伪代码。 –

+0

我已经发布了示例代码。希望现在好了。感谢提示。 – Stefan

+0

但是这样,我怎么能强制我的视图控制器运行viewDidLoad? (我把所有的代码放在viewDidLoad中)。当我将代码放在viewWillAppear或viewDidAppear中时,此解决方案正常工作。 –

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重装。

和程序增加了要移除的侦听观察者这样

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

我已经这样做了,但我认为有更好的方法,比如NSNotification或委托。顺便说一句,感谢您的帮助:) –

+0

欢迎您! :) –

+0

我在我的NSNotification的答案中添加了一些信息。希望能帮助到你。 –

3

我的解决方案

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

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. 

现在的viewController不要忘记你可以创建下面的字符串文件lihis

"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; 
} 

呼叫在您所需的视图控制器

-(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; 
} 

呼叫在这上面的方法appDelegate.m

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

随后的didFinishLaunchingWithOptions这上面的方法你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"]; 
} 

最后,如果你想改变你想要将其更改为其他语言,你有两个languages.If在(设置)表中的语言其他language.For例子,请按照下面的编码

- (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

我想在一个月前完成。 :) 好主意。 – Stefan

+0

如果它是有用的,请upvote我的答案。 – user3182143

0

您应该看一下这个:HERE

相关问题