16

我需要向我的服务器发送特定字符串的本地化列表。

意思是,如果我的应用程序有一个字符串 Foo,它在英语中本地化为 @"Foo",在俄语中本地化为 @"Фу",我想向服务器发送如下列表:

  • 字符串Foo:
    • 英语:“福”
    • 俄语:“Фу”

我认为我需要能够做的是:

  1. 为我的应用本地化的每种语言枚举本地化字符串
  2. 获取每种语言的本地化版本 Foo

我该怎么做(1),我该怎么做(2)?

4

1 回答 1

36

您可以通过读取 English.lproj/Localizable.strings 作为字典并获取其键来检索所有字符串键:

NSString *stringsPath = [[NSBundle mainBundle] pathForResource:@"Localizable" ofType:@"strings"];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:stringsPath];

要获得每种语言的翻译,您可以遍历每个英语键的语言并使用NSLocalizedStringFromTableInBundle

for (NSString *language in [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"]) {
    NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]];
    NSLog(@"%@: %@", language, NSLocalizedStringFromTableInBundle(@"Testing", @"Localizable", bundle, nil));
}
于 2011-06-24T14:10:41.017 回答