简体   繁体   English

iPhone中的语言翻译

[英]Language translation in iphone

I am trying to build an app which consists of a label which displays the text in English by default. 我正在尝试构建一个包含标签的应用程序,该标签默认情况下以英文显示文本。 The user gets a list to select his/her language and after selecting the language, the text changes to that particular language. 用户获得一个列表以选择他/她的语言,并且在选择语言之后,文本将更改为该特定语言。 Any idea as to how to do it? 关于如何做的任何想法? I had tried 我试过了

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"de", @"en", @"fr", nil] forKey:@"AppleLanguages"]; 

but it is not working. 但它不起作用。

You can do that with : 您可以使用:

-[NSBundle localizedStringForKey:value:table:]

There is a small sample in the docs. 文档中有一个小样本。 Basically what you need to do is create a MyTable.strings file with the localizations you need. 基本上,您需要做的是使用所需的本地化创建MyTable.strings文件。 Create one file per language you need. 用您需要的每种语言创建一个文件。 Then do: 然后做:

NSBundle *bundle = [NSBundle mainBundle];
NSString *localizedString = [bundle localizedStringForKey:@"TheKeyYouWantToLocalize"]
                                                    value:@"TheDefaultValue" 
                                                    table:@"MyTable"];

This method will look for the key: @"TheKeyYouWantToLocalize" in MyTable.strings file , if its is found then it will return that otherwise it will return @"TheDefaultValue" 此方法将在MyTable.strings文件中查找键: @"TheKeyYouWantToLocalize" ,如果找到它,则返回该键,否则返回@"TheDefaultValue"

FYI, This is the same process the system uses when localizing an app. 仅供参考,这与系统本地化应用程序时使用的过程相同。 (Heard of NSLocalizedString ?) but now you have to do it manually since you are asking the user the language to show and not relying in the system language. (听说过NSLocalizedString吗?),但是现在您必须手动执行此操作,因为您是在询问用户显示的语言,而不是依赖于系统语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM